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

How can I mock SignalR only for SOME Cypress tests? #9

Open
dstj opened this issue Sep 26, 2024 · 3 comments
Open

How can I mock SignalR only for SOME Cypress tests? #9

dstj opened this issue Sep 26, 2024 · 3 comments

Comments

@dstj
Copy link

dstj commented Sep 26, 2024

I have end-to-end Cypress tests that actually use the full backend with the real SignalR connection. Using your standard initialization like this:

const cypressSignalRMock = (useCypressSignalRMock('hubMock') as unknown) as signalR.HubConnection;
this.hubConnection = cypressSignalRMock || new signalR.HubConnectionBuilder()
	.withUrl(url, { accessTokenFactory })
...

always result in the mock being created as long as Cypress is running. This is because of these lines, correct?

How can I control the mock creation from each individual Cypress test so that it's the test that decides if SignalR is mocked or not?

That way, I could do something like this is my app code:

this.hubConnection = window['cypress-signalr-mock'] || new signalR.HubConnectionBuilder()
	.withUrl(url, { accessTokenFactory })
...

That would also remove the app's dependency on `Cypress-SignalR-Mock`, which is also something that I would like to avoid having.
@JasonLandbridge
Copy link
Owner

Hey there!

That would be a nice feature idea!

Using this example from the README:

const hubConnection = useCypressSignalRMock('testHub') ?? 
new HubConnectionBuilder().withUrl(`http://localhost:3000/testhub`).build();

you could do something like this in the test:

Cypress.env('ENABLE_SIGNALR_MOCK', true);

Then where you define the connection:

const hubConnection = Cypress.env('ENABLE_SIGNALR_MOCK') ? useCypressSignalRMock('testHub') : 
new HubConnectionBuilder().withUrl(`http://localhost:3000/testhub`).build()

This should work but is a bit messy, I will think about a way to implement this natively in the plugin.

That would also remove the app's dependency on Cypress-SignalR-Mock, which is also something that I would like to avoid having.

Why would you want to remove the dependency?

@dstj
Copy link
Author

dstj commented Sep 26, 2024

Thanks for the quick response. I'll try that, but I agree, it is a bit messy ;)

Why would you want to remove the dependency?

My Cypress project and app are two distinct projects. Having Cypress dependencies in both feels wrong. Shouldn't production code try to minimize the test library dependencies. This will reduce the bundle size, etc. By the way, the doc says --save-dev, but the playground example has it as a full-fledged dependency) make it's way into production?

I'm not the tree shaking expert, but doesn't having a useCypressSignalRMock call right in the production code automatically makes that not a dev dependency?

@JasonLandbridge
Copy link
Owner

My Cypress project and app are two distinct projects. Having Cypress dependencies in both feels wrong. Shouldn't production code try to minimize the test library dependencies. This will reduce the bundle size, etc.

Yeah that is a good point.

By the way, the doc says --save-dev, but the playground example has it as a full-fledged dependency) make it's way into production?

I'm not the tree shaking expert, but doesn't having a useCypressSignalRMock call right in the production code automatically makes that not a dev dependency?

Hmmm, I'm honestly not sure... If you find out then let me know cause that shouldn't happen

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

2 participants