首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法用ts-jest转换node_modules中的文件夹

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

Stack Overflow用户
提问于 2019-01-14 13:08:47
回答 2查看 3.6K关注 0票数 4

我正在用类型记录编写测试,并且在node_modules/@my-modules文件夹中有私有依赖项。我正在使用ts-jest编译器,而jest仍在抱怨node_modules文件夹中的下一个模块。

包版本:

代码语言:javascript
复制
"@types/jest": "^23.3.12",
"jest": "^23.6.0",
"ts-jest": "^23.10.5",

tsconfig.json:

代码语言:javascript
复制
{
  "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:

代码语言:javascript
复制
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)/)",
  ]
};

在运行玩笑时出现了精确的错误:

代码语言:javascript
复制
 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中传输文件的正确方法是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-18 08:36:06

现在对我起作用的配置

包版本:

代码语言:javascript
复制
"@types/jest": "^24.0.17",
"jest": "^24.8.0",
"ts-jest": "^23.10.5",

tsconfig.json:

代码语言:javascript
复制
{
  "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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
module.exports = 'test-file-stub';
票数 1
EN

Stack Overflow用户

发布于 2021-07-15 13:27:48

我跟着这里的图托,工作得很有魅力

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

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

代码语言:javascript
复制
transform: {
    //"^.+\\.(ts|tsx)$": "ts-jest",
  }, 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54182173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档