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

Commit

Permalink
feat: add plugins and support for react-scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Apr 3, 2020
1 parent a44405e commit 04f1db4
Show file tree
Hide file tree
Showing 11 changed files with 1,305 additions and 2,335 deletions.
4 changes: 2 additions & 2 deletions cypress.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"viewportWidth": 300,
"viewportHeight": 100,
"viewportWidth": 400,
"viewportHeight": 400,
"video": false,
"projectId": "z9dxah",
"testFiles": "**/*spec.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ it('Todo - should create snapshot', () => {
/>,
)
cy.get('[data-testid=item]').should('have.length', 2)
// disabled snapshot commands for now
// to speed up bundling
// let tree = component.toJSON();
// expect(tree).toMatchSnapshot();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe('Button', () => {
.find('button')
.should('have.css', 'background-color', 'rgb(245, 146, 62)')

cy.percySnapshot()
// for now disabled Percy in support commands
// to make the bundles smaller and faster
// cy.percySnapshot()
})
})
4 changes: 3 additions & 1 deletion cypress/component/component tests/basic/hello-x-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ describe('HelloX component', () => {
it('renders Unicode', () => {
mount(<HelloX name="🌎" />)
cy.contains('Hello 🌎!')
cy.percySnapshot('Hello globe')
// for now disabled Percy in support commands
// to make the bundles smaller and faster
// cy.percySnapshot('Hello globe')
cy.wait(1000)
})
})
4 changes: 2 additions & 2 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path')
const webpack = require('@cypress/webpack-preprocessor')
const babelConfig = require('../../babel.config.js')
const { initPlugin } = require('cypress-plugin-snapshots/plugin')
// const { initPlugin } = require('cypress-plugin-snapshots/plugin')

// should we just reuse root webpack config?
const webpackOptions = {
Expand Down Expand Up @@ -41,6 +41,6 @@ const options = {
module.exports = (on, config) => {
on('file:preprocessor', webpack(options))

initPlugin(on, config)
// initPlugin(on, config)
return config
}
4 changes: 2 additions & 2 deletions cypress/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
// custom commands provided by this package, built from TypeScript code in "lib"
// using "npm run transpile"
import 'cypress-react-unit-test/hooks'
import '@percy/cypress'
// import '@percy/cypress'
import '@testing-library/cypress/add-commands'
import 'cypress-plugin-snapshots/commands'
// import 'cypress-plugin-snapshots/commands'
3,531 changes: 1,207 additions & 2,324 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@
"testing"
],
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>",
"license": "ISC",
"license": "MIT",
"dependencies": {
"@cypress/webpack-preprocessor": "4.1.3",
"debug": "4.1.1",
"find-yarn-workspace-root": "1.2.1"
},
"devDependencies": {
"@babel/core": "7.4.5",
"@babel/plugin-proposal-class-properties": "7.4.4",
"@babel/plugin-transform-modules-commonjs": "7.7.5",
"@babel/preset-env": "7.4.5",
"@babel/preset-react": "7.0.0",
"@cypress/webpack-preprocessor": "1.1.3",
"@material-ui/core": "4.9.5",
"@material-ui/icons": "4.5.1",
"@material-ui/lab": "4.0.0-alpha.39",
Expand Down Expand Up @@ -75,7 +79,7 @@
"react-router": "6.0.0-alpha.1",
"react-router-dom": "6.0.0-alpha.1",
"semantic-release": "^17.0.4",
"standard": "12.0.1",
"standard": "14.3.3",
"style-loader": "0.23.1",
"styled-components": "5.0.0",
"svg-url-loader": "3.0.3",
Expand Down
69 changes: 69 additions & 0 deletions plugins/cra-v3/file-preprocessor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const debug = require('debug')('cypress-react-unit-test')
const path = require('path')
const findYarnWorkspaceRoot = require('find-yarn-workspace-root')
const webpack = require('@cypress/webpack-preprocessor')

const webpackConfigPath = path.resolve(
findYarnWorkspaceRoot() || process.cwd(),
'node_modules',
'react-scripts',
'config',
'webpack.config.js',
)

debug('path to react-scripts own webpack.config.js: %s', webpackConfigPath)

// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'development'
process.env.NODE_ENV = 'development'

const webpackFactory = require(webpackConfigPath)
const webpackOptions = webpackFactory('development')
debug('webpack options: %o', webpackOptions)

// remove bunch of options, we just need to bundle spec files
delete webpackOptions.optimization
delete webpackOptions.plugins

// ESLint loader does not know about our "cy" global so it will error
// find it in the module processing rules and add global "cy" option
debug('module property %o', webpackOptions.module)

if (webpackOptions.module && Array.isArray(webpackOptions.module.rules)) {
const modulePre = webpackOptions.module.rules.find(
rule => rule.enforce === 'pre',
)
if (modulePre && Array.isArray(modulePre.use)) {
debug('found Pre block %o', modulePre)

const useEslintLoader = modulePre.use.find(
use => use.loader && use.loader.includes('eslint-loader'),
)
if (useEslintLoader) {
debug('found useEslintLoader %o', useEslintLoader)

if (useEslintLoader.options) {
if (Array.isArray(useEslintLoader.options.globals)) {
debug(
'adding cy to existing globals %o',
useEslintLoader.options.globals,
)
useEslintLoader.options.globals.push('cy')
useEslintLoader.options.globals.push('Cypress')
} else {
debug('setting new list of globals with cy and Cypress')
useEslintLoader.options.globals = ['cy', 'Cypress']
}
}
}
}
}

const options = {
webpackOptions,
watchOptions: {},
}

module.exports = () => {
return webpack(options)
}
5 changes: 5 additions & 0 deletions plugins/cra-v3/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const filePreprocessor = require('./file-preprocessor')

module.exports = on => {
on('file:preprocessor', filePreprocessor())
}
3 changes: 3 additions & 0 deletions support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// should be loaded from cypress.json as a support file
// "supportFile": "node_modules/cypress-react-unit-test/support"
import 'cypress-react-unit-test/hooks'

0 comments on commit 04f1db4

Please sign in to comment.