jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.08k stars 6.45k forks source link

[Bug]: DataCloneError: Symbol(jest.asymmetricMatcher) could not be cloned using `--workerThreads`. #15078

Open VictorGlindasPaf opened 4 months ago

VictorGlindasPaf commented 4 months ago

Version

29.70

Steps to reproduce

Create a test with expect.any, or any other jest helpers that uses symbols.

it("works", () => {
  expect({}).toEqual({
    value1: 0,
    value2: expect.any(String),
  });
})

Run the test with worker threads:

jest --workerThreads --maxWorkers=50%

Expected behavior

I should get a regular test failure message, that actually tells me what expect is failing.

Actual behavior

I get an error that tells me next to nothing.

  ● Test suite failed to run

    DataCloneError: Symbol(jest.asymmetricMatcher) could not be cloned.

      at reportSuccess (../../node_modules/jest-worker/build/workers/threadChild.js:99:34)

Seed:        1878439832
Test Suites: 1 failed, 1 total

Additional context

No response

Environment

System:
    OS: macOS 14.0
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
    Yarn: 3.5.1 - ~/.nvm/versions/node/v20.10.0/bin/yarn
    npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm
  npmPackages:
    jest: ^29.7.0 => 29.7.0
SimenB commented 4 months ago

https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#javascript_types

Primitive types, except symbol.

Great... πŸ˜… Not sure how we can deal with this properly πŸ€”

VictorGlindasPaf commented 4 months ago

To expand on this, it doesn't only seem to be an issue with jest symbols, but with anything that throws and can't be structurally cloned.

For example if I run a test that calls axios and the test throws an AxiosError, I get the same vague DataCloneError.


Another side note; I don't seem to get this error with .toMatchObject, since the error that throws doesn't contain the symbols that .toEqual does.

ldeveber commented 3 months ago

I just hit this too, but my output is:

  ● Test suite failed to run

    DataCloneError: function () {
            return fn.apply(this, arguments);
          } could not be cloned.

      at reportSuccess (node_modules/jest-worker/build/workers/threadChild.js:98:34)

If I change my test to be like:

test('blah', () => {
  try {
    // all test contents here
  } catch (e) {
     console.log(e);
  }
});

I can actually get the real failure. Not a workaround by any means :(

  console.log
    JestAssertionError: expect(received).toHaveBeenCalledOnce(expected)

    Expected mock function to have been called exactly once, but it was called:
      0 times
        at Object.toHaveBeenCalledOnce (~/project/src/components/__tests__/Thing.test.tsx:86:25) {
      matcherResult: {
        pass: false,
        message: 'expect(received).toHaveBeenCalledOnce(expected)\n' +
          '\n' +
          'Expected mock function to have been called exactly once, but it was called:\n' +
          '  0 times',
        actual: [Function: mockConstructor] {
          _isMockFunction: true,
          getMockImplementation: [Function (anonymous)],
          mock: [Getter/Setter],
          mockClear: [Function (anonymous)],
          mockReset: [Function (anonymous)],
          mockRestore: [Function (anonymous)],
          mockReturnValueOnce: [Function (anonymous)],
          mockResolvedValueOnce: [Function (anonymous)],
          mockRejectedValueOnce: [Function (anonymous)],
          mockReturnValue: [Function (anonymous)],
          mockResolvedValue: [Function (anonymous)],
          mockRejectedValue: [Function (anonymous)],
          mockImplementationOnce: [Function (anonymous)],
          withImplementation: [Function: bound withImplementation],
          mockImplementation: [Function (anonymous)],
          mockReturnThis: [Function (anonymous)],
          mockName: [Function (anonymous)],
          getMockName: [Function (anonymous)]
        }
      }
    }
CheRayLiu commented 3 weeks ago

Just wanted to chime in here that I ran into the same issue as well.

  ● Test suite failed to run

    DataCloneError: () => somefunction() could not be cloned.

There was an actual test failure but it wouldn't show up in test output because the error.

Not an actual solution, but running the jest test --workerThreads=false is how I was able to see the actual error and able to fix my tests.