我似乎无法弄清楚为什么这个测试失败了;我已将问题减少到这两个文件..
[文件:esm-user.js]
const getJack = () => {return 'Jack';};
const getJill = () => {return 'Jill';};
const getUsers = () => {return [getJack(), getJill()];};
export default { getUsers, getJack };
[文件:esm-user-spy.spec.js]
import users from './esm-user';
describe("Mocks and Spies", () => {
test.only("partial mock (jack)", () => {
var getJackSpy = jest.spyOn(users, 'getJack').mockReturnValue('Mock Jack');
expect(users.getUsers()).toEqual(['Mock Jack', 'Jill']);
});
});
[输出]
expect(received).toEqual(expected) // deep equality
Array [
- "Mock Jack",
+ "Jack",
"Jill",
]
getUsers
调用函数声明getJack
,但您正在监视getJack
默认导出对象的属性,并用您的模拟实现替换默认导出的该键处的值。getUsers
该文件中的函数不会调用getJack
默认导出对象,而是调用getJack
文件中的声明。例如,下面是一种方法(还有很多其他方法),因为您正在模拟实现的函数与中调用的函数相同
getUsers