jest单元测试简记

App.test.tsx里可测试

sum.js文件

function sum(a, b) {
    return a + b;
  }
  module.exports = sum;

App.test.tsx,tobe用来判断对错,因为方法返回的结果不等于4,所以会报错

const sum = require('./sum');
// This is a custom Jest transformer turning style imports into empty objects.
// http://facebook.github.io/jest/docs/en/webpack.html
test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(4);
});

想要自己配置的话,可在package.json里配置

    "test": "jest --config .jest.js ",

然后.jest.js里就是配置信息

module.exports = { 
  verbose: true, 
  setupFiles: ['./tests/setup.js'],
  setupFilesAfterEnv: ['./tests/setupAfterEnv.ts'],
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'md'],
  modulePathIgnorePatterns: ['/_site/'],
  testRegex:'.*\\.test\\.(j|t)sx?$',//符合这个正则的则会加入判断
  snapshotSerializers: ['enzyme-to-json/serializer'],
  globals: {
    'ts-jest': {
      tsConfig: './tsconfig.test.json',
    },
  },
  testURL: 'http://localhost',
};

然后在比如button.test.js文件里可以找到
describe像是说明章节,没啥实际作用
it就是test
expect用来判断test的各种条件是否成立

猜你喜欢

转载自blog.csdn.net/chaogaoxiaojifantong/article/details/106224736