【发布时间】:2021-04-19 01:40:14
【问题描述】:
我正在努力在每个测试的基础上用 Jest 模拟一个常量。我让它与下面的代码一起工作,但模拟是“静态的”——我不能为每个测试不同地模拟它。
代码:
// allowList.ts
export const ALLOW_LIST = {
'1234': true
};
// listUtil.ts
import { ALLOW_LIST } from './allowList.ts';
export const checkList = (id: string) => {
if (ALLOW_LIST[id]) return true;
return false;
};
测试(工作):
// listUtil.test.ts
import { checkList } from './listUtil';
jest.mock('./listUtil', () => {
return {
'5678': true
};
});
test('in list', () => {
expect(checkList('5678')).toBe(true);
});
test('not in list', () => {
expect(checkList('1234')).toBe(false);
});
我想要的(不工作):
// listUtil.test.ts
import { checkList } from './listUtil';
test('in list', () => {
jest.mock('./listUtil', () => {
return {
'5678': true
};
});
expect(checkList('5678')).toBe(true);
});
test('not in list', () => {
jest.mock('./listUtil', () => {
return {
'9123': true
};
});
expect(checkList('1234')).toBe(false);
});
我正在尝试做的事情可能吗? This post 非常相似,并且在模拟函数时似乎可以工作,但我遇到了与接受答案的评论者相同的问题。我想我只是不明白 Jest 如何在幕后进行嘲笑。我相信工作版本可以工作,因为模拟被提升并且基本上覆盖了真实的实现,但我不确定如何或是否可以在每次测试中实现这一点。
我认为一种选择是通过函数公开 ALLOW_LIST:
// allowList.ts
const ALLOW_LIST = {
'1234': true
};
export const getAllowList = () => ALLOW_LIST;
并嘲笑它,但我想知道这是否有必要。
【问题讨论】:
标签: typescript unit-testing jestjs mocking ts-jest