question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"require(...).install is not a function" with Jest 23

See original GitHub issue

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.

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 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.

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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
ahnpnlcommented, Aug 6, 2018

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

1reaction
PatrickLehnerXIcommented, Jul 16, 2018

@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.)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found