A Cypress command for navigating via the History API during tests; an alternative to cy.visit
for manually navigating within single page applications.
Uses window.history.pushState
by default, but you can make the helper use a history
instance as well.
npm install --save-dev cypress-helper-history
// E.g. in cypress/support/index.js
import 'cypress-helper-history';
// E.g. in src/history.js
// Create your history instance, for example like this:
import { createBrowserHistory as createHistory } from 'history';
const history = createHistory();
export default history;
// Then expose it to the helper like this:
if ('Cypress' in window) {
window.__chh__history__ = history;
}
describe('cypress-helper-navigate', () => {
before(() => {
cy.visit('/');
});
it('can do client-side navigation', () => {
cy.location('pathname').should('equal', '/');
cy.navigate('/example');
cy.location('pathname').should('equal', '/example');
});
it('supports log option', () => {
cy.navigate('/not-logged', undefined, { log: false });
});
it('supports passing in state', () => {
cy.navigate('/with-state', { foo: 'bar' });
});
});