thymikee / jest-preset-angular

Jest configuration preset for Angular projects.
https://thymikee.github.io/jest-preset-angular/
MIT License
889 stars 308 forks source link

[Bug]: Tests fail with `ReferenceError: document is not defined` on Angular 19 #2883

Open marcelhohn opened 5 days ago

marcelhohn commented 5 days ago

Version

14.4.1

Steps to reproduce

  1. Create a new Angular 19 project via the Angular CLI
  2. Uninstall Jasmine/Karma dependencies and follow instructions to use Jest for testing
  3. Install ts-node to use a TS file for Jest config
  4. Run npm run test

Expected behavior

The tests pass

Actual behavior

The tests fail, with the following error:

  ● AppComponent › should render title

    The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string.
    Consider using the "jsdom" test environment.

    ReferenceError: document is not defined

Additional context

A reproduction example is here. Note that I created it by upgrading from Angular v18 to v19, but the setup and bug remain the same as in a newly created Angular v19 app.

Environment

System:
    OS: Windows 10 10.0.19045
    CPU: (16) x64 AMD Ryzen 7 PRO 5850U with Radeon Graphics     
  Binaries:
    Node: 20.11.1 - ~\AppData\Local\Programs\nodejs\node.EXE
    npm: 10.2.4 - ~\AppData\Local\Programs\nodejs\npm.CMD
  npmPackages:
    jest: ^29.7.0 => 29.7.0
dm-gc commented 5 days ago

I also have this issue on Angular 17 (it has been driving me crazy for the past four hours or so)!

Angular version: 17.3.3 jest: 29.7.0 jest-preset-angular: 14.4.1

The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string.
Consider using the "jsdom" test environment.

ReferenceError: document is not defined

  at node_modules/nwsapi/src/nwsapi.js:215:21
  at Factory (node_modules/nwsapi/src/nwsapi.js:245:6)
  at initNwsapi (node_modules/jsdom/lib/jsdom/living/helpers/selectors.js:10:10)
  at exports.addNwsapi (node_modules/jsdom/lib/jsdom/living/helpers/selectors.js:38:24)

setup-jest.ts:

import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';

setupZoneTestEnv();

jest.config.ts:

import type { Config } from 'jest';

const jestConfig: Config = {
    preset: 'jest-preset-angular',
    setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],

};

export default jestConfig;

Also fails with following jest.config.ts:

import presets from 'jest-preset-angular/presets';
import type { Config } from 'jest';

const presetConfig = presets.createCjsPreset({
  //...options
});

const jestConfig: Config = {
  ...presetConfig,
  setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
};

export default jestConfig;

tsconfig.spec.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/spec",
    "types": [
      "jest"
    ],
  },
  "include": [
    "src/**/*.spec.ts",
    "src/**/*.d.ts"
  ]
}
tomastrajan commented 5 days ago

Same problem

dm-gc commented 5 days ago

Guys and gals,

as indicated by error stack it is probably caused by latest version (2.2.14) of nwsapi:

bild

See open issue at their repo: https://github.com/dperini/nwsapi/issues/135 Open PR: https://github.com/dperini/nwsapi/pull/134

dm-gc commented 5 days ago

Yep, my tests run by downgrading to 2.2.13: npm install nwsapi@2.2.13.

tomastrajan commented 5 days ago

Thank you so much, I was losing my mind😅

Is this somehting which will be fixed in the next release of jest-preset-angular ?

dm-gc commented 5 days ago

@tomastrajan nwsapi have an open PR to address issue (see above). Not entirely sure jest-preset-angular can do without it, I leave that question to the devs.

Anyhow, it is issues like these that kind of prove the whole entangledness/brittleness of the npm ecosystem. An undefined variable crashes a ton of seemingly or even totally unrelated applications.

tomastrajan commented 5 days ago

@dm-gc right, yeah it's unfortunate.

Hmm, I was more thinking in a direction that the jest-preset-angular would fix the version of it's own transitive dep, the nwsapi so such thing could be prevented because it would need to be upgraded manually by the jest-preset-angular maintainers in the future?

but actually it's even worse lol

image

so it's fully on the jest side 🤦