pacote icon indicating copy to clipboard operation
pacote copied to clipboard

Cannot find module '@pacote/jest-either' from 'test/.../...' when upgrade to 5.0.0

Open recklyss opened this issue 4 years ago • 1 comments

What

When I upgrade @pacote/jest-either from 4.0.9 to 5.0.0, got error when running test:

Test suite failed to run

    Cannot find module '@pacote/jest-either' from ...

ENV

  • Node 14
  • "ts-jest": "27.0.5"
  • "jest": "27.1.0"

Code

import matchers from '@pacote/jest-either';
expect.extend(matchers);

...

expect(something).toBeLeft();

I tried some solutions searched from StackOverflow, didn't solve the problem, can anyone help with it?

Tried: modify modulePaths or moduleDirectories, didn't work...

recklyss avatar Nov 04 '21 02:11 recklyss

There seems to be an issue with the package, but for ESM support in Jest this is what I've been able to piece together:

  • npm install --save-dev jest@next ts-jest@next
  • Follow setup instructions for ts-jest with ESM support
  • Setup test script to run with NODE_OPTIONS=--experimental-vm-modules
  • Import @pacote/jest-either/lib instead of just @pacote/jest-either (this is the part that needs to be fixed)

Sample project

package.json

{
  "scripts": {
    "test": "NODE_OPTIONS=--experimental-vm-modules jest"
  },
  "jest":  {
    "preset": "ts-jest/presets/default-esm",
    "globals": {
      "ts-jest": {
        "useESM": true
      }
    },
    "moduleNameMapper": {
      "^(\\.{1,2}/.*)\\.js$": "$1"
    }
  },
  "devDependencies": {
    "@pacote/jest-either": "^5.0.0",
    "fp-ts": "^2.11.5",
    "jest": "^27.0.0-next.11",
    "ts-jest": "^27.0.0-next.12",
    "typescript": "^4.5.4"
  }
}

test.ts

import { right } from 'fp-ts/Either'
import matchers from '@pacote/jest-either/lib'

expect.extend(matchers)

test("sanity", () => {
  expect(right('ok')).toEqualRight('ok')
})

tsconfig.json

{
  "compilerOptions": {
    "moduleResolution": "node"
  }
}

Hope this helps.

goblindegook avatar Dec 21 '21 12:12 goblindegook