jest.config.js 中的`moduleNameMapper` 设置不适用于 CircleCI

Sai*_*awi 13 javascript typescript circleci reactjs jestjs

我已经在打字稿中使用我的 React-app 进行了测试,使用 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} />);
  });
});
Run Code Online (Sandbox Code Playgroud)

它在我的本地环境中通过了,但在 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";                              
                                 ~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

Components/是由 的别名表达式moduleNameMapper,我认为它仅在 CircleCI 中不起作用。

jest --showConfig 选项告诉我本地和 CI 环境之间没有区别。

我的设置有问题吗?

应用程序/前端/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
};
Run Code Online (Sandbox Code Playgroud)

应用程序/前端/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__"]
}
Run Code Online (Sandbox Code Playgroud)

应用程序/前端/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\"",
  },
}
Run Code Online (Sandbox Code Playgroud)

应用程序/.circleci/.config.yml

version: 2
jobs:
  build:
    ...
    steps:
      - run:
        name: run tests for frontend
        command: npm test -- -u
        working_directory: frontend
Run Code Online (Sandbox Code Playgroud)

Jam*_* L. 14

tsconfig-paths-jest在 Jest >23 中不可用。对于当前的 Jest 26,我通过以下方式使其工作:https : //kulshekhar.github.io/ts-jest/user/config/#paths-mapping

jest.config.js

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

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

tsconfig.json “编译器选项”

 "baseUrl": "./src",
 "paths": {            
  "@models/*": [ "./models/*" ],
  "@inputs/*": [ "./inputs/*" ],
  "@tests/*": [ "./__tests__/*" ],
  "@resolvers/*": [ "./resolvers/*" ],
  "@seeds/*": [ "./seeds/*" ],
  "@index": [ "./index.ts" ],
  "@ormconfig":[ "../ormconfig.ts" ]
  },
Run Code Online (Sandbox Code Playgroud)

  • 更新链接:https://kulshekhar.github.io/ts-jest/docs/getting-started/paths-mapping (2认同)

Sai*_*awi 9

我终于找到了它的解决方案。

根据这个问题,我使用tsconfig-pathstsconfig-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__"]
}
Run Code Online (Sandbox Code Playgroud)

笑话配置.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
};
Run Code Online (Sandbox Code Playgroud)

我的测试在 CircleCI 中运行良好,但我仍然不知道为什么这些测试在这个解决方案之前在我的本地运行良好。