首页 > 解决方案 > React TypeScript Jest 酶测试

问题描述

我正在尝试对打字稿做出反应来运行 uni 测试。

这是我正在使用的 npm 包:

"@types/enzyme": "^3.10.4",
"@types/enzyme-adapter-react-16": "^1.0.5",
"@types/jest": "^25.0.0",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.15.2",
"enzyme-to-json": "^3.4.3",
"jest": "^25.0.0",
"jest-cli": "^25.0.0",
"ts-jest": "^24.3.0"

est.config.ts

module.exports = {

  roots: ["<rootDir>/src"],
  transform: {
    "^.+\\.tsx?$": "ts-jest"
  },
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
  testEnvironment: "node",
  globals: {
    "ts-jest": {
      diagnostics: {
        warnOnly: true
      }
    }
  },

  // Setup Enzyme
  snapshotSerializers: ["enzyme-to-json/serializer"],
  setupFilesAfterEnv:
  [
     "<rootDir>/src/setupEnzyme.ts"
  ] 
};

setupEnzyme.ts

import { configure } from 'enzyme';
import EnzymeAdapter from 'enzyme-adapter-react-16';

configure({ adapter: new EnzymeAdapter() });

测试功能

export const sum = (...a: number[]) => a.reduce((acc, val) => acc + val, 0);

测试

import { sum } from './foo';

/**
 * Basic Example
 */
test('basic', () => {
  expect(sum()).toBe(0);
});

test('basic again', () => {
  expect(sum(1, 2)).toBe(3);
});

/**
 * Async Example
 *
 * Jest has built-in async/await support. e.g.
 */
test('basic async', async () => {
  expect(sum()).toBe(0);
});

test('basic async again', async () => {
  expect(sum(1, 2)).toBe(3);
});

foo.ts 和 foo.test.ts 位于测试文件夹中。

错误是:

FAIL src/ tests /foo.ts ● 测试套件未能运行

Your test suite must contain at least one test.

  at onResult (node_modules/@jest/core/build/TestScheduler.js:163:18)

通过 src/测试/foo.test.ts

测试套件:1 个失败,1 个通过,总共 2 个测试:4 个通过,总共 4 个快照:总共 0 个时间:1.714 秒,估计 2 秒运行所有测试套件。npm 错误!代码 ELIFECYCLE npm 错误!errno 1 npm 错误!wasyster.git.com@0.0.1 测试:jest npm 错误!退出状态 1 npm ERR!npm 错误!wasyster.git.com@0.0.1 测试脚本失败。npm 错误!这可能不是 npm 的问题。上面可能有额外的日志输出。

npm 错误!可以在以下位置找到此运行的完整日志:npm ERR!C:\Users\wasyster\AppData\Roaming\npm-cache_logs\2020-01-15T16_23_16_125Z-debug.log

在我的文件夹结构中有一个.babelrc文件,内容如下

{
  "presets": ["react", "es2015", "stage-0"],
  "plugins": ["emotion", "transform-react-jsx"]
}

如果需要更多,我可以提供任何配置文件。

谢谢

标签: reactjstypescriptunit-testingjestjsenzyme

解决方案


推荐阅读