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 jest plugins that use jest globals (jest is not defined) #2667

Closed
4 tasks done
fregante opened this issue Jan 15, 2023 · 10 comments
Closed
4 tasks done

Support jest plugins that use jest globals (jest is not defined) #2667

fregante opened this issue Jan 15, 2023 · 10 comments
Labels
documentation Improvements or additions to documentation

Comments

@fregante
Copy link
Contributor

Clear and concise description of the problem

I'm trying to use https://github.com/clarkbw/jest-webextension-mock in Vitest but it fails on load even if I use globals: true because the package expects the jest global

import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    globals: true,
    setupFiles: [
      "jest-webextension-mock"
    ]
  },
})

Suggested solution

Add jest global in dependencies as a tentative compatibility layer, optionally.

Alternative

Add explicit vitest support to every single package I try using 😰 with productivity going 📉

Additional context

No response

Validations

@fregante
Copy link
Contributor Author

fregante commented Jan 15, 2023

I also tried setting the jest global via setupFiles but that didn't reach the dependency:

import {defineConfig} from 'vitest/config';

export default defineConfig({
	test: {
		globals: true,
		setupFiles: [
			'./vitest.setup.js',
			'jest-webextension-mock',
		],
	},
});
// vitest.setup.js
import {vi} from 'vitest';

global.jest = vi;

This was also suggested by a member elsewhere: #1193 (comment)

@sheremet-va
Copy link
Member

sheremet-va commented Jan 15, 2023

You can add jest global before importing setup file:

globalThis.jest = vi
import "jest-webextension-mock"

@fregante
Copy link
Contributor Author

fregante commented Jan 15, 2023

That won't shouldn't work because import statements are hoisted, can that be documented or made it easier as I suggested?

@sheremet-va
Copy link
Member

sheremet-va commented Jan 15, 2023

That won't work because import statements are hoisted

Try before commenting, please. Import statements are not hoisted in Vitest to support module mocking.

If you are too afraid to break something, you can use await import

@sheremet-va
Copy link
Member

sheremet-va commented Jan 15, 2023

can that be documented or made it easier as I suggested?

Can be documented, yes.

We will never add jest global to Vitest. Other solutions are welcome. For example, we can add "prepareSetupFiles", since they are called in parallel and can't reliably affect each other.

@sheremet-va sheremet-va reopened this Jan 15, 2023
@sheremet-va sheremet-va added the documentation Improvements or additions to documentation label Jan 15, 2023
@fregante
Copy link
Contributor Author

How come my previous example didn't work? #2667 (comment)

I assume setupFiles are meant to create globals among other things

@sheremet-va
Copy link
Member

How come my previous example didn't work? #2667 (comment)

I assume setupFiles are meant to create globals among other things

Yes. They are running in parallel, not sequential, so it's possible mock resolved before globals were setup.

@o-alexandrov
Copy link

Since there's vi.importActual, could you please clarify plans to support jest plugins that use:

  • jest.requireActual

@sheremet-va
Copy link
Member

Since there's vi.importActual, could you please clarify plans to support jest plugins that use:

  • jest.requireActual

No plans to support requireActual.

@sheremet-va
Copy link
Member

You can use test.sequence.setupFiles: "list" to run setup files one after another. The hack with global.jest on the first line will stop working once Vite 4.3 is released as it will follow ESM spec for import hoisting.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants