gpt4 book ai didi

javascript - 如何清除 Jest 模拟实现以进行下一步测试?

转载 作者:行者123 更新时间:2023-12-02 23:08:15 26 4
gpt4 key购买 nike

我正在设置 Jest 来测试 typescript 应用程序。

如何清除模拟函数并恢复其他测试的原始实现?

模拟我使用过的函数:jest.fn().mockImplementationOnce()

到目前为止,我已经在 beforeEach 和 afterEach 中尝试了 jest.clearAll()/resetModules()/resetAllMocks(),但没有成功。

app.test.ts

import App from './app';
import { DbService } from './lib/dbService';

describe('App', () => {
let dbService: DbService;
let app: App;

beforeEach(() => {
jest.clearAllMocks();
dbService = new DbService();
app = new App();
});

describe('getUsers', () => {

it('Should get an array users #1', () => {
expect(app).toBeInstanceOf(App);
const allUsers = app.getAllUsers();
expect(allUsers[0].id).toBeDefined();
});

it('should return an error #2', () => {
DbService.prototype.getAllUsers =
jest.fn().mockImplementationOnce(() => {
return new Error('No connection to DB');
});
expect(app.getAllUsers()).toEqual(new Error('No connection to DB'));
});

it('Should get an array users #3', () => {
expect(app).toBeInstanceOf(App);
const allUsers = app.getAllUsers();
expect(allUsers[0].id).toBeDefined();
});
});
});

app.ts

import { DbService } from './lib/dbService';

export default class App {
private dbService: DbService;

constructor() {
this.dbService = new DbService();
}

getAllUsers() {
return this.dbService.getAllUsers();
}
}

lib/dbService.ts

let instance: DbService;

export class DbService {
constructor() {
if (!instance) {
instance = this;
}
return instance;
}

getAllUsers() {
return [
{id: 1, username: 'john'},
{id: 2, username: 'bill'}
]
}
}

我希望测试 #3 能够像测试 #1 一样通过,但实际上失败并出现以下错误:

FAIL  src/app.test.ts
App
getUsers
√ Should get an array users #1 (3ms)
√ should return an error #2 (1ms)
× Should get an array users #3 (1ms)

● App › getUsers › Should get an array users #3

TypeError: Cannot read property '0' of undefined

31 | expect(app).toBeInstanceOf(App);
32 | const allUsers = app.getAllUsers();
> 33 | expect(allUsers[0].id).toBeDefined();
| ^
34 | });
35 | });
36 | });

最佳答案

我不确定这是否是实现此目的的jest方法,但我认为您可以将原始方法实现保存在变量中,并在每次测试后重新设置该方法,以防万一在测试中被 mock 。

例如

describe('App', () => {    
let dbService: DbService;
let app: App;
let originalGetAllUsersFn = DbService.prototype.getAllUsers;

//...

afterEach(() => {
// restore mocked method
DbService.prototype.getAllUsers = originalGetAllUsersFn;
});

});

关于javascript - 如何清除 Jest 模拟实现以进行下一步测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57490718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com