美文网首页
【typescript测试 - Jest 配置与使用】

【typescript测试 - Jest 配置与使用】

作者: wn777 | 来源:发表于2024-05-05 23:12 被阅读0次
Jest

安装

npm install --save-dev @types/jest
npm install --save-dev ts-jest

配置

tsconfig.json

{
  "compilerOptions": {
    "types": ["jest"]
  }
}

jest.config.js

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};

使用

// add.js
function add(a, b) {
  return a + b;
}

module.exports = add;

测试文件

// add.test.js
const add = require('./add');

describe('add function', () => {
  let result;

  beforeEach(() => {
    result = add(1, 2);
  });

  test('adds 1 + 2 to equal 3', () => {
    expect(result).toBe(3);
  });
});

相关文章

网友评论

      本文标题:【typescript测试 - Jest 配置与使用】

      本文链接:https://www.haomeiwen.com/subject/rcclfjtx.html