Search code examples
typescriptjasmine

"jasmine is not defined"


I recently updated my Jest version from 24.x to 27.0.0 and this broke one of the tests I had:

I had my test framework set up as this:

describe('PersonDescriber', () => {
  let service: PersonDescriber;

  const mock = {
    getAutocompleteResults: jasmine.createSpy().and.returnValue({features: [{relevance: 1, meta: {}},{relevance: 0}]}),
  };
}

As soon as I updated versions it is now erroring out for jasmine saying:

ReferenceError: jasmine is not defined

Any idea what is going on? Do I manually need to import jasmine? When I was on the old version of Jest, I did not have an import for jasmine in this file.

jest:

 "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverage": true,
    "coverageDirectory": "../coverage",
    "collectCoverageFrom": [
      "**/*.ts",
      "!**/*.dto.ts",
      "!**/*.model.ts",
      "!**/*.module.ts",
      "!**/main.ts",
      "!**/configuration.ts"
    ],
    "moduleNameMapper": {
      "@config/(.*)": "<rootDir>/config/$1",
      "@personDescriber/(.*)": "<rootDir>/personDescriber/$1",
      "@health/(.*)": "<rootDir>/health/$1",
      "@shared/(.*)": "<rootDir>/shared/$1"
    },
    "coverageThreshold": {
      "global": {
        "branches": 20,
        "functions": 20,
        "lines": 20,
        "statements": 20
      }
    },
    "testEnvironment": "node"
  },

Solution

  • The default test runner was changed from jasmine2 in version 24.x to jest-circus/runner in version 27.x. You can either manually set the runner back to jasmine2 or rewrite your tests to jest-circus style. The configuration key is testRunner. You can find more information in

    Jest docs 24.x

    Jest docs 27.x

    jest-circus