Cannot find module when using jest in nx
Asked Answered
W

1

6

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.

Wore answered 10/4, 2021 at 17:38 Comment(0)
I
1

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',
  ...
Iams answered 20/12, 2021 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.