最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest messageParent can only be used inside a worker - Stack Overflow

programmeradmin2浏览0评论

Is there a way to get a proper error message?

when i do

$npm test

and I intentionally break my code(cough cough remove a line of code) I get this message

src/redux/drivers/driver.saga.spec.js
   Test suite failed to run

    "messageParent" can only be used inside a worker

      at messageParent (node_modules/jest-worker/build/workers/messageParent.js:46:11)

I believe this is a meaningless error message and it would be nice to have something meaningful (=.

Here's my test

describe("DriverSocketFlow failed REASON:\n", () => {
  let socket;
  beforeEach(() => {
    socket = new MockedSocket();
    io.mockReturnValue(socket);
  });
  afterEach(() => {
    jest.restoreAllMocks();
  });
  const mockGeneratorPayload = { payload: { name: "Royal Palms" } };
  const generator = DriverSocketFlow(mockGeneratorPayload);

  test("Checking if DriverSocketFlow was called with it's methods and disconnected gracefully", () => {
    expect(generator.next(socket).value).toEqual(
      call(connect, mockGeneratorPayload.payload.name)
    );
    expect(generator.next(socket).value).toEqual(
      call(socketbug, mockGeneratorPayload.payload.name)
    );
    //disconnect gracefully
    expect(generator.next(socket).value).toEqual(
      fork(Read_Emit_Or_Write_Emit, socket)
    );
    expect(generator.next().value).toEqual(
      take(DriversActionTypes.DRIVERS_SOCKET_OFF)
    );
    expect(generator.next(socket).value).toEqual(call(disconnect, socket));
    expect(generator.next(socket).value).toEqual(call(disconnect, socket));

    expect(generator.next().value).toEqual(cancel());
  });
});

It should say

DriverSocketFlow failed REASON:

Checking if DriverSocketFlow generator function was called and disconnected gracefully

Thanks! for looking!

.... So i think i figured it out! here's my new test

test("1. Connected to the socket successfully", () => {
    expect(generator.next(socket).value).toEqual(
      call(Connect_To_Socket, mockGeneratorPayload.payload.name)
    );
    expect(generator.next(socket).value).toEqual(
      call(socketbug, mockGeneratorPayload.payload.name)
    );
  });
  test("2. Read_Emit_Or_Write_Emit generator function operations for socket.on and emit", () => {
    expect(generator.next(socket).value.payload.fn).toEqual(
      fork(Read_Emit_Or_Write_Emit, socket).payload.fn
    );
  });

but i think it's bug within npm test Don't know why it does it, but if you stop watch in npm test within package.json script tag and restart the test...It should work.....

"scripts": {
    "test": "react-scripts test --watchAll=false",
    
  },

and also....! don't know if this step helped but i added jest.config.js within the root directory and removed it then all of a sudden it worked... " <- cough cough it probably doesn't do anything just keep restarting it ¯_(ツ)_/¯.... it worked for me"

Here's the full code

driver.saga.spec.js

Is there a way to get a proper error message?

when i do

$npm test

and I intentionally break my code(cough cough remove a line of code) I get this message

src/redux/drivers/driver.saga.spec.js
   Test suite failed to run

    "messageParent" can only be used inside a worker

      at messageParent (node_modules/jest-worker/build/workers/messageParent.js:46:11)

I believe this is a meaningless error message and it would be nice to have something meaningful (=.

Here's my test

describe("DriverSocketFlow failed REASON:\n", () => {
  let socket;
  beforeEach(() => {
    socket = new MockedSocket();
    io.mockReturnValue(socket);
  });
  afterEach(() => {
    jest.restoreAllMocks();
  });
  const mockGeneratorPayload = { payload: { name: "Royal Palms" } };
  const generator = DriverSocketFlow(mockGeneratorPayload);

  test("Checking if DriverSocketFlow was called with it's methods and disconnected gracefully", () => {
    expect(generator.next(socket).value).toEqual(
      call(connect, mockGeneratorPayload.payload.name)
    );
    expect(generator.next(socket).value).toEqual(
      call(socketbug, mockGeneratorPayload.payload.name)
    );
    //disconnect gracefully
    expect(generator.next(socket).value).toEqual(
      fork(Read_Emit_Or_Write_Emit, socket)
    );
    expect(generator.next().value).toEqual(
      take(DriversActionTypes.DRIVERS_SOCKET_OFF)
    );
    expect(generator.next(socket).value).toEqual(call(disconnect, socket));
    expect(generator.next(socket).value).toEqual(call(disconnect, socket));

    expect(generator.next().value).toEqual(cancel());
  });
});

It should say

DriverSocketFlow failed REASON:

Checking if DriverSocketFlow generator function was called and disconnected gracefully

Thanks! for looking!

.... So i think i figured it out! here's my new test

test("1. Connected to the socket successfully", () => {
    expect(generator.next(socket).value).toEqual(
      call(Connect_To_Socket, mockGeneratorPayload.payload.name)
    );
    expect(generator.next(socket).value).toEqual(
      call(socketbug, mockGeneratorPayload.payload.name)
    );
  });
  test("2. Read_Emit_Or_Write_Emit generator function operations for socket.on and emit", () => {
    expect(generator.next(socket).value.payload.fn).toEqual(
      fork(Read_Emit_Or_Write_Emit, socket).payload.fn
    );
  });

but i think it's bug within npm test Don't know why it does it, but if you stop watch in npm test within package.json script tag and restart the test...It should work.....

"scripts": {
    "test": "react-scripts test --watchAll=false",
    
  },

and also....! don't know if this step helped but i added jest.config.js within the root directory and removed it then all of a sudden it worked... " <- cough cough it probably doesn't do anything just keep restarting it ¯_(ツ)_/¯.... it worked for me"

Here's the full code

driver.saga.spec.js

Share Improve this question edited Jul 20, 2021 at 15:15 youllbehaunted asked Feb 15, 2021 at 4:56 youllbehauntedyoullbehaunted 6491 gold badge8 silver badges16 bronze badges 2
  • Getting this error too; unfortunately running the script with --watchAll=false did not fix it. Nor did creating and then deleting jest.config.js, but I think we expected that was a coincidence. – Orchis Commented May 9, 2021 at 14:27
  • 1 Double check your usage of toBe and toEqual. This issue is most likely being caused by a recent regression bug in Jest - essentially trying to stringify an object with a circular ref. More details here: github.com/facebook/jest/issues/10577 – Eric Commented Jul 19, 2021 at 7:43
Add a comment  | 

2 Answers 2

Reset to default 8

Maybe the type of expectation variable and the answer on the right side are different in your case.

In my case, the error occurred because I tried to use toBe assertion in an array, and not in the array.length

Wrong example:

expect(array).toBe(3)

Correct example:

expect(array.length).toBe(3)

I suspect you're being overly hasty when you dismiss this error as irrelevant.

"messageParent" can only be used inside a worker

That error message suggests pretty strongly that your test is attempting to run worker code in an non-worker context.

The jest source confirms that this error is thrown when the thread where this code runs is not a worker.


(Full disclosure: I've never written a web worker before, so I've never tested one.)

It looks to me like you are testing your worker code by loading it into some kind of non-worker. Your sample makes me think that DriverSocketFlow talks to io to obtain a socket. I assume this socket is the socket over which worker messages will be passed. Presumably, your real worker code uses messageParent to reply to incoming messages.

It looks like Jest provides some tech for running web workers. My guess is that this tech actually runs the code to be tested inside a web worker. I'm also guessing that you are trying to do it the other way: instead of really spawning a worker, you're using regular Jest mocks to create fake versions of the worker context that you believe are relevant to your test. One reason I'm guessing that is because that's how I assume would do this.

If all of this is correct, then Jest offers two ways to test worker code, and you're trying to mix the two techniques, which is failing.

You can either:

  • (A) use the Jest worker tech to load your production worker code into a real web worker (which I assume runs in a separate thread -- presumably one of the major benefits of using Jest's worker tech is that Jest then manages the lifecycle of that thread for you). That would make this error would go away, but you'd also have to stop mocking the socket. If my speculation is accurate, I would expect the Jest worker tech to provide you with a real socket from the worker thread it's managing.
  • (B) stop using Jest's worker tech, and run the worker code in the main testing thread, which would then rely on a set of mocks you would create and provide, including this fake socket that the "worker" will use to communicate with the test harness. If MockedSocket doesn't do it, you may be responsible for constructing a fake messageParent on messages your test sends to the worker.
发布评论

评论列表(0)

  1. 暂无评论