Skip to content

Instantly share code, notes, and snippets.

@Dobby89
Last active August 29, 2025 12:11
Show Gist options
  • Save Dobby89/b8274c9d31fee0f0058f9546a233d686 to your computer and use it in GitHub Desktop.
Save Dobby89/b8274c9d31fee0f0058f9546a233d686 to your computer and use it in GitHub Desktop.
Cypress snippets

Check how many requests were made to a particular endpoint

Yoinked from https://stackoverflow.com/a/55604505/5243574

cy.intercept({
  method: 'POST',
  url: 'myUrl',
}, {
  body: {}
}).as('myStubName');

cy.get('@myStubName.all').should('have.length', 0);

Stub Launch Darkly network calls

Yoinked from https://medium.com/@kutnickclose/how-to-use-cypress-with-launchdarkly-897349b7f976

// cypress\support\commands.js
Cypress.Commands.add('updateFeatureFlags', (featureFlags) => {
	// Turn off push (EventSource) updates from LaunchDarkly
	cy.intercept({ hostname: /.*clientstream.launchdarkly.com/ }, (req) => {
		const modifiedBody = {};
		Object.entries(featureFlags).forEach(
			([featureFlagName, featureFlagValue]) => {
				modifiedBody[featureFlagName] = { value: featureFlagValue };
			},
		);
		req.continue((res) => {
			req.reply({
				...res,
				body: JSON.stringify(modifiedBody),
			});
		});
	});

	// Ignore API calls to events endpoint
	cy.intercept({ hostname: /.*events.launchdarkly.com/ }, { body: {} });

	// Return feature flag values in format expected by launchdarkly client
	cy.intercept({ hostname: /.*app.launchdarkly.com/ }, (req) => {
		const body = {};
		Object.entries(featureFlags).forEach(
			([featureFlagName, featureFlagValue]) => {
				body[featureFlagName] = { value: featureFlagValue };
			},
		);
		req.reply({ body });
	});
});

// testfile.cy.js

beforeEach(() => {
  cy.updateFeatureFlags({ 'flag-name': true })
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment