Jest测试框架(未完)

https://www.jestjs.cn/

快速入门

  • 安装 Jest
yarn add --dev jest
  • 创建名为 sum.test.js 的文件。这个文件包含了实际测试内容
const sum = require('./sum');

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});
  • 将如下代码添加到 package.json 中:
{
  "scripts": {
    "test": "jest"
  }
}
  • 最后,运行 yarn test
PASS  ./sum.test.js
✓ adds 1 + 2 to equal 3 (5ms)

生成基础配置文件

  • Jest 将根据你的项目提出一系列问题,并且将创建一个基础配置文件。文件中的每一项都配有简短的说明:
jest --init
  • 通过 Babel,Jest 能够支持 Typescript。
yarn add --dev @babel/preset-typescript

// babel.config.js
module.exports = {
  presets: [
    ['@babel/preset-env', {targets: {node: 'current'}}],
+    '@babel/preset-typescript',
  ],
};
  • Jest 在运行时并不会对你的测试用例做类型检查。 如果你需要此功能,可以使用 ts-jest
原文地址:https://www.cnblogs.com/qq3279338858/p/15492287.html