gatsby-cypress-endpoints v0.0.6
gatsby-cypress-endpoints
A Cypress plugin for generating Gatsby endpoints.
This plugin gives you the ability to dynamically generate tests, which is useful for performing routine checks across your Gatsby site.
The following assumptions are made:
- Tests are being run within a Gatsby (v2) project in development mode
- Cypress has been configured within your Gatsby project (guide)
- Fixtures are stored in the following directory
<rootDir>/cypress/fixtures/
Installation
npm install gatsby-cypress-endpoints --save-dev
Configuration
Copy and paste the following code in your project's <rootDir>/cypress/plugins/index.js
:
const { generateEndpoints } = require("gatsby-cypress-endpoints");
module.exports = (on, config) => {
generateEndpoints(on, config);
return config;
};
Usage
When you next run Cypress, endpoints are stored as an environment variable, accessible from the Cypress config env
object.
Endpoints are also stored as a fixture in <rootDir>/cypress/fixtures/endpoints.js
.
Environment variables can be accessed using:
Cypress.env("endpoints");
Fixtures can be accessed using:
cy.fixture("../fixtures/endpoints");
NOTE: Certain endpoints may be cached from previous builds. To ensure all endpoints are valid, make sure to clean the cache before each test run.
Examples
For running an accessibility audit (with cypress-axe) across your site, you may use the following approach:
Using environment variables (multiple grouped tests):
describe("Accessibility checks", () => {
Cypress.env("endpoints").forEach(endpoint => {
it(endpoint, () => {
cy.visit(endpoint).then(() => {
cy.wait(500);
cy.injectAxe();
cy.checkA11y();
});
});
});
});
Using fixtures (a single test):
it("Check Accessibility", () => {
cy.fixture("../fixtures/endpoints").then(endpoints => {
endpoints.forEach(endpoint => {
cy.visit(endpoint).then(() => {
cy.wait(500);
cy.injectAxe();
cy.checkA11y();
});
});
});
});
NOTE:
cy
commands cannot be called outside of a running test, therefore the approach to use environment variables vs fixtures is entirely dependent on the end-goal of the test.