如何嘲笑一名工人参加耶稣会考试



我有一个测试:

import { convertHeicToPng } from './heicUtils';
class Worker {
url: string;
onmessage: (m?: any) => void;
constructor(stringUrl: string) {
this.url = stringUrl;
this.onmessage = () => {};
}
postMessage(msg: any) {
this.onmessage(msg);
}
}
(window.Worker as any) = Worker;
describe('test heicUtils', () => {
test('should convert HEIC to PNG', async () => {
const file = new File([''], 'test.heic', { type: 'image/heic' });
const base64 = await convertHeicToPng(file);
expect(base64).toContain('data:image/png;base64');
});
});

heicUtils中,我使用的是heic2any,它使用WebWorkers。我如何才能正确地嘲笑一个工人进行一次恶作剧测试?

如果您使用的是typescript,将此代码添加到您的jest设置文件中可能会起作用:

type MessageHandler = (msg: string) => void;
class Worker {
url: string;
onmessage: MessageHandler;
constructor(stringUrl: string) {
this.url = stringUrl;
this.onmessage = noop;
}
postMessage(msg: string): void {
this.onmessage(msg);
}
}
Object.defineProperty(window, 'Worker', {
writable: true,
value: Worker
});

由于您正在测试heicUtils模块,因此应该模拟heic2any库,否则,您将测试第三方库,而不是您自己的代码。

在mock中,您应该定义heicUtils使用的heic2any的函数/方法,以及它们应该为您打算编写的每个测试用例返回什么。

如何模拟模块的示例可以在这里找到:https://jestjs.io/docs/manual-mocks

tsc&摩卡——报告员规格-t 5000——退出

.npm安装mocha
。然后执行此cmd。示例我的github:https://github.com/www778878net/koa78-base78

最新更新