4

There is a simple question I cannot find an answer to. When using:

  1. Node 17 + Typescript
  2. ESM ("type": "module" in package.json)

Can I implement mock for one function only? or module?

If so, can I get an example? No matter what framework you use.

thanks!!

package.json:

{
    "test": "node --experimental-vm-modules  ./node_modules/.bin/jest --bail --colors --coverage --detectOpenHandles --forceExit --verbose",

    "jest": {
        "preset": "ts-jest/presets/default-esm",
        "transform": {
            ".ts": "ts-jest"
        },
        "testEnvironment": "node",
        "moduleFileExtensions": [
            "ts",
            "js",
            "json"
        ],
        "transformIgnorePatterns": [
            "<rootDir>/node_modules/"
        ],
        "globals": {
            "ts-jest": {
                "useESM": true
            }
        }
    }
}

Jest.spy gives me an error: cannot assign to read-only property I tried using Sinon too, and I got: es modules cannot be stubbed

Romain Durand
  • 783
  • 1
  • 6
  • 22
Gal Rab
  • 41
  • 3
  • Please provide enough code so others can better understand or reproduce the problem. – Community Mar 06 '22 at 20:54
  • 1
    Developments in ESM support in jest is blocked due to 2 bugs in v8 and chromium. This is a real frustration because 1000s of projects are blocked from migrating to the esm. You can read about it here https://github.com/facebook/jest/issues/9430#issuecomment-1055787154 – Wajahath Mar 07 '22 at 01:26
  • Plus you may find this thread interesting https://stackoverflow.com/q/35240469/3370568 – Wajahath Mar 07 '22 at 01:37

0 Answers0