【问题标题】:Jest + React + TypeScript: default configurationJest + React + TypeScript:默认配置
【发布时间】:2019-03-19 15:55:11
【问题描述】:

有人可以为 Jest + React + TypeScript 应用程序提供一个默认的“像魅力一样工作;)”配置示例吗?

我们当前的 jest.config.js 是

module.exports = {
roots: [ '/src' ],
transform: {
'^.+\.tsx?$': 'ts-jest',
'.+\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
},
testRegex: '(/tests/.*|(\.|/)(test|spec))\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],

// Setup Enzyme
snapshotSerializers: ['enzyme-to-json/serializer'],
setupFilesAfterEnv: ["/enzymeConfig.ts"]
}

似乎我们仍然遗漏了一些重要的东西,但不知道到底是什么..

【问题讨论】:

  • 您遇到了什么错误?
  • 通常我无法到达元素或触发事件或更改。

标签: reactjs typescript configuration jestjs


【解决方案1】:

我没有使用 Enzyme(对大多数功能使用 React 测试库)。这是我的配置,可以解决问题,但并没有太大的不同。

在 package.json 而不是 jest.config.js 中定义


  "jest": {
    "rootDir": "src",
    "globals": {
      "ts-jest": {
        "tsConfig": "tsconfig.test.json"
      }
    },
    "collectCoverageFrom": [
      "<rootDir>/**/*.{js,jsx,ts,tsx}"
    ],
    "coverageDirectory": "jest-coverage",
    "setupFilesAfterEnv": [
      "@testing-library/react/cleanup-after-each",
      "jest-styled-components"
    ],
    "setupFiles": [
      "raf/polyfill",
      "<rootDir>/__jest__/browserMocks.js",
      "<rootDir>/__jest__/matchMediaMock.js"
    ],
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    },
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|svg|css|less|scss)$": "<rootDir>/__jest__/fileMock.js"
    }
  },

【讨论】:

    猜你喜欢
    • 2022-11-15
    • 1970-01-01
    • 2020-02-04
    • 2018-12-11
    • 2019-12-17
    • 2017-06-24
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    相关资源
    最近更新 更多