Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
marcelhohn opened this issue Nov 29, 2024 · 7 comments

Comments

@marcelhohn
Copy link

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
@marcelhohn marcelhohn changed the title [Bug]: Test fail with "ReferenceError: document is not defined" on Angular 19 [Bug]: Tests fail with "ReferenceError: document is not defined" on Angular 19 Nov 29, 2024
@marcelhohn marcelhohn changed the title [Bug]: Tests fail with "ReferenceError: document is not defined" on Angular 19 [Bug]: Tests fail with ReferenceError: document is not defined on Angular 19 Nov 29, 2024
@dm-gc
Copy link

dm-gc commented Nov 29, 2024

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
Copy link

Same problem

@dm-gc
Copy link

dm-gc commented Nov 29, 2024

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: dperini/nwsapi#135
Open PR: dperini/nwsapi#134

@dm-gc
Copy link

dm-gc commented Nov 29, 2024

Yep, my tests run by downgrading to 2.2.13: npm install [email protected].

@tomastrajan
Copy link

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
Copy link

dm-gc commented Nov 29, 2024

@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
Copy link

@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 🤦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants