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

ESM compabillity #70

Closed
Tracked by #636
viceice opened this issue Jul 20, 2021 · 9 comments · Fixed by #83
Closed
Tracked by #636

ESM compabillity #70

viceice opened this issue Jul 20, 2021 · 9 comments · Fixed by #83

Comments

@viceice
Copy link

viceice commented Jul 20, 2021

Since 1.0.17 or 1.0.18 this package no longer works with jest in esm mode.

image

I think it's because of the new different typescript compilation.

@winfr34k
Copy link

winfr34k commented Aug 5, 2021

This seems like it's still an issue. We've updated our project to use ESMs and experience the same error message. I thought that the 2.0 release would be enough.

Is this related to jestjs/jest#11677?

@marchaos
Copy link
Owner

marchaos commented Aug 9, 2021

Not related. I'll take a look.

@viceice
Copy link
Author

viceice commented Aug 9, 2021

It's the ways typescript exports in newer versions. for esm the export must be enumerable i think. Thats the only difference i've seen in the version diff.

@marchaos
Copy link
Owner

marchaos commented Aug 9, 2021

Can you test out version 2.0.2-beta2? That version has both commonjs and ESM modules so should work with both.

@winfr34k
Copy link

winfr34k commented Aug 9, 2021

@marchaos At least for me this doesn't change a thing:

 FAIL  tests/client/medium.spec.ts
  ● Test suite failed to run

    SyntaxError: The requested module 'jest-mock-extended' does not provide an export named 'mock'

      at Runtime.linkAndEvaluateModule (node_modules/jest-runtime/build/index.js:669:5)
☁  communityvi-frontend [update/jest-v27] ⚡  grep "mock" package.json
		"jest-mock-extended": "^2.0.2-beta2",
☁  communityvi-frontend [update/jest-v27] ⚡  grep "mock" package-lock.json
				"jest-mock-extended": "^2.0.2-beta2",
				"jest-mock": "^27.0.6"
				"jest-mock": "^27.0.6",
				"jest-mock": "^27.0.6",
				"jest-mock": "^27.0.6",
		"node_modules/jest-mock": {
			"resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-27.0.6.tgz",
		"node_modules/jest-mock-extended": {
			"resolved": "https://registry.npmjs.org/jest-mock-extended/-/jest-mock-extended-2.0.2-beta2.tgz",
		"node_modules/jest-mock/node_modules/@jest/types": {
		"node_modules/jest-mock/node_modules/@types/yargs": {
				"jest-mock": "^27.0.6",
				"jest-mock": "^27.0.6"
				"jest-mock": "^27.0.6",
				"jest-mock": "^27.0.6",
				"jest-mock": "^27.0.6",
		"jest-mock": {
			"resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-27.0.6.tgz",
		"jest-mock-extended": {
			"resolved": "https://registry.npmjs.org/jest-mock-extended/-/jest-mock-extended-2.0.2-beta2.tgz",
				"jest-mock": "^27.0.6",

@valerybugakov
Copy link

Same issue here. Any suggestion on how to resolve it?

@shrink
Copy link

shrink commented Nov 29, 2021

For anyone with the same issue, I've been unable to work out how to use the mock function because there seems to be some issue with how it's exported, but as a workaround, the deepMock method works in its place.

@Toilal
Copy link
Contributor

Toilal commented Feb 28, 2022

Maybe the cause if that jest property is not globally available in an ESM environement, please read this issue

jestjs/jest#9430

jest "global" property
This is not really a global - it's injected into the module scope. Since the module scope is gone in ESM, we need to move it somewhere. Adding it to import.meta seems natural - there's an option called initializeImportMeta which we can use.

EDIT: Solution here is to fetch it via import {jest} from '@jest/globals'. We might still add it via import.meta in the future, but this should be enough for now.

I'll give it a try and open a pull request if it's fix the issue.

@Toilal
Copy link
Contributor

Toilal commented Feb 28, 2022

I have opened a pull request that should solve the issue.

Note that jest-resolve actually doesn't support hybrid package.json (ESM + CJS) as node Conditional exports properties are NOT read here :

https://github.com/facebook/jest/blob/a20bd2c31e126fc998c2407cfc6c1ecf39ead709/packages/jest-resolve/src/shouldLoadAsEsm.ts#L74-L94

For extension-less imports (like dependencies), it only checks for type: 'module' property into package.json.

So, when importing jest-mock-extended inside a jest test, it will use CJS even if your try to implement an hybrid package. For more info about how to create an hybrid pattern, check this post : https://2ality.com/2019/10/hybrid-npm-packages.html

Beraliv pushed a commit to Beraliv/jest-mock-extended that referenced this issue May 1, 2024
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

Successfully merging a pull request may close this issue.

6 participants