Jest learning manual

Docs

Jestjs

Samples

Edit Jest-Samples

采坑系列

  • 异步 await expect(fetchData()).rejects does not work

function fetchData() {
  return new Promise(function(resolve, reject) {
    if (true) {
      resolve("peanut butter");
    } else {
      reject(new Error("error"));
    }
  });
}
test("the fetch fails with an error", async () => {
// await expect(fetchData()).rejects.toMatch("error");
//expect(received).rejects.toMatch()
// Expected received Promise to reject, instead it resolved to value "peanut butter"

// await expect(fetchData()).rejects.toThrow("error");
// expect(received).rejects.toThrow()
// Expected received Promise to reject, instead it resolved to value "peanut butter"
});
  • Cannot spy the fetch property because it is not a function;

  • No tests found, exiting with code 1

The test file need to be xxx.test.js, not xxx.js when the Jest config contains testRegex ".test.js"

  • Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    https://www.cnblogs.com/xueyoucd/p/10495922.html 解决jest处理es模块 - 学友2000 - 博客园

    • 安装依赖包,特别是 babel-plugin-transform-es2015-modules-commonjs

    • 配置 babel.config.js

      • 配置 jest.config.js 或在 package.json 中添加 jest 配置

最后更新于

这有帮助吗?