起步

  1. jest --init

按照文档写了一个测试用例

  1. // last.ts
  2. function last(array: any[]) {
  3. const length = array == null ? 0 : array.length
  4. return length ? array[length - 1] : undefined
  5. }
  6. export default last
  7. // __test__/last.test.ts
  8. test("get array last element", () => {
  9. expect(last([1, 2, 3])).toBe(3)
  10. })
  1. "scripts": {
  2. "test": "jest",
  3. "test:watch": "jest --coverage --watch",
  4. "test:prod": "npm run lint && npm run test -- --no-cache"
  5. },

npm run test

last.ts: Unexpected token, expected “,” (9:19)

然后开始配置babel
yarn add --dev babel-jest @babel/core @babel/preset-env @babel/preset-typescript

// babel.config.js

参考链接

羚珑项目自动化测试方案实践