admin管理员组文章数量:1316012
I have a test:
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');
});
});
and in heicUtils
, I'm using heic2any, which uses WebWorkers. How can I properly mock a Worker for a Jest test?
I have a test:
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');
});
});
and in heicUtils
, I'm using heic2any, which uses WebWorkers. How can I properly mock a Worker for a Jest test?
- What exactly doesn't work with your implementation? Are you missing several features of a Worker and don't want to manually implement them or are you running into errors that you wouldn't expect with your mock? – Viktor Luft Commented Sep 13, 2022 at 16:52
-
2
The library's author should test it works fine. You may want to test that you do pass the correct arguments there, and that when your receive a result you do handle it correctly, but you probably don't have to test what happens in between. If you really need to test that e.g you do transfer data correctly etc. you may want to use a
MessageChannel
to use the same StructureSerializeWithTransfer algorithm than Workers, but even then that'd be testing node's implementation and not your code per se. – Kaiido Commented Sep 13, 2022 at 23:02
3 Answers
Reset to default 4if you are using typescript, adding this code to your jest setup file might work:
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
});
Since you are testing your heicUtils
module, you should mock the heic2any
lib, otherwise, you will be testing the 3rd party library instead of your own code.
In the mock, you should define the functions/methods of heic2any
that your heicUtils
use and what they should return for each test case you intend to write.
Examples of how to mock modules can be found here: https://jestjs.io/docs/manual-mocks
tsc && mocha --reporter spec -t 5000 --exit
.npm install mocha
.then do this cmd
.Examples my github : https://github./www778878net/koa78-base78
本文标签: javascriptHow to mock a worker for a Jest testStack Overflow
版权声明:本文标题:javascript - How to mock a worker for a Jest test? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741987415a2408764.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论