jestjs - 无法使用 ts-jest 转换 node_modules 中的文件夹

标签 jestjs ts-jest

我正在用 typescript 编写测试,我在 node_modules/@my-modules 中有私有(private)依赖项文件夹。我正在使用 ts-jest编译器和 jest 仍然提示 node_modules 文件夹中的 esnext 模块。

包版本:

"@types/jest": "^23.3.12",
"jest": "^23.6.0",
"ts-jest": "^23.10.5",

tsconfig.json:
{
  "compilerOptions": {
    "baseUrl": "./src",
    "outDir": "build/dist",
    "module": "commonjs",
    "target": "es5",
    "lib": ["es5", "es6", "dom", "esnext"],
    "sourceMap": true,
    "allowJs": true,
    "jsx": "react",
    "moduleResolution": "node",
    "rootDir": "src",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": false,
    "strict": false,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  }
}

jest.config.js:
module.exports = {
  globals: {
    "ts-jest": {
      diagnostics: true,
      tsConfig: "<rootDir>/tsconfig.json",
    },
  },
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json"],
  preset: "ts-jest",
  roots: ["<rootDir>/src/"],
  setupTestFrameworkScriptFile: "<rootDir>/src/setupEnzyme.ts",
  snapshotSerializers: ["enzyme-to-json/serializer"],
  testEnvironment: "jest-environment-jsdom",
  transformIgnorePatterns: [
    "node_modules/(?!(@my-modules)/)",
  ]
};

运行 jest 时的确切错误:
 Details:

    /sites/frontend/node_modules/@my-modules/ui-components/.dist/src/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { default as Calendar } from './components/calendar';
                                                                                             ^^^^^^

    SyntaxError: Unexpected token export


@my-modules 中转译文件的正确方法是什么?开 Jest ?

最佳答案

现在对我有用的配置

包版本:

"@types/jest": "^24.0.17",
"jest": "^24.8.0",
"ts-jest": "^23.10.5",

tsconfig.json:
{
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "build/dist",
    "lib": ["es5", "es6", "dom", "esnext"],
    "module": "esnext",
    "moduleResolution": "node",
    "target": "es5",
    "sourceMap": true,
    "allowJs": true,
    "jsx": "react",
    "rootDir": "src",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  },
  "exclude": [
    "node_modules",
    "build",
    "dist"
  ]
}


jest.config.js:
module.exports = {
  globals: {
    "ts-jest": {
      diagnostics: true,
      tsConfig: "<rootDir>/tsconfig.json",
    },
  },
  moduleDirectories: [
    "node_modules",
    "utils",
    __dirname,
  ],
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json"],
  moduleNameMapper: {
    "\\.(css|less)$": "identity-obj-proxy",
    "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
      "<rootDir>/config/jest/fileMock.js",
  },
  preset: "ts-jest",
  roots: ["<rootDir>/src/"],
  setupFilesAfterEnv: ["<rootDir>/config/jest/setupJest.ts"],
  snapshotSerializers: ["enzyme-to-json/serializer"],
  testEnvironment: "jest-environment-jsdom",
  transform: {
    "^.+\\.jsx?$": "babel-jest",
    "^.+\\.tsx?$": "ts-jest",
  },
  transformIgnorePatterns: [
    "node_modules/(?!(@my-modules)/)",
  ],
};


配置/开 Jest/fileMock.js:
module.exports = 'test-file-stub';

关于jestjs - 无法使用 ts-jest 转换 node_modules 中的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54182173/

相关文章:

javascript - 在 Jest 中排序的单元测试用例

javascript - VS Code TypeScript Jest 模块映射

cookies - 使用 Jest 测试安全 cookie 值

mocking - jest.mock 与命名导出如何监视

node.js - 如何在 Jest 的自定义测试环境文件中使用 TypeScript?

Angular 14 + Jest - typescript_1.default.canHaveDecorators 不是函数

javascript - 如何为以下代码片段编写单元测试用例

javascript - Jest : mocking console. 错误 - 测试失败

angular - 将 Angular Testing fakeAsync 与 jest it.each 一起使用

typescript - 如何强输入 Jest 模拟