Adam Jenkins
Adam Jenkins

Reputation: 55643

jest and jasmine in angular project

I've effectively replaced karma/jasmine with jest in an angular project and the tests are running great....but:

I didn't remove jasmine (protractor needs it) and now I'm running into the issue that if I use spyOn instead of jest.spyOn in my spec files, it uses the jasmine spy and not the jest one (which are implemented differently). This is a gross developer experience and I'd like to just remove jasmine completely from my unit tests, but I have no idea how.

How can I set up my tsconfig.spec.json to exclude jasmine during compilation of my spec files?

I'm using jest-preset-angular which uses ts-jest.

If I haven't included enough information, please feel free to bug me for more!

This is my tsconfig.spec.json file:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "module": "commonjs",
    "target": "es5",
    "baseUrl": ""
  },
  "files": [
    "test.ts",
    "polyfills.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}

This is my tsconfig.json file:

{
  "compileOnSave": false,
  "compilerOptions": {
    "importHelpers": true,
    "outDir": "./dist/out-tsc",
    "baseUrl": "public",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types" <-- jasmine types are in here too!
    ],
    "lib": [
      "es2016",
      "dom"
    ],
    "module": "es2015"
  }
}

My jest config tells ts-jest to use my tsconfig.spec.json file like this:

  "globals": {
    "ts-jest": {
      ...
      "tsConfig": "<rootDir>/public/tsconfig.spec.json" <-- here!
    }
  }

Upvotes: 5

Views: 2627

Answers (1)

Brian Adams
Brian Adams

Reputation: 45810

Jest is based on Jasmine and includes a "light" version...

...so spyOn exists in the Jest source code.


It is deprecated and undocumented, but as you have found it still works to call it.

It is likely to be removed completely in a future version of Jest (the legacy Jasmine pieces are gradually being removed).


To help track down calls to spyOn you can use the errorOnDeprecated CLI option.

Launch Jest like this:

jest --errorOnDeprecated

...and you will see an error logged any time spyOn is called in your tests.

Upvotes: 2

Related Questions