Skip to content

Commit da4b1e0

Browse files
author
Barthélémy Ledoux
authored
feat: allow usage of @react/plugins with cypress.config.js (#17738)
1 parent 36be9eb commit da4b1e0

File tree

42 files changed

+397
-126
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+397
-126
lines changed

cli/types/cypress.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5263,7 +5263,7 @@ declare namespace Cypress {
52635263
tag?: string
52645264
}
52655265

5266-
interface DevServerOptions {
5266+
interface DevServerConfig {
52675267
specs: Spec[]
52685268
config: ResolvedConfigOptions & RuntimeConfigOptions
52695269
devServerEvents: NodeJS.EventEmitter
@@ -5282,7 +5282,7 @@ declare namespace Cypress {
52825282
(action: 'before:spec', fn: (spec: Spec) => void | Promise<void>): void
52835283
(action: 'before:browser:launch', fn: (browser: Browser, browserLaunchOptions: BrowserLaunchOptions) => void | BrowserLaunchOptions | Promise<BrowserLaunchOptions>): void
52845284
(action: 'file:preprocessor', fn: (file: FileObject) => string | Promise<string>): void
5285-
(action: 'dev-server:start', fn: (file: DevServerOptions) => Promise<ResolvedDevServerConfig>): void
5285+
(action: 'dev-server:start', fn: (file: DevServerConfig) => Promise<ResolvedDevServerConfig>): void
52865286
(action: 'task', tasks: Tasks): void
52875287
}
52885288

npm/react/examples/a11y/cypress/plugins/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
// load file preprocessor that comes with this plugin
1+
// @ts-check
2+
3+
// load file devServer that comes with this plugin
24
// https://github.com/bahmutov/cypress-react-unit-test#install
3-
const preprocessor = require('@cypress/react/plugins/react-scripts')
5+
const devServer = require('@cypress/react/plugins/react-scripts')
46

7+
/**
8+
* @type Cypress.PluginConfig
9+
*/
510
module.exports = (on, config) => {
6-
preprocessor(on, config)
11+
devServer(on, config)
712

813
// IMPORTANT to return the config object
914
// with the any changed environment variables

npm/react/examples/craco/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ Use the plugin:
77

88
// import your craco.config.js
99
const cracoConfig = require('../../craco.config.js')
10-
const injectDevServer = require('@cypress/react/plugins/craco')
10+
const devServer = require('@cypress/react/plugins/craco')
1111

1212
module.exports = (on, config) => {
13-
injectDevServer(on, config, cracoConfig)
13+
devServer(on, config, cracoConfig)
1414

1515
return config
1616
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
// @ts-check
2+
13
const cracoConfig = require('../../craco.config.js')
2-
const injectDevServer = require('@cypress/react/plugins/craco')
4+
const devServer = require('@cypress/react/plugins/craco')
35

6+
/**
7+
* @type Cypress.PluginConfig
8+
*/
49
module.exports = (on, config) => {
5-
injectDevServer(on, config, cracoConfig)
10+
devServer(on, config, cracoConfig)
611

712
return config
813
}

npm/react/examples/find-webpack/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ Here we are adding some Component Testing specific options, hence the `"componen
3838
The last thing we need to is tell Cypress to use `@cypress/webpack-dev-server` for component tests. Plugins are explained in detail in the [Cypress documentation](https://docs.cypress.io/guides/tooling/plugins-guide#Installing-plugins). By default plugins are loaded from `cypress/plugins/index.js`. Create that file and add:
3939

4040
```js
41-
const injectDevServer = require("@cypress/react/plugins/react-scripts")
41+
const devServer = require("@cypress/react/plugins/react-scripts")
4242

4343
module.exports = (on, config) => {
44-
injectDevServer(on, config)
44+
devServer(on, config)
4545
return config
4646
}
4747
```

npm/react/examples/find-webpack/cypress/plugins/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
// @ts-check
2+
13
const findReactScriptsWebpackConfig = require('@cypress/react/plugins/react-scripts/findReactScriptsWebpackConfig')
24
const { startDevServer } = require('@cypress/webpack-dev-server')
35
const _ = require('lodash')
46

7+
/**
8+
* @type Cypress.PluginConfig
9+
*/
510
module.exports = (on, config) => {
611
const map = _.map([4, 8], (n) => n * 2)
712

@@ -10,9 +15,9 @@ module.exports = (on, config) => {
1015
const webpackConfig = findReactScriptsWebpackConfig(config)
1116

1217
const rules = webpackConfig.module.rules.find((rule) => !!rule.oneOf).oneOf
13-
const babelRule = rules.find((rule) => /babel-loader/.test(rule.loader))
18+
const babelRule = rules.find((rule) => typeof rule.loader === 'string' && /babel-loader/.test(rule.loader))
1419

15-
babelRule.options.plugins.push(require.resolve('babel-plugin-istanbul'))
20+
typeof babelRule.options !== 'string' && babelRule.options.plugins.push(require.resolve('babel-plugin-istanbul'))
1621

1722
on('dev-server:start', (options) => {
1823
return startDevServer({ options, webpackConfig })
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const injectNextDevServer = require('@cypress/react/plugins/next')
1+
const devServer = require('@cypress/react/plugins/next')
22

33
/**
44
* @type {Cypress.PluginConfig}
55
*/
66
module.exports = (on, config) => {
7-
injectNextDevServer(on, config)
7+
devServer(on, config)
88

99
return config
1010
}

npm/react/examples/nextjs/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
In order to reuse next's webpack configuration and all the custom configuration defined in `next.config.js` connect special plugin in [plugin file](./cypress/plugins/index.js)
1010

1111
```js
12-
const preprocessor = require('@cypress/react/plugins/next')
12+
const devServer = require('@cypress/react/plugins/next')
1313

1414
module.exports = (on, config) => {
15-
preprocessor(on, config)
15+
devServer(on, config)
1616

1717
return config
1818
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const injectNextDevServer = require('@cypress/react/plugins/next')
1+
const devServer = require('@cypress/react/plugins/next')
22

33
/**
44
* @type {Cypress.PluginConfig}
55
*/
66
module.exports = (on, config) => {
7-
injectNextDevServer(on, config)
7+
devServer(on, config)
88

99
return config
1010
}

npm/react/examples/react-scripts-folder/cypress/plugins/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
// load file preprocessor that comes with this plugin
1+
// @ts-check
2+
3+
// load file devServer that comes with this plugin
24
// https://github.com/bahmutov/cypress-react-unit-test#install
3-
const preprocessor = require('@cypress/react/plugins/react-scripts')
5+
const devServer = require('@cypress/react/plugins/react-scripts')
46

7+
/**
8+
* @type {Cypress.PluginConfig}
9+
*/
510
module.exports = (on, config) => {
6-
preprocessor(on, config)
11+
devServer(on, config)
712

813
// IMPORTANT to return the config object
914
// with the any changed environment variables
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
const injectDevServer = require('@cypress/react/plugins/react-scripts')
1+
const devServer = require('@cypress/react/plugins/react-scripts')
22

3+
/**
4+
* @type {Cypress.PluginConfig}
5+
*/
36
module.exports = (on, config) => {
4-
injectDevServer(on, config)
7+
devServer(on, config)
58

69
return config
710
}

npm/react/examples/react-scripts/cypress/plugins/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
// load file preprocessor that comes with this plugin
1+
// @ts-check
2+
3+
// load file devServer that comes with this plugin
24
// https://github.com/bahmutov/cypress-react-unit-test#install
3-
const preprocessor = require('@cypress/react/plugins/react-scripts')
5+
const devServer = require('@cypress/react/plugins/react-scripts')
46

7+
/**
8+
* @type {Cypress.PluginConfig}
9+
*/
510
module.exports = (on, config) => {
6-
preprocessor(on, config)
11+
devServer(on, config)
712

813
// IMPORTANT to return the config object
914
// with the any changed environment variables

npm/react/examples/sass-and-ts/cypress/plugins/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
// load Webpack file preprocessor that comes with this plugin
1+
// @ts-check
2+
3+
// load Webpack file devServer that comes with this plugin
24
// https://github.com/bahmutov/cypress-react-unit-test#install
3-
const injectWebpackDevServer = require('@cypress/react/plugins/load-webpack')
5+
const devServer = require('@cypress/react/plugins/load-webpack')
46

7+
/**
8+
* @type {Cypress.PluginConfig}
9+
*/
510
module.exports = (on, config) => {
6-
injectWebpackDevServer(on, config, {
11+
devServer(on, config, {
712
webpackFilename: 'webpack.config.js',
813
})
914

npm/react/examples/snapshots/cypress/plugins/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
// load file preprocessor that comes with this plugin
1+
// @ts-check
2+
3+
// load file devServer that comes with this plugin
24
// https://github.com/bahmutov/cypress-react-unit-test#install
3-
const preprocessor = require('@cypress/react/plugins/react-scripts')
5+
const devServer = require('@cypress/react/plugins/react-scripts')
46
const { initPlugin: initSnapshots } = require('cypress-plugin-snapshots/plugin')
57

8+
/**
9+
* @type {Cypress.PluginConfig}
10+
*/
611
module.exports = (on, config) => {
7-
preprocessor(on, config)
12+
devServer(on, config)
813
// initialize the snapshots plugin following
914
// https://github.com/meinaart/cypress-plugin-snapshots
1015
initSnapshots(on, config)

npm/react/examples/tailwind/cypress/plugins/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
// load file preprocessor that comes with this plugin
1+
// @ts-check
2+
3+
// load file devServer that comes with this plugin
24
// https://github.com/bahmutov/cypress-react-unit-test#install
3-
const injectReactScriptsDevServer = require('@cypress/react/plugins/react-scripts')
5+
const devServer = require('@cypress/react/plugins/react-scripts')
46

7+
/**
8+
* @type {Cypress.PluginConfig}
9+
*/
510
module.exports = (on, config) => {
6-
injectReactScriptsDevServer(on, config)
11+
devServer(on, config)
712

813
// IMPORTANT to return the config object
914
// with the any changed environment variables

npm/react/examples/using-babel-typescript/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ npm test
3131

3232
## Specs
3333

34-
See spec files [src/\*.spec.js](src). The specs are bundled using [babel.config.js](babel.config.js) settings via [cypress/plugins/index.js](cypress/plugins/index.js) file that includes file preprocessor.
34+
See spec files [src/\*.spec.js](src). The specs are bundled using [babel.config.js](babel.config.js) settings via [cypress/plugins/index.js](cypress/plugins/index.js) file that includes file devServer.
3535

3636
```js
3737
// let's bundle spec files and the components they include using
3838
// the same bundling settings as the project by loading .babelrc
39-
const preprocessor = require('@cypress/react/plugins/babelrc')
39+
const devServer = require('@cypress/react/plugins/babel')
4040
module.exports = (on, config) => {
41-
preprocessor(on, config)
41+
devServer(on, config)
4242
// IMPORTANT to return the config object
4343
// with the any changed environment variables
4444
return config

npm/react/examples/using-babel-typescript/cypress/plugins/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
// @ts-check
2+
13
// let's bundle spec files and the components they include using
24
// the same bundling settings as the project by loading .babelrc
35
// https://github.com/bahmutov/cypress-react-unit-test#install
4-
const injectWebpackWithBabelDevServer = require('@cypress/react/plugins/babel')
6+
const devServer = require('@cypress/react/plugins/babel')
57

8+
/**
9+
* @type {Cypress.PluginConfig}
10+
*/
611
module.exports = (on, config) => {
7-
injectWebpackWithBabelDevServer(on, config)
12+
devServer(on, config)
813

914
// IMPORTANT to return the config object
1015
// with the any changed environment variables

npm/react/examples/using-babel/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ npm test
3131

3232
## Specs
3333

34-
See spec files [src/\*.spec.js](src). The specs are bundled using [.babelrc](.babelrc) settings via [cypress/plugins/index.js](cypress/plugins/index.js) file that includes file preprocessor
34+
See spec files [src/\*.spec.js](src). The specs are bundled using [.babelrc](.babelrc) settings via [cypress/plugins/index.js](cypress/plugins/index.js) file that includes file devServer
3535

3636
```js
3737
// let's bundle spec files and the components they include using
3838
// the same bundling settings as the project by loading .babelrc
39-
const preprocessor = require('@cypress/react/plugins/babelrc')
39+
const devServer = require('@cypress/react/plugins/babel')
4040
module.exports = (on, config) => {
41-
preprocessor(on, config)
41+
devServer(on, config)
4242
// IMPORTANT to return the config object
4343
// with the any changed environment variables
4444
return config

npm/react/examples/using-babel/cypress/plugins/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
// @ts-check
2+
13
// let's bundle spec files and the components they include using
24
// the same bundling settings as the project by loading .babelrc
35
// https://github.com/bahmutov/cypress-react-unit-test#install
4-
const injectWebpackWithBabelDevServer = require('@cypress/react/plugins/babel')
6+
const devServer = require('@cypress/react/plugins/babel')
57

8+
/**
9+
* @type Cypress.PluginConfig
10+
*/
611
module.exports = (on, config) => {
7-
injectWebpackWithBabelDevServer(on, config)
12+
devServer(on, config)
813

914
// IMPORTANT to return the config object
1015
// with the any changed environment variables

npm/react/examples/visual-sudoku/cypress/plugins/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
// load file preprocessor that comes with this plugin
1+
// @ts-check
2+
3+
// load file devServer that comes with this plugin
24
// https://github.com/bahmutov/cypress-react-unit-test#install
3-
const preprocessor = require('@cypress/react/plugins/react-scripts')
5+
const devServer = require('@cypress/react/plugins/react-scripts')
46
const { addMatchImageSnapshotPlugin } = require('cypress-image-snapshot/plugin')
57

8+
/**
9+
* @type {Cypress.PluginConfig}
10+
*/
611
module.exports = (on, config) => {
712
addMatchImageSnapshotPlugin(on, config)
8-
preprocessor(on, config)
13+
devServer(on, config)
914
// IMPORTANT to return the config object
1015
// with the any changed environment variables
1116
return config
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
// load file preprocessor that comes with this plugin
1+
// @ts-check
2+
3+
// load file devServer that comes with this plugin
24
// https://github.com/bahmutov/cypress-react-unit-test#install
3-
const preprocessor = require('@cypress/react/plugins/react-scripts')
5+
const devServer = require('@cypress/react/plugins/react-scripts')
46

7+
/**
8+
* @type {Cypress.PluginConfig}
9+
*/
510
module.exports = (on, config) => {
6-
preprocessor(on, config)
11+
devServer(on, config)
712

813
// IMPORTANT to return the config object
914
// with the any changed environment variables
1015
return config
1116
}
1217

18+
// @ts-ignore
1319
require('@applitools/eyes-cypress')(module)

npm/react/examples/visual-testing-with-happo/cypress/plugins/index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
// load file preprocessor that comes with this plugin
1+
// @ts-check
2+
3+
// load file devServer that comes with this plugin
24
// https://github.com/bahmutov/cypress-react-unit-test#install
3-
const preprocessor = require('@cypress/react/plugins/react-scripts')
5+
const devServer = require('@cypress/react/plugins/react-scripts')
6+
// @ts-ignore
47
const happoTask = require('happo-cypress/task')
58

9+
/**
10+
* @type {Cypress.PluginConfig}
11+
*/
612
module.exports = (on, config) => {
713
on('task', happoTask)
8-
preprocessor(on, config)
14+
devServer(on, config)
915

1016
// IMPORTANT to return the config object
1117
// with the any changed environment variables

npm/react/examples/visual-testing-with-percy/cypress/plugins/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
// load file preprocessor that comes with this plugin
1+
// @ts-check
2+
3+
// load file devServer that comes with this plugin
24
// https://github.com/bahmutov/@cypress/react#install
3-
const preprocessor = require('@cypress/react/plugins/react-scripts')
5+
const devServer = require('@cypress/react/plugins/react-scripts')
46

7+
/**
8+
* @type {Cypress.PluginConfig}
9+
*/
510
module.exports = (on, config) => {
6-
preprocessor(on, config)
11+
devServer(on, config)
712

813
// IMPORTANT to return the config object
914
// with the any changed environment variables

0 commit comments

Comments
 (0)