Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 7201f6a

Browse files
authored
feat: throw an error if a component spec calls cy.visit (#287)
1 parent 6e4fd6f commit 7201f6a

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Spec | Description
121121
[window-spec.js](cypress/component/basic/window-spec.js) | In the component test, the spec `window` and the application's `window` where the component is running should be the same object
122122
[css](cypress/component/basic/css) | Shows that component with `import './Button.css'` works
123123
[network](cypress/component/basic/network) | Confirms we can use `cy.route` to stub / spy on component's network calls
124+
[no-visit](cypress/component/basic/no-visit) | Component specs cannot call `cy.visit`
124125
[react-book-by-chris-noring](cypress/component/basic/react-book-by-chris-noring) | Copied test examples from [React Book](https://softchris.github.io/books/react) and adapted for Cypress component tests
125126
[react-tutorial](cypress/component/basic/react-tutorial) | Tests from official [ReactJS tutorial](https://reactjs.org/tutorial/tutorial.html) copied and adapted for Cypress component tests
126127
[stub-example](cypress/component/basic/stub-example) | Uses `cy.stub` as component props
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference types="cypress" />
2+
describe('Trying to use cy.visit in component spec', () => {
3+
it('throws an error', () => {
4+
// https://github.com/bahmutov/cypress-react-unit-test/issues/286
5+
expect(() => {
6+
cy.visit('index.html')
7+
}).to.throw
8+
})
9+
})

cypress/integration/integration tests/integration-spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/// <reference types="cypress" />
2+
const React = require('react')
3+
const { mount } = require('../../..')
4+
25
describe('integration tests', () => {
36
it('loads page for E2E', () => {
47
cy.visit('index.html')
@@ -9,4 +12,11 @@ describe('integration tests', () => {
912
cy.visit('index.html')
1013
cy.window().should('have.property', 'React')
1114
})
15+
16+
it('throws an Error if I try to use mount', () => {
17+
cy.log('About to try using *mount*')
18+
expect(() => {
19+
mount(<div>Example</div>)
20+
}).to.throw
21+
})
1222
})

lib/hooks.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
// @ts-ignore
22
const isComponentSpec = () => Cypress.spec.specType === 'component'
33

4+
// When running component specs, we cannot allow "cy.visit"
5+
// because it will wipe out our preparation work, and does not make much sense
6+
// thus we overwrite "cy.visit" to throw an error
7+
Cypress.Commands.overwrite('visit', (visit, ...args: any[]) => {
8+
if (isComponentSpec()) {
9+
throw new Error(
10+
'cy.visit from a component spec is not allowed\n' +
11+
'see https://github.com/bahmutov/cypress-react-unit-test/issues/286',
12+
)
13+
} else {
14+
// allow regular visit to proceed
15+
return visit(...args)
16+
}
17+
})
18+
419
/** Initialize an empty document with root element */
520
function renderTestingPlatform() {
621
// Let's mount components under a new div with this id

0 commit comments

Comments
 (0)