我有一个按钮,上面有用ReactJS代码定义的“下载”文本。现在,我想编写一个单元测试,以检查该函数在单击此按钮时是否被调用。我写了一个单元测试,但它不起作用。
import * as FileSaver from "file-saver"
it('File has to be saved when clicked on the "Download" button', () => {
jest.mock('file-saver', ()=>({saveAs: jest.fn()}));
fireEv
通过在我的测试文件的顶部添加以下行,我可以使用jasmine (库测试)和jest (快照测试)通过测试:
/// <reference path="../../node_modules/@types/jasmine/index.d.ts" />
// -> in my jest test files
/// <reference path="../../node_modules/@types/jest/index.d.ts" />
但是,当我运行tslint时,我得到了重复的错误,例如
ERROR in [at-loader]
我正在使用@angular-builders/jest来代替因果报应,在测试角项目时用玩笑来代替业力。有两个库,我喜欢得到额外匹配的玩笑:jest-extended和@testing-library/jest-dom。
我无法找到一种自动导入匹配器的方法,这样我就不必在每个规范文件中导入它们。
用jest-extended再现问题的最小示例
首先,创建一个角项目并安装jest依赖项。
ng new --defaults my-project
cd my-project
yarn add -D jest @types/jest @angular-builders/jest jest-extend
我得到了这个错误
测试套件运行失败
Your test suite must contain at least one test.
at node_modules/jest-cli/build/test_scheduler.js:108:22
使用jest配置
"jest": {
"transform": {
".*": "./tests/preprocessor.js"
},
"modulePaths": [
"src"
],
我从Jest单元测试开始。在运行一个jest单元测试时,我一直得到“没有测试发现”。
错误描述
yarn test
yarn run v1.3.2
$ jest
No tests found
In E:\Course\Testing JavaScript\Jest Demo
4 files checked.
testMatch: **/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x) - 3 matches
testPathIgnorePatterns: \\node_modul
我正在编写一个单元测试,测试一个等待超时的函数,然后再继续,我尝试使用
jest.useFakeTimers();
jest.advanceTimersByTime(20000);
但我一直收到一条错误信息:
: Timeout - Async callback was not invoked within the 20000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeou