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

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

Closed
PatrickLehnerXI opened this issue Jul 16, 2018 · 10 comments
Closed

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

PatrickLehnerXI opened this issue Jul 16, 2018 · 10 comments

Comments

@PatrickLehnerXI
Copy link

PatrickLehnerXI commented Jul 16, 2018

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"
  }
}
@thymikee
Copy link
Owner

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

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jul 16, 2018

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 :)

@PatrickLehnerXI
Copy link
Author

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

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jul 20, 2018

@PatrickLehnerXI I think your issue is related to this issue

@ahnpnl
Copy link
Collaborator

ahnpnl commented Aug 3, 2018

hi @PatrickLehnerXI , would you please try to verify if the issue still exists with v6.0.0 just release ?

@herkulano
Copy link

@ahnpnl I have the same error but only started to have it after upgrading to v6

@ahnpnl
Copy link
Collaborator

ahnpnl commented 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

@herkulano
Copy link

@ahnpnl works! thank you!

@thymikee thymikee closed this as completed Aug 6, 2018
@AnthonyNahas
Copy link

@ahnpnl that worked for me too 👍

@thearchitgarg
Copy link

jest --clearCache solved the issue for me too. Thanks.

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

No branches or pull requests

6 participants