"require(...).install is not a function" with Jest 23
See original GitHub issueWhen 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.
Reproduction Steps
- create a new Angular 6 app with Angular-CLI 6:
ng new test-app
cd test-app
- follow jest-preset-angular installation steps from the README or the blog post
npm install -D jest ts-jest jest-preset-angular
- add file
src/setupJest.ts
with contents [1] - add file
src/jestGlobalMocks.ts
with contents [2] - add the configuration key to
package.json
[3]
- run jest:
npx jest
Expected result:
src/app/app.component.spec.ts
is executed and have 2 passes and 1 failure (like withnpm 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.
Additional Notes
Everything works as expected when installing jest@22
, ts-jest@22
and jest-preset-angular@5.2.2
instead.
Reproduction Repo
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"
}
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:10 (1 by maintainers)
Top Results From Across the Web
Jest : TypeError: require(...) is not a function - Stack Overflow
I assume this is happening because Jest configured to work with babel-jest to handle import statements, but compiled codes are using require for ......
Read more >Configuring Jest
This function gets Jest's globalConfig object as a parameter. Note: A global setup module configured in a project (using multi-project runner) ...
Read more >Getting Started - Jest
Install Jest using your favorite package manager: ... Let's get started by writing a test for a hypothetical function that adds two numbers....
Read more >An Async Example - Jest
To enable async/await in your project, install @babel/preset-env and enable the feature in your babel.config.js file. Error handling. Errors ...
Read more >ES6 Class Mocks - Jest
The reason for that is that arrow functions are not present on the ... If you don't need to replace the implementation of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
hi @herkulano , would you please try to clean
jest
cache and rerun your tests ? I had this error but it was gone after I cleanedjest
cache@thymikee unfortunately, with
jest-preset-angular@next
(andjest@latest
,ts-jest@latest
), I now get the following error:Note that the relative reference to
../tsconfig.json
that Angular has by default insrc/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.)