我似乎无法弄清楚为什么这个测试失败了;我已将问题减少到这两个文件..
[文件: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 声明。