AlexZ33 / lessions

自己练习的各种demo和课程
12 stars 2 forks source link

jest测试 #74

Open AlexZ33 opened 4 years ago

AlexZ33 commented 4 years ago

过程

npm install --save-dev jest
npm install --save-dev babel-jest regenerator-runtime
npm install --save-dev @babel/cli @babel/core @babel/preset-env @babel/plugin-transform-regenerator @babel/plugin-transform-runtime @babel/runtime

项目目录添加.babelrc 文件

{
    "presets": ["@babel/preset-env"],
  "plugins": [
    "@babel/plugin-transform-runtime"
  ]
}

package.json 里添加命令 test:jest

"scripts": {
    "build": "webpack --mode=production --config=config/webpack.prod.js",
    "test": "jest"
  },

项目里添加 test 目录

test("测试返回数组长度为1", () => {
  expect(fun("url")).toHaveLength(1);
});
test("异步", () => {
  return fun("id").then(res => {
    // console.log(res)
    expect(res).toHaveLength(1);
  });
});
// 或者
test("异步", async () => {
  let res = await fun("id");
  // console.log(res)
  expect(res).toHaveLength(1);
});