jeudi 14 janvier 2021

Jest does not find paths in CI

I want to test my typescript library. It has two parts: core and client. client instance core to use its functions.

import { Core } from "@lib/core";
class Client {
  constructor() {
    new Core();
  }
}

To make these two parts independent, I use lerna as it they were separated npm packages: @lib/core and @lib/client.

Everything runs ok in local environment, but in gitlab CI this error is thrown before the client tests start:

Cannot find module '@lib/core' from 'src/lib/client.spec.ts'
      3 |
    > 4 | jest.mock('@lib/core');
        |      ^

lerna.json

{
  "packages": ["packages/core", "packages/client"],
  "version": "independent",
  "npmClient": "npm"
}

.gitlab-ci.yml

image: node:14

cache:
  paths:
    - node_modules
    - .npm
    - dist

---
unit:
  stage: test
  script:
    - npx jest --clear-cache
    - npm run test

I have tried adding path configuration in tsconfig.json, to see if jest did recognize them this way. Result: runs ok in local but fails in CI

tsconfig.json

{
  "compilerOptions": {
    "paths": {
      "@lib/core": ["dist/core"],
      "@lib/client": ["dist/client"]
    }
  }
}

jest.config.js

const { pathsToModuleNameMapper } = require("ts-jest/utils");
const { compilerOptions } = require("./tsconfig");

module.exports = {
  preset: "jest-preset-angular",
  roots: ["<rootDir>/src/"],
  testMatch: ["**/+(*.)+(spec).+(ts)"],
  setupFilesAfterEnv: ["<rootDir>/src/test-setup.ts"],
  collectCoverage: true,
  coverageReporters: ["html"],
  coverageDirectory: "coverage",
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths || {}, {
    prefix: "<rootDir>/",
  }),
  cache: false,
};

Aucun commentaire:

Enregistrer un commentaire