Open marcelhohn opened 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"
]
}
Same problem
Guys and gals,
as indicated by error stack it is probably caused by latest version (2.2.14) of nwsapi:
See open issue at their repo: https://github.com/dperini/nwsapi/issues/135 Open PR: https://github.com/dperini/nwsapi/pull/134
Yep, my tests run by downgrading to 2.2.13: npm install nwsapi@2.2.13
.
Thank you so much, I was losing my mind😅
Is this somehting which will be fixed in the next release of jest-preset-angular
?
@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.
@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
so it's fully on the jest side 🤦
Version
14.4.1
Steps to reproduce
ts-node
to use a TS file for Jest confignpm run test
Expected behavior
The tests pass
Actual behavior
The tests fail, with the following error:
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