Cannot find module when using jest in nx

3.5k Views Asked by At

I have an nx workspace that I am trying to move to jest. At first I got an error about PointerEvent not being defined. the proposed workaround for that was to do an Object.defineProperty(window, 'PointerEvent'... My workspace has over 200 libraries so it was not realistic to add that in every single test-setup file so I created an npm package that defines it. I import that in test-setup just like you would import jest-preset-angular. it worked in the first 15 libraries I converted but the latest one throws this error when running tests.

enter image description here

I have compared this lib to every other one I can think of and there is no difference. I am completely out of ideas. To say I am frustrated with this endeavor would be an understatement. Hoping somebody out there has a large nx repo and has dealt with this. It seems every answer I get assumes I chose nx to only have one app and one or two libs. I cannot provide a repo because this error is so random I have no idea how to reproduce it. je-test-helper is in node_modules and as I stated every other identical usage of this works.

1

There are 1 best solutions below

0
On

Having same issue in very similar environment Tried everything around imports and moduleDirectories, root... in jest.config.js

No luck

At end of the day, it looks like jest-runner-groups package problem

Why? Because I am trying to run only one test via group runner, BUT when I try to run all tests (without using runner) or one particular test from IDE (jest runner extension in VS) it will PASS

Therefore I think solution may be running tests differently.(? i dunno)

in test.spec.ts file I use following annotation:

/**
 * @group runthis
 */
import {...

how i run it in console:

npm run test:unit -- -group=runthis

package.json

...
scripts:
 "test:unit": "jest",
...
devDepencies:
 "jest-runner-groups": "^2.1.0",

jest.preset.js

const nxPreset = require('@nrwl/jest/preset');
module.exports = {
  runner: 'groups',
  ...