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?

Share Improve this question edited Sep 13, 2022 at 14:00 tenshi 26.4k4 gold badges30 silver badges61 bronze badges asked Sep 13, 2022 at 13:56 ShamoonShamoon 43.6k101 gold badges329 silver badges628 bronze badges 2
  • 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
Add a ment  | 

3 Answers 3

Reset to default 4

if 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