Sha*_*oon 9 javascript web-worker typescript jestjs
我有一个测试:
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');
});
});
Run Code Online (Sandbox Code Playgroud)
在 中heicUtils
,我使用的是heic2any,它使用 WebWorkers。我如何才能正确地模拟 Worker 进行 Jest 测试?
由于您正在测试您的heicUtils
模块,因此您应该模拟该heic2any
库,否则,您将测试第 3 方库而不是您自己的代码。
在模拟中,您应该定义heic2any
您heicUtils
使用的函数/方法以及它们应该为您打算编写的每个测试用例返回什么。
可以在这里找到如何模拟模块的示例:https://jestjs.io/docs/manual-mocks
归档时间: |
|
查看次数: |
2636 次 |
最近记录: |