Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support arbitrary import.meta.* syntax (vitest insource testing) #167

Open
musicq opened this issue Sep 21, 2023 · 5 comments
Open

support arbitrary import.meta.* syntax (vitest insource testing) #167

musicq opened this issue Sep 21, 2023 · 5 comments

Comments

@musicq
Copy link

musicq commented Sep 21, 2023

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)
@pi0
Copy link
Member

pi0 commented Sep 21, 2023

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

@antfu
Copy link
Member

antfu commented Sep 21, 2023

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.

@pi0
Copy link
Member

pi0 commented Sep 21, 2023

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)

@pi0 pi0 changed the title vitest insource testing is not supported support arbitrary import.meta.* (vitest insource testing) Sep 21, 2023
@pi0 pi0 changed the title support arbitrary import.meta.* (vitest insource testing) support arbitrary import.meta.* syntax (vitest insource testing) Sep 21, 2023
@antfu
Copy link
Member

antfu commented Sep 21, 2023

Yeah I think import.meta should be polyfilled in non-esm jiti env as it's designed to hold arbitrary data

@Barbapapazes
Copy link
Member

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants