nrwl / nx

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

Unable to set VirtualConsole in jest via executing with @nx/jest #21261

Closed cluxig closed 1 month ago

cluxig commented 5 months ago

Current Behavior

Setting up the VirtualConsole (to track and omit CSS parsing errors) leads to runtime error in case of execution via @nx/jest executor: TypeError: virtualConsole must be an instance of VirtualConsole at transformOptions (../../../node_modules/jsdom/lib/api.js:260:11)

Running it with plain CLI mode: node node_modules/jest/bin/jest.js ... it works.

Expected Behavior

Tests should run with this configuration, where as the VirtualConsole is replaced accordingly.

GitHub Repo

No response

Steps to Reproduce

I just configed it like this in global jest.preset.js

const nxPreset = require('@nx/jest/preset').default;
const jsdom = require('jsdom');

const virtualConsole = new jsdom.VirtualConsole();
virtualConsole.sendTo(console, { omitJSDOMErrors: true });
virtualConsole.on('jsdomError', error => {
  if (error.type === 'css parsing') {
    console.warn('Ignoring CSS error');
  } else {
    console.error(error);
  }
});

module.exports = {
  ...nxPreset,
  collectCoverage: true,
  collectCoverageFrom: ['src/lib/**/*.ts', 'src/app/**/*.ts'],
  coverageReporters: ['html-spa', 'lcov', 'text-summary'],
  testEnvironmentOptions: {
    virtualConsole: virtualConsole,
  },
};

It does not make a difference, if i put the configuration into the project's test-setup.ts.

Nx Report

npx nx report
>  NX  Falling back to ts-node for local typescript execution. This may be a little slower.
  - To fix this, ensure @swc-node/register and @swc/core have been installed

 >  NX   Report complete - copy this into the issue template

   Node   : 18.16.0
   OS     : win32-x64
   npm    : 9.5.1

   nx                 : 16.10.0
   @nx/js             : 16.10.0
   @nx/jest           : 16.10.0
   @nx/linter         : 16.10.0
   @nx/workspace      : 16.10.0
   @nx/angular        : 16.10.0
   @nx/cypress        : 16.10.0
   @nx/devkit         : 16.10.0
   @nx/esbuild        : 16.10.0
   @nx/eslint-plugin  : 16.10.0
   @nx/storybook      : 16.10.0
   @nrwl/tao          : 16.10.0
   @nx/web            : 16.10.0
   @nx/webpack        : 16.10.0
   typescript         : 5.1.6
   ---------------------------------------
   Community plugins:
   @ngrx/effects        : 16.3.0
   @ngrx/entity         : 16.3.0
   @ngrx/schematics     : 12.5.1
   @ngrx/store          : 16.3.0
   @ngrx/store-devtools : 16.3.0
   eslint-plugin-ngrx   : 2.1.4
   ng-mocks             : 14.12.1

Failure Logs

No response

Package Manager Version

No response

Operating System

Additional Information

Tried different things and ended up with package.json with overrides to force the same jsdom lib all over the project (because of non-peer dependencies of jest-environment-jsdom). But still no luck.

  "overrides": {
    "jsdom": "23.2.0"
  },
leosvelperez commented 2 months ago

Thanks for reporting this!

Could you please provide a repo where the issue can be reproduced?

I only manage to reproduce the issue when there are multiple versions of jsdom resolved in the workspace. If I override the jsdom version like you did:

  "overrides": {
    "jsdom": "23.2.0"
  },

The issue goes away for me. Also, if I don't override the jsdom version, I get the same runtime error running the tests with the @nx/jest:jest executor or with the Jest CLI directly:

npx nx test app1

> nx run app1:test

 FAIL   app1  apps/app1/src/app/app.component.spec.ts
  ● Test suite failed to run

    TypeError: virtualConsole must be an instance of VirtualConsole

      at transformOptions (../../node_modules/jest-environment-jsdom/node_modules/jsdom/lib/api.js:259:11)

...
node_modules/.bin/jest -c apps/app1/jest.config.ts
 FAIL   app1  apps/app1/src/app/app.component.spec.ts
  ● Test suite failed to run

    TypeError: virtualConsole must be an instance of VirtualConsole

      at transformOptions (../../node_modules/jest-environment-jsdom/node_modules/jsdom/lib/api.js:259:11)

...

For reference, the steps I took to reproduce it (based on the provided info) are:

With that, I can successfully run the tests using the test target with the @nx/jest:jest executor. There's no runtime error like the one you mentioned.

leosvelperez commented 2 months ago

Also, note that trying to override the VirtualConsole like you're doing won't work with Jest. The jsdom environment doesn't use it and will always create a new instance of VirtualConsole: https://github.com/jestjs/jest/blob/v29.0.0/packages/jest-environment-jsdom/src/index.ts#L41.

See https://github.com/nrwl/nx/issues/21846#issuecomment-2104580279 for reference on how to stub out console.* methods.

github-actions[bot] commented 1 month ago

This issue has been automatically marked as stale because no reproduction was provided within 7 days. Please help us help you. Providing a repository exhibiting the issue helps us diagnose and fix the issue. Any time that we spend reproducing this issue is time taken away from addressing this issue and other issues. This issue will be closed in 21 days if a reproduction is not provided. If a reproduction has been provided, please reply to keep it active. Thanks for being a part of the Nx community! 🙏

github-actions[bot] commented 6 days 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.