Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sinon Chai calledWithMatch causes structuredClone to error in the test runner #2772

Open
Sanderovich opened this issue Aug 2, 2024 · 10 comments

Comments

@Sanderovich
Copy link

See this repo for a reproduction https://github.com/Sanderovich/web-test-runner-called-with-match-error-reproduction.

When an expect(spy).to.be.calledWithMatch() is unsuccesful the expect throws an AssertionError that web test runner can not handle which causes the following exception.
image

The crash seems to happen because of the actual property inside the AssertionError. The actual property is set to a function called proxy and structuredClone cannot handle functions (https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm).

A possible solution could be to JSON.stringify() and JSON.parse() the AssertionError before the structuredClone.

@CendioOssman
Copy link

This is blocking our (@novnc) migration to web test runner, as we make heavy use of sinon. :/

Have you found any way to work around the issue?

@Sanderovich
Copy link
Author

@CendioOssman A quick work around would be to delete the actual property from the error thrown by the expect on failure and re-throw the error so web-test-runner can handle it.

it('stops working when errors', () => {
	const sinonSpy = spy();

	sinonSpy();

	try {
		expect(sinonSpy).to.be.calledWithMatch({ foo: 'bar' });
	} catch (e) {
		delete e.actual;
		throw e;
	}
});

@CendioOssman
Copy link

Thanks. That doesn't scale terribly well. And we have 765 such assertions. Any workaround would need to be somewhere more central. :/

@Sanderovich
Copy link
Author

Sanderovich commented Aug 6, 2024

For a central work around I would implement the work around in a Chai plugin:

import { use, Assertion } from 'chai';
import sinonChai from 'sinon-chai';

function overrideCallWithMatch() {
	Assertion.overwriteMethod('calledWithMatch', _super => {
		return function() {
			try {
				_super.apply(this, arguments)
			} catch (error) {
				delete error.actual;
				throw error;
			}
		}
	});
}

use(sinonChai);
use(overrideCallWithMatch);

Depending on for which Sinon Chai methods you want to implement the work around you can extend the plugin. Would this help you @CendioOssman ?

@lideen
Copy link

lideen commented Aug 21, 2024

I have the same issue. This is an issue with any value that structuredClone cannot handle. In my projects tests started failing for HTMLElements as well as they also cannot be cloned.

My current workaround is to lock @web/[email protected]

@rschristian
Copy link

rschristian commented Aug 25, 2024

I was running into this with sinon-chai but saw no errors in the browser console at all -- seemingly just a failed test that wasn't properly detected. Wasn't using calledWithMatch either.

Both workarounds (wrapper and pinning to @web/[email protected]) worked for me though.

@CendioOssman
Copy link

Depending on for which Sinon Chai methods you want to implement the work around you can extend the plugin. Would this help you @CendioOssman ?

Possibly. It is a bit annoying that you have to do it for a bunch of methods, though. But I supposed that can be optimized a bit.

My current workaround is to lock @web/[email protected]

This workaround does not work for me. Is there a regression in 0.7.2 that causes this problem?

@lideen
Copy link

lideen commented Sep 18, 2024

Depending on for which Sinon Chai methods you want to implement the work around you can extend the plugin. Would this help you @CendioOssman ?

Possibly. It is a bit annoying that you have to do it for a bunch of methods, though. But I supposed that can be optimized a bit.

My current workaround is to lock @web/[email protected]

This workaround does not work for me. Is there a regression in 0.7.2 that causes this problem?

I think it is this change that causes the problem: 4a4b699. To be more precise, the usage of structuredClone in packages/dev-server-core/src/web-sockets/webSocketsPlugin.ts

@CendioOssman
Copy link

Hmm.. Very odd that it didn't resolve the issue here for me in that case.

@lucaelin, is this issue something you are aware of and working on?

@lucaelin
Copy link
Contributor

Hey, thanks for bringing this second issue to my attention, I will see what I can do this weekend,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants