nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.63k stars 2.36k forks source link

Cannot run jest with nrwl/jest executor #11484

Closed winston0410 closed 1 year ago

winston0410 commented 2 years ago

Current Behavior

I am getting to following error when I run the test. I have confirmed that I have all the dependency installed.

 >  NX   Jest: Failed to parse the TypeScript config file /Users/xxx/Desktop/xxx/apps/prescription/jest.config.ts

     Error: Jest: 'ts-node' is required for the TypeScript configuration files. Make sure it is installed
   Error: Cannot find module 'tsconfig-paths/register'
   Require stack:
   - /Users/xxx/Desktop/xxx/node_modules/ts-node/dist/util.js
   - /Users/xxx/Desktop/xxx/node_modules/ts-node/dist/index.js
   - /Users/xxx/Desktop/xxx/node_modules/jest-config/build/readConfigFileAndSetRootDir.js
   - /Users/xxx/Desktop/xxx/node_modules/jest-config/build/index.js
   - /Users/xxx/Desktop/xxx/node_modules/@nrwl/jest/src/executors/jest/jest.impl.js
   - /Users/xxx/Desktop/xxx/node_modules/nx/src/config/workspaces.js
   - /Users/xxx/Desktop/xxx/node_modules/nx/src/command-line/run.js
   - /Users/xxx/Desktop/xxx/node_modules/nx/bin/run-executor.js
   Pass --verbose to see the stacktrace.

Expected Behavior

No error

Steps to Reproduce

I can reproduce this with nrwl default quickstart, Nx (14.5.4) at the time of writing

npx create-nx-workspace happynrwl --preset=ts
cd ./happynrwl
nx generate @nrwl/js:library --name=hello-tsc --buildable
npx nx run happynrwl:test

This issue may not be prioritized if details are not provided to help us reproduce the issue.

Failure Logs

Environment

NX Report complete - copy this into the issue template

Node : 16.15.1 OS : darwin arm64 npm : 8.11.0

nx : 14.5.2 @nrwl/angular : Not Found @nrwl/cypress : 14.5.2 @nrwl/detox : Not Found @nrwl/devkit : 14.5.2 @nrwl/eslint-plugin-nx : 14.5.2 @nrwl/express : Not Found @nrwl/jest : 14.5.2 @nrwl/js : 14.5.2 @nrwl/linter : 14.5.2 @nrwl/nest : Not Found @nrwl/next : Not Found @nrwl/node : 14.5.2 @nrwl/nx-cloud : Not Found @nrwl/nx-plugin : Not Found @nrwl/react : 14.5.2 @nrwl/react-native : Not Found @nrwl/schematics : Not Found @nrwl/storybook : 14.5.2 @nrwl/web : 14.5.2 @nrwl/workspace : 14.5.2 typescript : 4.6.4

Community plugins: @nxext/react: 14.0.4 @nxext/sveltekit: 14.0.0-next.02 @nxext/vite: 14.0.3

AgentEnder commented 2 years ago

Can you check if ts-node is installed?

If it is, can you run npm ls ts-node and paste it's outputs here?

winston0410 commented 2 years ago

Yes I can sure I have installed ts-node. This is the output of npm ls ts-node

happynrwl@0.0.0 /Users/xxx/Desktop/happynrwl
├─┬ @nrwl/jest@14.5.4
│ └─┬ jest-config@27.5.1
│   └── ts-node@10.8.2 deduped
└── ts-node@10.8.2
AgentEnder commented 2 years ago

Can you try to reproduce this and share the repository with us? Thanks

winston0410 commented 2 years ago

Sure, this is the repo https://github.com/winston0410/happynrwl

AgentEnder commented 2 years ago

Can you try installing tsconfig-paths?

@barbados-clemens do we need to install this package? It may need to be added as a peer deps to @nrwl/jest

winston0410 commented 2 years ago

@AgentEnder Sure I have just done that and pushed to the repo, same result. One information even though I am getting this error in local, the test runs fine in CI(CircleCI), and I have no idea why

https://github.com/winston0410/happynrwl/commit/403764fbba7dac8b2eff5ce94ce55875f14478aa

barbados-clemens commented 2 years ago

Just thinking aloud right now, but do you have ts-node globally installed? Maybe jest is trying to use that instead of the local version?

Can you make sure no global ts-node version is installed?

winston0410 commented 2 years ago

Yes I don't have ts-node globally:

base ❯ npm list --location=global                       
/opt/homebrew/lib
├── @ziglang/cli@0.0.9
├── npm@8.12.1
└── yarn@1.22.17

Are you able to run that reproduction repository?

barbados-clemens commented 2 years ago

@winston0410 yes it works for me just fine. this looks to be environmental related. can you make sure your NODE_ENV is unset? jest can kind of get cranky when it's set to something else, as internally it will be set as NODE_ENV=test

npx jest --clear-cache && npx nx test hello-tsc --skip-nx-cache
Cleared /private/var/folders/ws/_4mjt9x975g1z4r61kwm6c8r0000gn/T/jest_dx

> nx run hello-tsc:test

 PASS   hello-tsc  packages/hello-tsc/src/lib/hello-tsc.spec.ts
  helloTsc
    ✓ should work (1 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.56 s
Ran all test suites.

 ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 >  NX   Successfully ran target test for project hello-tsc (1s)
winston0410 commented 2 years ago

Just checked, NODE_ENV is unset

base ❯ echo $NODE_ENV      

And I got that error even when I run npx jest --clear-cache

base ❯ npx jest --clear-cache                           
Error: Jest: Failed to parse the TypeScript config file /Users/xxx/Desktop/xxx/jest.config.ts
  Error: Cannot find module 'tsconfig-paths/register'

But when I invoke jest command, like cd libs/hello-tsc && npx jest it is able to run. So I am not 100% sure if I can isolate the error in jest. Really confusing at this point.

base ❯ cd packages/hello-tsc && npx jest
 PASS   hello-tsc  src/lib/hello-tsc.spec.ts
  helloTsc
    ✓ should work (1 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.538 s, estimated 1 s
Ran all test suites.
barbados-clemens commented 2 years ago

you wouldn't happen to have jest or nx globally installed?

can you run jest from your node_modules/.bin and then nx?

ie.

node_modules/.bin/jest --clear-cache && node_modules/.bin/jest
Cleared /private/var/folders/ws/_4mjt9x975g1z4r61kwm6c8r0000gn/T/jest_dx
 PASS   hello-tsc  packages/hello-tsc/src/lib/hello-tsc.spec.ts
  helloTsc
    ✓ should work (1 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.569 s
Ran all test suites.
node_modules/.bin/jest --clear-cache && node_modules/.bin/nx test hello-tsc --skip-nx-cache
Cleared /private/var/folders/ws/_4mjt9x975g1z4r61kwm6c8r0000gn/T/jest_dx

> nx run hello-tsc:test

 PASS   hello-tsc  packages/hello-tsc/src/lib/hello-tsc.spec.ts
  helloTsc
    ✓ should work (1 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.552 s
Ran all test suites.

 >  NX   Successfully ran target test for project hello-tsc (1s)
barbados-clemens commented 2 years ago

also, just to be thorough you have tried nuking the node_modules and installing fresh?

winston0410 commented 2 years ago

Nope I don't have them globally installed:

base ❯ npm list --location=global                                    
/opt/homebrew/lib
├── @ziglang/cli@0.0.9
├── npm@8.12.1
└── yarn@1.22.17

And the result of running it from node_modules

base ❯ $(npm bin)/jest --clear-cache && $(npm bin)/nx test hello-tsc --skip-nx-cache    
Error: Jest: Failed to parse the TypeScript config file /Users/hugosum/Desktop/happynrwl/jest.config.ts
  Error: Jest: 'ts-node' is required for the TypeScript configuration files. Make sure it is installed
Error: Cannot find module 'tsconfig-paths/register'

Not fun!!!

also, just to be thorough you have tried nuking the node_modules and installing fresh?

Yup I have done that too, and I havn't installed anything special recently in my system, so I honestly don't have any idea what went wrong.

barbados-clemens commented 2 years ago

this is very strange. right now my only thoughts are the internal nx register system is causing something wonky to happen on your system because something is getting set/overridden that shouldn't be.

the fact it works in a project folder makes me think some other env var is set.

can you double check you don't have env vars that point to other tsconfig files, like TS_NODE_PROJECT or TS_NODE_BASEURL

what is odd is I can remove ts-node from the root package.json and it still works bc it's a dep of @nrwl/jest so unsure why you'd be seeing this issue at all.

can you also manually verify that you can run the ts-node RPEL and import jest?

npx ts-node
> require('jest')
{
  SearchSource: [Getter],
  TestWatcher: [Getter],
  createTestScheduler: [Getter],
  getVersion: [Getter],
  run: [Getter],
  runCLI: [Getter]
}
>
barbados-clemens commented 2 years ago

another thing to try is installing swc-node/register and nx will use that internally to register ts paths instead of tsconfig-paths

I seriously doubt that would be the issue since I think this would end up being more widespread, but something else to try to eliminate.

barbados-clemens commented 2 years ago

any luck @winston0410

gRoussac commented 2 years ago

Hi, thanks all for helping debug this.

Not sure if this helps but I did reproduce this error on a fresh install. I do have jest 28.1.3 installed globally thought for a VsCode jest runner plugin and this was apparently the issue, it was fetching the global one instead of the node_modules one.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. If we missed this issue please reply to keep it active. Thanks for being a part of the Nx community! 🙏

github-actions[bot] commented 1 year ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.