Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Add support for component tests #138

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
]
}
10 changes: 10 additions & 0 deletions cypress-component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"testFiles": "*.{md,js}",
"experimentalComponentTesting": true,
"integrationFolder": "cypress/empty",
"supportFile": "cypress/support/components.js",
"$schema": "https://on.cypress.io/cypress.schema.json",
"env": {
"coverage": false
}
}
3 changes: 2 additions & 1 deletion cypress-markdown.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"testFiles": "*.md"
"testFiles": "*.md",
"$schema": "https://on.cypress.io/cypress.schema.json"
}
17 changes: 17 additions & 0 deletions cypress/component/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { testExamples } from '../..'
import { source } from 'common-tags'
const tests = [
{
name: 'mount Hello component',
description: 'Without passing any props',
// maybe we could move the common code here
// into the support file?
// for now, no JSX
test: source`
const hello = React.createElement('div', null, 'Hello')
mount(hello)
cy.contains('Hello').should('be.visible')
`,
},
]
testExamples(tests)
10 changes: 10 additions & 0 deletions cypress/component/hello.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Hello component

<!-- fiddle Hello -->
```js
const {mount} = require('cypress-react-unit-test')
const Hello = () => <div>Hello</div>
mount(<Hello />)
cy.contains('Hello')
```
<!-- fiddle-end -->
20 changes: 20 additions & 0 deletions cypress/component/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference types="cypress" />

// note: React and mount where set as globals in the support file
describe('simple', () => {
it('has the right spec type', () => {
expect(Cypress.spec).to.have.property('specType', 'component')
})

it('mounts the component', () => {
const hello = React.createElement('div', null, 'Hello')
mount(hello)
cy.contains('Hello').should('be.visible')
})

it('mounts the component using JSX', () => {
const Hello = () => <div>Hello</div>
mount(<Hello />)
cy.contains('Hello').should('be.visible')
})
})
1 change: 1 addition & 0 deletions cypress/empty/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Empty on purpose
2 changes: 1 addition & 1 deletion cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const mdPreprocessor = require('../../src/markdown-preprocessor')
const { mdPreprocessor } = require('../../src/markdown-preprocessor')

module.exports = (on, config) => {
// find and run tests in JS or Markdown files
Expand Down
7 changes: 7 additions & 0 deletions cypress/support/components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const React = require('react')
const { mount } = require('cypress-react-unit-test')

globalThis.React = React
globalThis.mount = mount

require('cypress-react-unit-test/support')
Loading