nrwl / nx

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

Jest26 on Node10 fails with 'ReferenceError: globalThis is not defined' with V8 coverage provider #3776

Closed Lonli-Lokli closed 4 years ago

Lonli-Lokli commented 4 years ago

Current Behavior

CodeCoverage on Node 10 fails when coverage provider is v8

Expected Behavior

It should work

Steps to Reproduce

Generate empty angular application with Nx and update jest.config.js

collectCoverage: true,
cooverageProvider: "v8"

Run npm run test

Failure Logs

ng run jestee:test FAIL apps/jestee/src/app/app.component.spec.ts ● Test suite failed to run

evalmachine.<anonymous>:1
globalThis
^

ReferenceError: globalThis is not defined

  at ../../evalmachine.<anonymous>:1:1

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

Environment

Node 10 nx : Not Found @nrwl/angular : 10.2.1 @nrwl/cli : 10.2.1 @nrwl/cypress : 10.2.1 @nrwl/eslint-plugin-nx : Not Found @nrwl/express : Not Found @nrwl/jest : 10.2.1 @nrwl/linter : Not Found @nrwl/nest : Not Found @nrwl/next : Not Found @nrwl/node : Not Found @nrwl/react : Not Found @nrwl/schematics : Not Found @nrwl/tao : 10.2.1 @nrwl/web : Not Found @nrwl/workspace : 10.2.1 typescript : 3.9.7

rbroen commented 4 years ago

I can confirm this with node 10.19.0. After running npx create-nx-workspace@latest f and installing an angular+nest project, I ran nx test f. The result is:

> ng run f:test 
 FAIL  apps/f/src/app/app.component.spec.ts
  ● Test suite failed to run

    evalmachine.<anonymous>:1
    globalThis
    ^

    ReferenceError: globalThis is not defined
Lonli-Lokli commented 4 years ago

Might be related to https://github.com/jsdom/jsdom/issues/2795#issuecomment-624460723

rbroen commented 4 years ago

I switched to node 12.18.4 and everything was fine.

Lonli-Lokli commented 4 years ago

@rbroen unfortunately for my project Node10 is the only option right now.

vsavkin commented 4 years ago

@Cammisuli do you think this is an issue with Jest? If that's the case, node 10 users can use an earlier version of Jest, right?

Lonli-Lokli commented 4 years ago

I've created sample with pure angular, jest26 and jest-preset-angular and it works on node10.

Cammisuli commented 4 years ago

From the looks of it this is definitely something in jsdom. The fix they applied doesn't work :(

@Lonli-Lokli there's probably a polyfill that's being added in the sample you have. Can you share it?

Lonli-Lokli commented 4 years ago

@Cammisuli do you need sample with issue (with nrwl) or without (pure angular)?

Cammisuli commented 4 years ago

@Lonli-Lokli the one that's working please.

Lonli-Lokli commented 4 years ago

@Cammisuli here is it https://github.com/Lonli-Lokli/jest-26

Cammisuli commented 4 years ago

Thank you! It looks like it's related to something in @nrwl/jest/plugin/resolver. I'll investigate a bit further.

Yup, it looks like typescript is adding globalThis to the global object..

Cammisuli commented 4 years ago

@Lonli-Lokli here's a solution: yarn add jest-environment-jsdom-fifteen --dev Then change your root jest.config.js to have this:

 testEnvironment: "jest-environment-jsdom-fifteen"

That should allow it to work better with Node 10. But we do really recommend that you update to the latest LTS as Node 10 will be EOL next year 😮

Lonli-Lokli commented 4 years ago

@Cammisuli I was going to use v8 coverage, introduced in jest26 with jest-envieonment-jsdom@16. So I believe your proposal will be like use Jest 26 without new enhancements.

Pure angular allows to use it, why is it broken with nrwl?

Cammisuli commented 4 years ago

The v8 coverageProvider was added with v25. It also appears that it's very experimental and even Jest says to use Node 14 to get the best results.

Note that using v8 is considered experimental. This uses V8's builtin code coverage rather than one based on Babel. It is not as well tested, and it has also improved in the last few releases of Node. Using the latest versions of node (v14 at the time of this writing) will yield better results.

Using Jest v26 will still give you the same benefits, but the test environment will be different. You can also use puppeteer as a testEnvironment, right?

As to why it doesn't work well with Nx, is because of the @nrwl/jest/plugin/resolver. We include that so that we can use the tsconfig path mappings (along with handling styles) to find nx projects in a workspace. As soon as TypeScript is imported anywhere, TypeScript will automatically include a globalThis shim. We can't do anything about that unfortunately.

Lonli-Lokli commented 4 years ago

@Cammisuli agree with you now. I think it'll be good to add some notes to readme or wiki for users on Node 10. Unfortunately our customer prefer to upgrade in last minute, it was a huge move for them to go up to 10 from Node 8.

Cammisuli commented 4 years ago

Changing the testEnvironment to "jest-environment-jsdom-fifteen" looks like the best solution for handling this situation. If anything else arises, reply to this thread, or open a new issue.

Thank you!

Lonli-Lokli commented 4 years ago

Just FYI, Jest-runner (https://marketplace.visualstudio.com/items?itemName=firsttris.vscode-jest-runner) requires this setting to be added to every project and ignores root-level jest.config.js.

Cammisuli commented 4 years ago

You should be able to add any options into the root jest.preset.js file. As that should be a preset (by default) for all jest projects within Nx.

Lonli-Lokli commented 4 years ago

@Cammisuli it does not work when this option exists in latest (> 10.3) root config, I have to add it to the project configs.

paolotozzo commented 3 years ago

I had the same problem and as I am stuck with node version 10 for this project the only solution I found was to lock jest-environment-jsdom to a version lower than 25. In the package.json just add to the resolutions as follows

"resolutions": { "jest-environment-jsdom": "24.9.0" },

I use yarn, npm probably does not support it, although you could use npm-force-resolutions

lfoliveir4 commented 3 years ago

@Lonli-Lokli here's a solution: yarn add jest-environment-jsdom-fifteen --dev Then change your root jest.config.js to have this:

 testEnvironment: "jest-environment-jsdom-fifteen"

That should allow it to work better with Node 10. But we do really recommend that you update to the latest LTS as Node 10 will be EOL next year 😮

Solved to me

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.