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

Commit 04f1db4

Browse files
committed
feat: add plugins and support for react-scripts
1 parent a44405e commit 04f1db4

File tree

11 files changed

+1305
-2335
lines changed

11 files changed

+1305
-2335
lines changed

cypress.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"viewportWidth": 300,
3-
"viewportHeight": 100,
2+
"viewportWidth": 400,
3+
"viewportHeight": 400,
44
"video": false,
55
"projectId": "z9dxah",
66
"testFiles": "**/*spec.js",

cypress/component/component tests/advanced/react-book-example/src/Todos.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ it('Todo - should create snapshot', () => {
1616
/>,
1717
)
1818
cy.get('[data-testid=item]').should('have.length', 2)
19+
// disabled snapshot commands for now
20+
// to speed up bundling
1921
// let tree = component.toJSON();
2022
// expect(tree).toMatchSnapshot();
2123

cypress/component/component tests/basic/css/orange-button-spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ describe('Button', () => {
1212
.find('button')
1313
.should('have.css', 'background-color', 'rgb(245, 146, 62)')
1414

15-
cy.percySnapshot()
15+
// for now disabled Percy in support commands
16+
// to make the bundles smaller and faster
17+
// cy.percySnapshot()
1618
})
1719
})

cypress/component/component tests/basic/hello-x-spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ describe('HelloX component', () => {
1515
it('renders Unicode', () => {
1616
mount(<HelloX name="🌎" />)
1717
cy.contains('Hello 🌎!')
18-
cy.percySnapshot('Hello globe')
18+
// for now disabled Percy in support commands
19+
// to make the bundles smaller and faster
20+
// cy.percySnapshot('Hello globe')
1921
cy.wait(1000)
2022
})
2123
})

cypress/plugins/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require('path')
22
const webpack = require('@cypress/webpack-preprocessor')
33
const babelConfig = require('../../babel.config.js')
4-
const { initPlugin } = require('cypress-plugin-snapshots/plugin')
4+
// const { initPlugin } = require('cypress-plugin-snapshots/plugin')
55

66
// should we just reuse root webpack config?
77
const webpackOptions = {
@@ -41,6 +41,6 @@ const options = {
4141
module.exports = (on, config) => {
4242
on('file:preprocessor', webpack(options))
4343

44-
initPlugin(on, config)
44+
// initPlugin(on, config)
4545
return config
4646
}

cypress/support/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
// custom commands provided by this package, built from TypeScript code in "lib"
1717
// using "npm run transpile"
1818
import 'cypress-react-unit-test/hooks'
19-
import '@percy/cypress'
19+
// import '@percy/cypress'
2020
import '@testing-library/cypress/add-commands'
21-
import 'cypress-plugin-snapshots/commands'
21+
// import 'cypress-plugin-snapshots/commands'

package-lock.json

Lines changed: 1207 additions & 2324 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,18 @@
4040
"testing"
4141
],
4242
"author": "Gleb Bahmutov <[email protected]>",
43-
"license": "ISC",
43+
"license": "MIT",
44+
"dependencies": {
45+
"@cypress/webpack-preprocessor": "4.1.3",
46+
"debug": "4.1.1",
47+
"find-yarn-workspace-root": "1.2.1"
48+
},
4449
"devDependencies": {
4550
"@babel/core": "7.4.5",
4651
"@babel/plugin-proposal-class-properties": "7.4.4",
4752
"@babel/plugin-transform-modules-commonjs": "7.7.5",
4853
"@babel/preset-env": "7.4.5",
4954
"@babel/preset-react": "7.0.0",
50-
"@cypress/webpack-preprocessor": "1.1.3",
5155
"@material-ui/core": "4.9.5",
5256
"@material-ui/icons": "4.5.1",
5357
"@material-ui/lab": "4.0.0-alpha.39",
@@ -75,7 +79,7 @@
7579
"react-router": "6.0.0-alpha.1",
7680
"react-router-dom": "6.0.0-alpha.1",
7781
"semantic-release": "^17.0.4",
78-
"standard": "12.0.1",
82+
"standard": "14.3.3",
7983
"style-loader": "0.23.1",
8084
"styled-components": "5.0.0",
8185
"svg-url-loader": "3.0.3",

plugins/cra-v3/file-preprocessor.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const debug = require('debug')('cypress-react-unit-test')
2+
const path = require('path')
3+
const findYarnWorkspaceRoot = require('find-yarn-workspace-root')
4+
const webpack = require('@cypress/webpack-preprocessor')
5+
6+
const webpackConfigPath = path.resolve(
7+
findYarnWorkspaceRoot() || process.cwd(),
8+
'node_modules',
9+
'react-scripts',
10+
'config',
11+
'webpack.config.js',
12+
)
13+
14+
debug('path to react-scripts own webpack.config.js: %s', webpackConfigPath)
15+
16+
// Do this as the first thing so that any code reading it knows the right env.
17+
process.env.BABEL_ENV = 'development'
18+
process.env.NODE_ENV = 'development'
19+
20+
const webpackFactory = require(webpackConfigPath)
21+
const webpackOptions = webpackFactory('development')
22+
debug('webpack options: %o', webpackOptions)
23+
24+
// remove bunch of options, we just need to bundle spec files
25+
delete webpackOptions.optimization
26+
delete webpackOptions.plugins
27+
28+
// ESLint loader does not know about our "cy" global so it will error
29+
// find it in the module processing rules and add global "cy" option
30+
debug('module property %o', webpackOptions.module)
31+
32+
if (webpackOptions.module && Array.isArray(webpackOptions.module.rules)) {
33+
const modulePre = webpackOptions.module.rules.find(
34+
rule => rule.enforce === 'pre',
35+
)
36+
if (modulePre && Array.isArray(modulePre.use)) {
37+
debug('found Pre block %o', modulePre)
38+
39+
const useEslintLoader = modulePre.use.find(
40+
use => use.loader && use.loader.includes('eslint-loader'),
41+
)
42+
if (useEslintLoader) {
43+
debug('found useEslintLoader %o', useEslintLoader)
44+
45+
if (useEslintLoader.options) {
46+
if (Array.isArray(useEslintLoader.options.globals)) {
47+
debug(
48+
'adding cy to existing globals %o',
49+
useEslintLoader.options.globals,
50+
)
51+
useEslintLoader.options.globals.push('cy')
52+
useEslintLoader.options.globals.push('Cypress')
53+
} else {
54+
debug('setting new list of globals with cy and Cypress')
55+
useEslintLoader.options.globals = ['cy', 'Cypress']
56+
}
57+
}
58+
}
59+
}
60+
}
61+
62+
const options = {
63+
webpackOptions,
64+
watchOptions: {},
65+
}
66+
67+
module.exports = () => {
68+
return webpack(options)
69+
}

plugins/cra-v3/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const filePreprocessor = require('./file-preprocessor')
2+
3+
module.exports = on => {
4+
on('file:preprocessor', filePreprocessor())
5+
}

support/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// should be loaded from cypress.json as a support file
2+
// "supportFile": "node_modules/cypress-react-unit-test/support"
3+
import 'cypress-react-unit-test/hooks'

0 commit comments

Comments
 (0)