如何使用Jest Framework从模块中仅模拟一个单打字条类,但要保持其他类/功能
我有一个带有函数的打字稿模块,该功能正在尝试与Jest一起测试。我要测试的功能使用与函数同一模块中定义的打字稿类。两者都是出口。
我正在尝试嘲笑这堂课,但是模拟的班级没有使用。正在使用正在测试的文件中的原始版本。
免责声明:出于简洁起见,这是一个人为的示例,但与我与我合作的代码库的设计非常相似。
这是代码:
// mock.ts
export class Foo {
bar(): string {
return 'Original Foo.bar';
}
}
export function fubar() {
return new Foo().bar();
}
// mock.test.ts
import { expect, jest, test } from '@jest/globals';
import { fubar } from './mock';
jest.mock('./mock.ts', () => {
return {
...(jest.requireActual('./mock') as Object),
Foo: {
bar: jest.fn().mockImplementation(() => {
return 'Mocked Foo.bar';
})
}
};
});
describe('Mock Testing', () => {
test('Test if mocked Foo class works', () => {
const result = fubar();
expect(fubar()).toBe('Mocked Foo.bar');
});
});
我的测试的输出是:
Expected: "Mocked Foo.bar"
Received: "Original Foo.bar"
16 | test('Test if mocked Foo class works', () => {
17 | const result = fubar();
> 18 | expect(fubar()).toBe('Mocked Foo.bar');
| ^
19 | });
20 | });
21 |
显然,我没有正确设置模拟,但是开玩笑的文档似乎很容易就如何在打字稿中进行操作和/或当单个模块时如何处理情况。导出多个功能/类,您不想模拟所有内容。
I have a typescript module with a function that I'm trying to test with jest. The function I'm trying to test uses a typescript class that's defined in the same module as the function. Both are exported.
I am trying to mock the class, but the mocked class isn't getting used. The original version in the file under test is being used.
Disclaimer: This is a contrived example for the sake of brevity, but is very similar to how the codebase is designed I'm working with.
Here's the code:
// mock.ts
export class Foo {
bar(): string {
return 'Original Foo.bar';
}
}
export function fubar() {
return new Foo().bar();
}
// mock.test.ts
import { expect, jest, test } from '@jest/globals';
import { fubar } from './mock';
jest.mock('./mock.ts', () => {
return {
...(jest.requireActual('./mock') as Object),
Foo: {
bar: jest.fn().mockImplementation(() => {
return 'Mocked Foo.bar';
})
}
};
});
describe('Mock Testing', () => {
test('Test if mocked Foo class works', () => {
const result = fubar();
expect(fubar()).toBe('Mocked Foo.bar');
});
});
The output of my test is:
Expected: "Mocked Foo.bar"
Received: "Original Foo.bar"
16 | test('Test if mocked Foo class works', () => {
17 | const result = fubar();
> 18 | expect(fubar()).toBe('Mocked Foo.bar');
| ^
19 | });
20 | });
21 |
Clearly, I'm not setting up my mock correctly, but the jest documentation seems to be light on how to do this in typescript and/or how to handle the case when a single module exports multiple functions/classes and you don't want to mock everything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过严重的震惊,事实证明不可能实现这一目标。更确切地说,在我的示例中,即使在我的测试脚本的上下文中嘲笑了foo,fubar看到的foo副本是未锁定的版本。唯一的解决方案是将FOO移至其他模块/文件。
After much consternation, it turns out it is not possible to achieve this. More precisely, in my example, even though Foo is mocked in the context of my test script, the copy of Foo that fubar sees is the unmocked version. The only solution is to move Foo to a different module/file.
这对我有用:
This worked for me: