javascript - הגדרות `moduleNameMapper` ב- jest.config.js לא עובדות ב-CircleCI


ניסיתי עם אפליקציית React שלי ב-Typescript, בעזרת ts-jest כמו למטה.

import * as React from "react";
import * as renderer from "react-test-renderer";

import { ChartTitle } from "Components/chart_title";

describe("Component: ChartTitle", () => {
  it("will be rendered with no error", () => {
    const chartTitle = "My Chart 1";
    renderer.create(<ChartTitle title={chartTitle} />);
  });
});

והוא עבר בסביבה המקומית שלי אבל נכשל ב-CircleCI.

 FAIL  __tests__/components/chart_title.tsx
  ● Test suite failed to run

    TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
    __tests__/components/chart_title.tsx:4:28 - error TS2307: Cannot find module 'Components/chart_title'.

    4 import { ChartTitle } from "Components/chart_title";                              
                                 ~~~~~~~~~~~~~~~~~~~~~~~~

ה-Components/ הזה הוא ביטוי כינוי על ידי moduleNameMapper, ואני חושב שהוא לא עובד רק ב-CircleCI.

האופציה jest --showConfig אומרת לי שאין הבדל בין סביבת הפיתוח המקומית לסביבת ה-CI.

האם יש איזושהי טעות בהגדרות שלי?

app/frontend/jest.config.js

module.exports = {
  globals: {
    "ts-jest": {
      tsConfig: "tsconfig.json",
      diagnostics: true
    },
    NODE_ENV: "test"
  },
  moduleNameMapper: {
    "^Components/(.+)$": "<rootDir>/src/components/$1"
  },
  moduleDirectories: ["node_modules", 'src'],
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json"],
  transform: {
    "^.+\\.tsx?$": "ts-jest"
  },
  verbose: true
};

app/frontend/tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "src",
    "outDir": "dist",
    "allowJs": true,
    "checkJs": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "noImplicitAny": true,
    "target": "esnext",
    "module": "commonjs",
    "lib": ["es6", "dom"],
    "jsx": "react",
    "strict": false,
    "removeComments": true,
    "types": ["jest"]
  },
  "typeRoots": ["node_modules/@types"],
  "paths": {
    "Components/*": ["src/components/*"]
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "__tests__"]
}

app/frontend/package.json

{
  "scripts": {
    "build": "webpack --mode development --watch",
    "build-production": "node_modules/.bin/webpack --mode production",
    "test": "jest",
    "lint": "npx eslint src/**/* __tests__/**/* --ext \".ts, .tsx\"",
  },
}

app/.circleci/.config.yml

version: 2
jobs:
  build:
    ...
    steps:
      - run:
        name: run tests for frontend
        command: npm test -- -u
        working_directory: frontend

תשובה #1

tsconfig-paths-jest אינו שמיש ב-Jest >23. עבור Jest 26 הנוכחי הצלחתי לעבוד דרך: https://kulshekhar.github.io/ts-jest/docs/getting-started/paths-mapping/

jest.config.js
const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig');

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/src/' } )
};

tsconfig.json "אפשרויותקומפילציה"

 "baseUrl": "./src",
 "paths": {            
  "@models/*": [ "./models/*" ],
  "@inputs/*": [ "./inputs/*" ],
  "@tests/*": [ "./__tests__/*" ],
  "@resolvers/*": [ "./resolvers/*" ],
  "@seeds/*": [ "./seeds/*" ],
  "@index": [ "./index.ts" ],
  "@ormconfig":[ "../ormconfig.ts" ]
  },

תשובה #2

סוף סוף מצאתי את הפתרון שלו.

לפי הנושא הזה, אני משתמש ב-tsconfig-paths וב-tsconfig-paths-jest.

אז קבצי ההגדרות שלי השתנו כפי שמוצג למטה.

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "dist/",
    "allowJs": true,
    "checkJs": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "noImplicitAny": true,
    "target": "es6",
    "module": "commonjs",
    "lib": ["es5", "es6", "dom"],
    "jsx": "react",
    "strict": false,
    "removeComments": true,
    "types": ["node", "jest"],
    "paths": {
      "Components/*": ["src/components/*"]
    }
  },
  "typeRoots": ["node_modules/@types"],
  "include": ["src/**/*"],
  "exclude": ["node_modules", "__tests__"]
}

jest.config.js

/* eslint-disable import/order */
/* eslint-disable @typescript-eslint/no-var-requires */
const tsconfig = require("./tsconfig.json");
const moduleNameMapper = require("tsconfig-paths-jest")(tsconfig);

module.exports = {
  globals: {
    "ts-jest": {
      tsConfig: "tsconfig.json",
      diagnostics: true
    },
    NODE_ENV: "test"
  },
  setupFilesAfterEnv: [`${__dirname}/src/setupTests.ts`],
  moduleDirectories: ["node_modules", 'src'],
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json"],
  transform: {
    "^.+\\.tsx?$": "ts-jest"
  },
  verbose: true,
  moduleNameMapper
};

והמבחנים שלי עבדו טוב ב-CircleCI, אבל עדיין אין לי שום רעיון למה המבחנים האלה עבדו אצלי מקומית לפני הפתרון הזה.