This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["@babel/preset-env", "@babel/preset-react"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# example: webpack5-file | ||
|
||
> Component tests for projects using existing [webpack.config.js](webpack.config.js) file and [Webpack 5](https://webpack.js.org/blog/2020-10-10-webpack-5-release/) | ||
## Usage | ||
|
||
1. Make sure the root project has been built . | ||
|
||
```bash | ||
# in the root of the project | ||
npm install | ||
npm run build | ||
``` | ||
|
||
2. Run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency. | ||
|
||
```bash | ||
# in this folder | ||
npm install | ||
``` | ||
|
||
3. Start Cypress | ||
|
||
```bash | ||
npm run cy:open | ||
# or just run headless tests | ||
npm test | ||
``` | ||
|
||
## Notes | ||
|
||
See tests in the [cypress/component](cypress/component) folder. We also allow tests to load components using a [webpack alias](webpack.config.js) from `more-components/src` folder using `import Hello from '@components/Hello'`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"fixturesFolder": false, | ||
"testFiles": "**/*cy-spec.js", | ||
"viewportWidth": 500, | ||
"viewportHeight": 500, | ||
"experimentalComponentTesting": true | ||
} |
20 changes: 20 additions & 0 deletions
20
examples/webpack5-file/cypress/component/ChildComponent.cy-spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/// <reference types="cypress" /> | ||
import React from 'react' | ||
import { mount } from 'cypress-react-unit-test' | ||
import ChildComponent from './ChildComponent' | ||
import * as calc from './calc' | ||
|
||
describe('ChildComponent unstubbed', () => { | ||
it('works', () => { | ||
cy.spy(calc, 'getRandomNumber').as('getRandomNumber') | ||
mount(<ChildComponent />) | ||
// make sure the component shows the random value | ||
// returned by the calc.getRandomNumber function | ||
cy.get('@getRandomNumber') | ||
.should('have.been.called') | ||
.its('returnValues.0') | ||
.then(n => { | ||
cy.contains('.random', n) | ||
}) | ||
}) | ||
}) |
10 changes: 10 additions & 0 deletions
10
examples/webpack5-file/cypress/component/ChildComponent.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React from 'react' | ||
import { getRandomNumber } from './calc' | ||
|
||
const ChildComponent = () => ( | ||
<div> | ||
Child component <p className="random">Random number {getRandomNumber()}</p> | ||
</div> | ||
) | ||
|
||
export default ChildComponent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// <reference types="cypress" /> | ||
import React from 'react' | ||
import { mount } from 'cypress-react-unit-test' | ||
// use path alias defined in webpack config | ||
import Hello from '@components/Hello' | ||
|
||
describe('Hello using path alias', () => { | ||
it('works', () => { | ||
mount(<Hello />) | ||
cy.contains('Hello!').should('be.visible') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/// <reference types="cypress" /> | ||
import React from 'react' | ||
import { mount } from 'cypress-react-unit-test' | ||
import ParentComponent from './ParentComponent' | ||
import * as calc from './calc' | ||
import * as ChildComponent from './ChildComponent' | ||
|
||
describe('Mocking', () => { | ||
it('named getRandomNumber imported in the child component', () => { | ||
cy.stub(calc, 'getRandomNumber') | ||
.as('lucky') | ||
.returns(777) | ||
mount(<ParentComponent />) | ||
cy.contains('.random', '777') | ||
}) | ||
|
||
it('entire child component exported as default', () => { | ||
cy.stub(ChildComponent, 'default') | ||
.as('child') | ||
.returns(<div className="mock-child">Mock child component</div>) | ||
mount(<ParentComponent />) | ||
cy.contains('.mock-child', 'Mock child component') | ||
}) | ||
}) |
12 changes: 12 additions & 0 deletions
12
examples/webpack5-file/cypress/component/ParentComponent.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react' | ||
import ChildComponent from './ChildComponent' | ||
|
||
const ParentComponent = () => ( | ||
<div> | ||
Parent component, child component below | ||
<br /> | ||
<ChildComponent /> | ||
</div> | ||
) | ||
|
||
export default ParentComponent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/// <reference types="cypress" /> | ||
import React from 'react' | ||
import { mount } from 'cypress-react-unit-test' | ||
import Test from './Test' | ||
|
||
describe('components', () => { | ||
it('works', () => { | ||
mount(<Test />) | ||
cy.contains('Text') | ||
}) | ||
|
||
it('works with plain div', () => { | ||
mount(<div>Text</div>) | ||
cy.contains('Text') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from 'react' | ||
|
||
const Test = () => <div>Text</div> | ||
|
||
export default Test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const getRandomNumber = () => Math.round(Math.random() * 1000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/// <reference types="cypress" /> | ||
describe('integration spec', () => { | ||
it('works', () => { | ||
expect(1).to.equal(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// load file preprocessor that comes with this plugin | ||
// https://github.com/bahmutov/cypress-react-unit-test#install | ||
module.exports = (on, config) => { | ||
// from the root of the project (folder with cypress.json file) | ||
config.env.webpackFilename = 'webpack.config.js' | ||
require('cypress-react-unit-test/plugins/load-webpack')(on, config) | ||
// IMPORTANT to return the config object | ||
// with the any changed environment variables | ||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require('cypress-react-unit-test/dist/hooks') | ||
// if we need code coverage, need to include its custom support hook | ||
require('@cypress/code-coverage/support') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from 'react' | ||
|
||
const Hello = () => <div>Hello!</div> | ||
|
||
export default Hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "example-webpack-options", | ||
"description": "Using default Webpack options to transpile simple tests", | ||
"private": true, | ||
"scripts": { | ||
"test": "cypress-expect run --passing 7", | ||
"cy:open": "cypress open", | ||
"check-coverage": "check-coverage Test.js calc.js ParentComponent.js ChildComponent.js", | ||
"only-covered": "only-covered Test.js calc.js ParentComponent.js ChildComponent.js more-components/src/Hello.js" | ||
}, | ||
"devDependencies": { | ||
"babel-loader": "8.1.0", | ||
"check-code-coverage": "1.10.0", | ||
"cypress": "5.3.0", | ||
"cypress-expect": "2.1.0", | ||
"cypress-react-unit-test": "file:../..", | ||
"react": "^16.13.1", | ||
"react-dom": "^16.13.1", | ||
"webpack": "5.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const path = require('path') | ||
|
||
module.exports = { | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.(js|jsx)$/, | ||
exclude: /node_modules/, | ||
use: { | ||
loader: 'babel-loader', | ||
}, | ||
}, | ||
], | ||
}, | ||
// add alias to load "@components" from another folder | ||
// so that tests in cypress/component can load components using | ||
// import X from '@components/X' | ||
// see https://glebbahmutov.com/blog/using-ts-aliases-in-cypress-tests/ | ||
resolve: { | ||
extensions: ['.js'], | ||
alias: { | ||
'@components': path.resolve(__dirname, 'more-components', 'src'), | ||
}, | ||
}, | ||
} |