Lakshmikanth G.M
Lakshmikanth G.M

Reputation: 31

ng test command is not working with angular 8 and Jest

I am facing a issue when i try to run test cases with "npm run test" command with Angular 8 and Jest

I am trying to migrate from Karma/jasmine framework to Jest and also upgrading angular version to 8.

But npm run test command is throwing the below error

File not found: /src/tsconfig.spec.json (resolved as: D:\app-jest\src\tsconfig.spec.json)

at ConfigSet.resolvePath (node_modules/ts-jest/dist/config/config->set.js:712:19)

Package.json

   {
      scripts": {
          "ng": "ng",
          "start": "ng serve",
          "test": "jest"
     }

jest.config.js

 module.exports = {
    preset: 'jest-preset-angular',
    roots: ['src'],
    setupFilesAfterEnv: ["./src/setup-jest.ts"]
 };

Upvotes: 2

Views: 2798

Answers (2)

sampopes
sampopes

Reputation: 2696

This is the quick fix Move tsconfig.spec.json back to src. Update jest.config.js:

Newer version has this fixed. Refer following thread

https://github.com/briebug/jest-schematic/issues/12

Upvotes: 1

Fabricio Biron
Fabricio Biron

Reputation: 71

Here it is my suggestion:

1. Use this statement in your package.json file. Obviously, you may need to change the files' path.

  "jest": {
    "preset": "jest-preset-angular",
    "setupFilesAfterEnv": [
      "<rootDir>/src/setupJest.ts"
    ],
    "globals": {
      "ts-jest": {
        "tsConfig": "./tsconfig.spec.json",
        "diagnostics": false,
        "stringifyContentPathRegex": "\\.html$",
        "astTransformers": ["<rootDir>/node_modules/jest-preset-angular/InlineHtmlStripStylesTransformer"]
      }
    }
  }

2. I had to remove test.ts file from src folder. There was a conflict related to this jasmine script.

3. I also added the following statement in to the tsconfig.app.json

  "compilerOptions": {
    //...
    "types": [
      "jest"
    ]
  }

I hope that helps you and I recommend you to read this article

Upvotes: 0

Related Questions