When adding Jest 23, ts-jest 23 and jest-preset-angular 5.2.3 to an Angular 6 app, running tests results in the execution error "TypeError: require(...).install is not a function" for each test file.
The same works with Jest 22, ts-jest 22 and jest-preset-angular 5.2.3.
ng new test-appcd test-appnpm install -D jest ts-jest jest-preset-angularsrc/setupJest.ts with contents [1] src/jestGlobalMocks.ts with contents [2]package.json [3]npx jestExpected result:
src/app/app.component.spec.ts is executed and have 2 passes and 1 failure (like with npm run test)Actual result:
src/app/app.component.spec.ts fails to start with the following error: FAIL src/app/app.component.spec.ts
● Test suite failed to run
TypeError: require(...).install is not a function
> 1 | import 'jest-preset-angular';
| ^
2 | import './jestGlobalMocks'; // browser mocks globally available for every test
3 |
at Object.<anonymous> (src/setupJest.ts:1:60)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 3.746s
Ran all test suites.
Everything works as expected when installing jest@22, ts-jest@22 and [email protected] instead.
https://github.com/PatrickLehnerXI/jest-preset-angular-repro-issue171
[1] src/setupJest.ts:
import 'jest-preset-angular';
import './jestGlobalMocks'; // browser mocks globally available for every test
[2] src/jestGlobalMocks.ts:
const mock = () => {
let storage = {};
return {
getItem: key => key in storage ? storage[key] : null,
setItem: (key, value) => storage[key] = value || '',
removeItem: key => delete storage[key],
clear: () => storage = {},
};
};
Object.defineProperty(window, 'localStorage', {value: mock()});
Object.defineProperty(window, 'sessionStorage', {value: mock()});
Object.defineProperty(window, 'getComputedStyle', {
value: () => ['-webkit-appearance']
});
[3] configuration key in package.json:
{
"jest": {
"preset": "jest-preset-angular",
"setupTestFrameworkScriptFile": "<rootDir>/src/setupJest.ts"
}
}
Have you tried the "next" tag? That's one thing. The second is I recall this issue, can you search better on this issue tracker or Google? I'm on a mobile right now, so it's a hard task with Github UI :D
Hi @PatrickLehnerXI , your issue is caused by ts-jest, please check this issue. There was quite a big discussion around it. I think you can find your answer there :)
@thymikee unfortunately, with jest-preset-angular@next (and jest@latest, ts-jest@latest), I now get the following error:
FAIL src/app/app.component.spec.ts
● Test suite failed to run
Some errors occurred while attempting to read from <containing path>\jest-preset-angular-repro-issue171\src\tsconfig.spec.json: error TS5058: The specified path does not exist: '<containing path>/tsconfig.json'.
Error: Some errors occurred while attempting to read from <containing path>\jest-preset-angular-repro-issue171\src\tsconfig.spec.json: error TS5058: The specified path does not exist: '<containing path>/tsconfig.json'.
at readCompilerOptions (node_modules/jest-preset-angular/node_modules/ts-jest/src/utils.ts:42:11)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 3.136s
Ran all test suites.
Note that the relative reference to ../tsconfig.json that Angular has by default in src/tsconfig.spec.json is incorrectly resolved to ./../tsconfig.json instead of ./src/../tsconfig.json.
@ahnpnl thanks for the hint, but unfortunately, I can't see anything actionable in that ts-jest issue, nor the linked issue in the jest repo. The original poster of kulshekhar/ts-jest#603 finally said that their problem went away but changing the version of one of their other dependencies.
TBH this really doesn't speak to the "Zero configuration testing platform" that Jest advertises :(
(Unfortunately, since this is for a work project, and we were going to try if Jest is viable for testing Angular apps, I can't spend too much more time on this.)
@PatrickLehnerXI I think your issue is related to this issue
hi @PatrickLehnerXI , would you please try to verify if the issue still exists with v6.0.0 just release ?
@ahnpnl I have the same error but only started to have it after upgrading to v6
hi @herkulano , would you please try to clean jest cache and rerun your tests ? I had this error but it was gone after I cleaned jest cache
@ahnpnl works! thank you!
@ahnpnl that worked for me too 👍
jest --clearCache solved the issue for me too. Thanks.
Most helpful comment
hi @herkulano , would you please try to clean
jestcache and rerun your tests ? I had this error but it was gone after I cleanedjestcache