Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conflicting types using jest next to karma, jasmine on Angular

Background

I am working on a project that originated as a angular.js project. We are currently migrating everything to Angular. This means we have legacy unit tests for Angular.js which will eventually be removed. For Angular, we have migrated all tests to Jest.

Problem

The problem arises when trying to have both Karma and Jasmine as Jest in the project. The legacy unit tests are wired up with its own tsconfig file and these work fine. Both libraries seem to be adding the same functions to the global environment, and the Jest tests somehow keep resolving to the Jasmine types. If I remove Karma and Jasmine from the project all the tests work just fine.

Question

How can I can exclude jasmine's types like expect when running jest without removing karma and jasmine from the project altogether?

I've tried

I have a seperate tsconfig.spec file for the jest tests which I have referenced in the jest.config.js file. In this I've tried adding the wanted types only. Which I thought would only load node_modules/@types/<included>. But it will still include jasmine.

  "compilerOptions": {
    "types": ["node", "jquery", "jest"],

I've also tried including them through typeRoots, but this only gives me more errors like: error TS2708: Cannot use namespace 'jest' as a value..

{
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "typeRoots": [
      "../node_modules/@types/jest"
      ...
    ],
    "baseUrl": ".",
    "paths": {
      "*": ["./*"]
    },
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "target": "es5",
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "allowJs": true
  },
  "files": ["polyfills.ts"],
  "include": ["./app/**/*.spec.ts"]
}

Somehow I do not seem able to make TypeScript ignore the node_modules/@types/jasmine. Any help or pointers would be greatly appreciated.

like image 473
Daan van Hulst Avatar asked Oct 25 '19 17:10

Daan van Hulst


1 Answers

You can use jest-without-globals to explicitly import describe, expect, it, and test, overriding the global versions of these symbols which would otherwise be resolved from Jasmine.

like image 87
Adam Avatar answered Nov 12 '22 23:11

Adam