如何在Jest中清除模拟函数中的间谍?如果原始函数不是jest.fn(),那么jest.restoreAllMocks()可以很好地工作。如何正确清除jest.fn()中的所有间谍?这个问题可能是How to change mock implementation on a per single test basis [Jestjs]的重复,尽管我的问题描述了一个更简单的场景,其中不涉及多个模块。 describe('Mock', () => {
const mock = {
method1: jest.fn().mockReturnValue(false),
我有一个TypeScript模块(应该是无关紧要的,因为我认为这也会影响JS ),我正在尝试测试我有的一个模块。该模块从外部文件导入大量数据,并根据a变量选择应返回的数据。
我正在尝试运行一些测试,其中我更新了变量,重新require了模块,并在一个文件中运行了进一步的测试。但我的问题是文件的require只运行一次。我猜它被缓存了。有没有可能告诉Jest的require函数不缓存或在测试之间清除缓存?
下面是我想要实现的一些精简代码:
module.ts
import { getLanguage } from "utils/functions";
import * as m
我正在测试一些代码来计算重现次数。
这个不起作用,因为我像个孩子一样传递<MyComponent>。
it("should get the same object when the parent rerenders", async () => {
jest.useFakeTimers();
const callback = jest.fn();
let renderCount = 0;
let x = 0;
function MyComponent() {
const random = Math.rand
在我的项目文件夹中,我有很多包含js代码和test.js文件的子文件夹。我想要能够测试特定的文件。例如,假设在我们的项目文件夹中有'fib‘文件夹:
C:.
└───exercises
└───fib
fib-test.js
index.js
现在,在习题文件夹中执行jest命令:
jest fib\fib-test.js
我得到了:
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In C:\exercise
我想测试一个在其实现中使用状态的钩子,但是每次我运行测试时都会得到以下错误:
Error: Uncaught [Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.]
钩子很复杂,我找不到哪个部分可能导致问题(另外,在使用npm start运行应用程序时,它在实际情况下工作得非常好)。
我试图编写一个虚拟测试,看看我是否能够找到任何东西,我发现更新状态(我在钩子中做的)触发了错误。
基本上,这是失败的:
import { renderHook } from "
我有一个轮询挂钩,似乎在我的应用程序中工作。
import { useCallback, useEffect, useRef } from "react";
/**
* @param asyncFunction
* @param interval milliseconds between calls to the asyncFunction, defaults to a minute
* @param immediate if true it will run the asyncFunction immediately before looping
*/
export
为了运行Jest测试的React代码示例(来自 ),我得到了以下错误:
> @ test /home/aizquier/jest/examples/react
> jest
Using Jest CLI v0.7.1
FAIL __tests__/CheckboxWithLabel-test.js
● Runtime Error
SyntaxError: /home/aizquier/jest/examples/react/__tests__/CheckboxWithLabel-test.js: Unexpected token (15:6)
n
我正在为Jest编写一个小测试工具(只是为了学习)。它被称为assertTruthy(msg, fn, args),它需要一个消息、一个函数和参数,如果使用参数调用该函数时返回的内容是真实的,则应该传递,如果它不正确,则会失败。
我想将它添加到Jest中,这样您就可以调用它,而无需在每个环境中导入它。
describe('someFn()', () => {
// like 'it', 'describe' and 'expect' it should just be available here
it('i
在顶部,我在一个javascript应用程序中提供了以下内容:
const d = require('files/file1.json)
export class Processor{
getTag(){
//process data
return tag;
}
}
其中,文件在文件所在且正在运行的目录的package.json中定义。file1.json只包含带有数字的字符串键。我想为每个测试用例模拟这些值,但到目前为止我还不能:
it("should tag data as red", ()