Blog
  • README
  • Daily-life
    • 关于简历
    • 如何做好劳逸结合
    • 探索历程
    • 如何走出个人舒适圈
    • 怎样提升领导能力
    • 个人装换机之 Windows 系列
    • 工作之余 - 技能修炼
    • Search: 业余的专业搜索技能
    • 自我迭代
    • 优秀的自我介绍很重要
    • 工作心得
  • Checklist
    • 开发自检清单
    • 代码教鞭:不值得犯第二次的错
  • Docs
    • Bash: 入门重点
    • CSS 特殊用法
    • ChatGPT 使用总结
    • Console
    • Corn 表达式
    • DOS command
    • Emoji: 供日常学习、查询及使用
    • Git 命令集
    • GitBook
    • Github Action
    • Github
    • HTTP: 小知识巧总结
    • JS 精选
    • Jest learning manual
    • License: 解说
    • Mac
    • Markdown
    • 小程序
    • NPM and Plugin Guide
    • 渐进式 Web 应用(PWA)
    • Regexp
    • Rollup
    • SEO
    • SQL
    • 专业名词
    • Testing
    • TypeScript
    • Vue - fragment knowledge
    • Web API
    • 站点:那些可以经常逛逛的开发工具网站
    • Webpack
    • Windows 之路遇精彩
  • Project
    • README 模板:Project Name
    • 借鉴-项目规范
  • React
    • Antd typescript
    • React: 总结整理
    • React Hook - 速览
    • React-生命周期方法转 Hooks-1
    • React: 生命周期方法转 Hooks-2
    • Redux
    • React router
    • useEffect
  • Share
    • StartToGit
    • 3步自动同步你的 Github 仓库到 Gitee 仓库
    • 软考高级“信息系统项目管理师”考试忠告
    • 轻松使用WebWorker,解放耗时较大的算法代码
  • Tool
    • 最好的工具: 理解并使用那些耳目一新的巧工具
    • IntelliJ IDEA
    • VS Code:发挥应有的效率
  • Office
    • Excel 常用操作
    • Word: 文档操作精选技巧
由 GitBook 提供支持
在本页
  • Docs
  • Samples
  • 采坑系列

这有帮助吗?

  1. Docs

Jest learning manual

上一页JS 精选下一页License: 解说

最后更新于3年前

这有帮助吗?

Docs

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;

beforeEach(() => {
  global.fetch = jest.fn().mockImplementation(() => mockFetchPromise);
});

afterEach(() => {
    // 退出时进行清理
    global.fetch.mockClear();
    delete global.fetch;
});
  • 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

      yarn add --dev babel-jest @babel/core @babel/preset-env babel-plugin-transform-es2015-modules-commonjs
    • 配置 babel.config.js

      module.exports = {
          presets: [
              [
                  "@babel/preset-env",
                  {
                      targets: {
                          node: "current"
                      }
                  }
              ]
          ],
          plugins: ["transform-es2015-modules-commonjs"]
      };
      • 配置 jest.config.js 或在 package.json 中添加 jest 配置

      module.exports = {
          // 查看报错的 import 方法来源的包,比如是 lodash-es, 如果还有其他的,添加到 other-es-lib 的位置
          // 重点:将不忽略 lodash-es, other-es-lib 这些es库, 从而使 babel-jest 去处理它们
          transformIgnorePatterns: ["<rootDir>/node_modules/(?!(lodash-es|other-es-lib))"]
      };
Jestjs
Edit Jest-Samples