marchaos / jest-mock-extended

Type safe mocking extensions for Jest https://www.npmjs.com/package/jest-mock-extended
MIT License
828 stars 57 forks source link

ESM compabillity #70

Closed viceice closed 2 years ago

viceice commented 3 years ago

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 commented 3 years ago

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 https://github.com/facebook/jest/issues/11677?

marchaos commented 3 years ago

Not related. I'll take a look.

viceice commented 3 years ago

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 commented 3 years ago

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

winfr34k commented 3 years ago

@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 commented 2 years ago

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

shrink commented 2 years ago

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 commented 2 years ago

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

https://github.com/facebook/jest/issues/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 commented 2 years ago

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