Jest messageParent只能在工作线程内部使用

lb3vh1jj  于 2022-12-08  发布在  Jest
关注(0)|答案(2)|浏览(172)

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

xam8gpfp

xam8gpfp1#

在您的案例中,可能期望变量的类型和右侧的答案不同。

在我的例子中,发生错误是因为我试图在数组中使用toBeAssert,而不是在数组中。
错误示例:

expect(array).toBe(3)

正确示例:

expect(array.length).toBe(3)
btqmn9zl

btqmn9zl2#

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.

相关问题