support arbitrary `import.meta.*` syntax (vitest insource testing)
musicq opened this issue · 7 comments
Environment
- Node: v18.16.1
- Jiti: 1.20.0
Reproduction
if (import.meta.vitest) {}
Describe the bug
I'm using vitest with insource testing, it uses a specific variable import.meta.vitest
which is not supported by jiti.
Additional context
No response
Logs
console.log(import.meta.vitest);
^^^^
SyntaxError: Cannot use 'import.meta' outside a module
at new Script (node:vm:100:7)
Thanks for reporting issue.
I really do hope if vitest could also support import.meta.env.vitest
or import.meta.env.test
as an alternative which is currently well supported in jiti.
But we might also think to support this specific import.meta
key from import-meta-env.ts transformer.
/cc @antfu
import.meta.vitest
is actually the whole vitest
module, the full usage would be:
// in-source test suites
if (import.meta.vitest) {
const { it, expect } = import.meta.vitest
it('add', () => {
expect(add()).toBe(0)
expect(add(1)).toBe(1)
expect(add(1, 2, 3)).toBe(6)
})
}
It's not just a flag, so it probably can't be in import.meta.env
, and probably won't work in jiti's env anyway. The work on #158 might solve this.
Thanks for explaining now it makes sense. It is different from #158 i guess the goal here is make same .ts
code runnable both in jiti and vitest environments.
console.log(import.meta.vitest);
in a normal .mjs
files returns undefined
so i think that's something we can fix in jiti transformer. (once had ESM support with jiti.import()
or another workaround)
Yeah I think import.meta
should be polyfilled in non-esm jiti env as it's designed to hold arbitrary data
I've got a similar issue. I'm trying to use parseMarkdown
from https://github.com/nuxt-modules/mdc/blob/main/README.md#parsing-markdown within a script that is run by jiti.
ERROR Cannot use 'import.meta' outside a module 2:56:12 PM
if (import.meta.dev)
^^^^
SyntaxError: Cannot use 'import.meta' outside a module
at new Script (node:vm:99:7)
I get this trying to use import.meta.dirname