我花了几个小时试图用人工模拟来模拟常量,但它无法工作。救命啊!!
假设我想通过模拟status变量来测试src/age.ts中显示的CURRENT_AGE方法。
// src/age.ts
import { CURRENT_AGE } from "./constants"
export class Age {
/**
* return "OLD" if the CURRENT_AGE is over 50.
* return "YOUNG" if otherwise.
*/
public status() {
在我的测试文件中,我有:
describe('component', () => {
globals.Globalfunct = {
testfunc: jest.fn().mockReturnValue('something)
}
it('is a test', () => {
})
it('is another test', () => {
// in here I want testfunc to return something else, how can I d
我在TypeScript中使用Jest (使用ts-jest)。这是我的测试主题,它使用dotenv库:
import * as dotenv from 'dotenv';
export class TestSubject {
public function test() {
console.log(dotenv); // Here I debug if I'm using a mock or not
}
}
我只想为了一个特定的测试而模拟dotenv库,但是我被困住了。
这是我的测试套件:
import { TestSubject } from
我在TypeScript中使用Jest 27.5.1。当我尝试测试时,我得到了以下错误:
● Test suite failed to run
TypeError: Cannot read properties of undefined (reading 'extend')
2 | <div>Hello</div>
3 | </template&g
我想用nodejs和jest编写集成测试。
据我所知,集成测试应该与数据库和第三方交互。
但是怎么写呢?
In jest?,他们的编写方式与单元测试不同吗?,他们在文件中有另一个扩展名吗?或者他们最终使用了.spec.tsDo,他们需要使用特殊的directories?Do,我从来没有在这些测试中创建过存根/模拟?
我发现当使用jest.doMock而不是jest.mock来模拟一个函数时(我需要在不同的it块中为同一个函数创建多个模拟实现),我发现测试失败了
误差
expect(jest.fn()).toBeCalled()
Expected mock function to have been called.
此外,如果我要求模块位于测试的顶部而不是在相同的it块中执行,则会给出一个不同的错误:
expect(jest.fn())[.not].toBeCalled()
jest.fn() value must be a mock function or spy.
Received:
funct