【问题标题】:can't transform a folder inside node_modules with ts-jest无法使用 ts-jest 转换 node_modules 中的文件夹
【发布时间】:2019-06-08 11:31:30
【问题描述】:

我正在使用 typescript 编写测试,并且我在 node_modules/@my-modules 文件夹中有私有依赖项。我正在使用 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 中转译文件的正确方法是什么?

【问题讨论】:

  • 好吧,我终于解决了。稍后会分享答案
  • 你是怎么解决的?

标签: jestjs ts-jest


【解决方案1】:

现在适合我的配置

软件包版本:

"@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)/)",
  ],
};

config/jest/fileMock.js:

module.exports = 'test-file-stub';

【讨论】:

    【解决方案2】:

    我关注the tuto here 并且工作得像个魅力人

    • npm install --save-dev jest typescript ts-jest @types/jest
    • npx ts-jest config:init

    我的另一个解决方案是更新jest.config.ts,使其包含以下设置

    transform: {
        //"^.+\\.(ts|tsx)$": "ts-jest",
      }, 
    

    【讨论】:

      猜你喜欢
      • 2017-03-25
      • 2021-09-04
      • 1970-01-01
      • 2021-02-13
      • 2019-12-08
      • 2023-02-01
      • 2016-02-18
      • 2021-06-16
      • 1970-01-01
      相关资源
      最近更新 更多