Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@cypress/react): Make correct plugins for different adapters/bundlers #15299

Merged
merged 21 commits into from
Mar 5, 2021
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ build
.publish
_test-output
cypress.zip
.babel-cache

# from extension
Cached Theme.pak
Expand Down
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ jobs:

npm-react:
<<: *defaults
parallelism: 9
parallelism: 8
steps:
- attach_workspace:
at: ~/
Expand Down
4 changes: 2 additions & 2 deletions npm/create-cypress-tests/__snapshots__/babel.test.ts.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
exports['babel installation template correctly generates plugins config 1'] = `
const preprocessor = require('@cypress/react/plugins/babel');
const injectDevServer = require('@cypress/react/plugins/babel');

const something = require("something");

module.exports = (on, config) => {
preprocessor(on, config);
injectDevServer(on, config);
return config; // IMPORTANT to return the config object
};
`
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
exports['Injects guessed next.js template cypress.json'] = `
const preprocessor = require("@cypress/react/plugins/next");
const injectDevServer = require("@cypress/react/plugins/next");

module.exports = (on, config) => {
preprocessor(on, config);
injectDevServer(on, config);
return config; // IMPORTANT to return the config object
};

`

exports['Injects guessed next.js template plugins/index.js'] = `
const preprocessor = require("@cypress/react/plugins/next");
const injectDevServer = require("@cypress/react/plugins/next");

module.exports = (on, config) => {
preprocessor(on, config);
injectDevServer(on, config);
return config; // IMPORTANT to return the config object
};

Expand All @@ -24,20 +24,20 @@ import "@cypress/react/support";
`

exports['Injected overridden webpack template cypress.json'] = `
const preprocessor = require("@cypress/react/plugins/react-scripts");
const injectDevServer = require("@cypress/react/plugins/react-scripts");

module.exports = (on, config) => {
preprocessor(on, config);
injectDevServer(on, config);
return config; // IMPORTANT to return the config object
};

`

exports['Injected overridden webpack template plugins/index.js'] = `
const preprocessor = require("@cypress/react/plugins/react-scripts");
const injectDevServer = require("@cypress/react/plugins/react-scripts");

module.exports = (on, config) => {
preprocessor(on, config);
injectDevServer(on, config);
return config; // IMPORTANT to return the config object
};

Expand Down
4 changes: 2 additions & 2 deletions npm/create-cypress-tests/__snapshots__/next.test.ts.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
exports['next.js install template correctly generates plugins config 1'] = `
const preprocessor = require('@cypress/react/plugins/next');
const injectDevServer = require('@cypress/react/plugins/next');

const something = require("something");

module.exports = (on, config) => {
preprocessor(on, config);
injectDevServer(on, config);
return config; // IMPORTANT to return the config object
};
`
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
exports['create-react-app install template correctly generates plugins config 1'] = `
const preprocessor = require('@cypress/react/plugins/react-scripts');
const injectDevServer = require('@cypress/react/plugins/react-scripts');

const something = require("something");

module.exports = (on, config) => {
preprocessor(on, config);
injectDevServer(on, config);
return config; // IMPORTANT to return the config object
};
`
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
exports['webpack-file install template correctly generates plugins config when webpack config path is missing 1'] = `
const preprocessor = require("@cypress/react/plugins/load-webpack");
const injectDevServer = require("@cypress/react/plugins/load-webpack");

const something = require("something");

module.exports = (on, config) => {
// TODO replace with valid webpack config path
config.env.webpackFilename = './webpack.config.js';
preprocessor(on, config);
injectDevServer(on, config);
return config; // IMPORTANT to return the config object
};
`

exports['webpack-file install template correctly generates plugins config when webpack config path is provided 1'] = `
const preprocessor = require("@cypress/react/plugins/load-webpack");
const injectDevServer = require("@cypress/react/plugins/load-webpack");

const something = require("something");

module.exports = (on, config) => {
config.env.webpackFilename = 'config/webpack.config.js';
preprocessor(on, config);
injectDevServer(on, config);
return config; // IMPORTANT to return the config object
};
`
38 changes: 23 additions & 15 deletions npm/create-cypress-tests/__snapshots__/rollup.test.ts.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
exports['rollup-file install template correctly generates plugins config when webpack config path is missing 1'] = `
const rollupPreprocessor = require("@bahmutov/cy-rollup");
const path = require("path");

const {
startDevServer
} = require("@cypress/rollup-dev-server");

const something = require("something");

module.exports = (on, config) => {
on('file:preprocessor', rollupPreprocessor({
// TODO replace with valid rollup config path
configFile: 'rollup.config.js'
}));

require('@cypress/code-coverage/task')(on, config);

on("dev-server:start", async options => {
return startDevServer({
options,
// TODO replace with valid rollup config path
rollupConfig: path.resolve(__dirname, 'rollup.config.js')
});
});
return config; // IMPORTANT to return the config object
};
`

exports['rollup-file install template correctly generates plugins config when webpack config path is provided 1'] = `
const rollupPreprocessor = require("@bahmutov/cy-rollup");
const path = require("path");

const {
startDevServer
} = require("@cypress/rollup-dev-server");

const something = require("something");

module.exports = (on, config) => {
on('file:preprocessor', rollupPreprocessor({
configFile: 'config/rollup.config.js'
}));

require('@cypress/code-coverage/task')(on, config);

on("dev-server:start", async options => {
return startDevServer({
options,
rollupConfig: path.resolve(__dirname, 'config/rollup.config.js')
});
});
return config; // IMPORTANT to return the config object
};
`
4 changes: 2 additions & 2 deletions npm/create-cypress-tests/__snapshots__/vueCli.test.ts.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
exports['vue webpack-file install template correctly generates plugins for vue-cli-service 1'] = `
const preprocessor = require("@cypress/vue/dist/plugins/webpack");
const injectDevServer = require("@cypress/vue/dist/plugins/webpack");

const something = require("something");

module.exports = (on, config) => {
preprocessor(on, config); // IMPORTANT return the config object
injectDevServer(on, config); // IMPORTANT return the config object

return config;
};
Expand Down
12 changes: 8 additions & 4 deletions npm/create-cypress-tests/__snapshots__/webpackOptions.test.ts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
exports['webpack-options template correctly generates plugins config 1'] = `
const webpackPreprocessor = require("@cypress/webpack-preprocessor");
const path = require("path");

const {
startDevServer
} = require("@cypress/webpack-dev-Server");

const something = require("something");

Expand All @@ -15,13 +19,13 @@ module.exports = (on, config) => {
publicPath: '/',
chunkFilename: '[name].bundle.js'
},
// TODO: update with valid configuration for your app
// TODO: update with valid configuration for your components
module: {
rules: [{
test: /\\.(js|jsx|mjs|ts|tsx)$/,
loader: 'babel-loader',
options: { ...babelConfig,
cacheDirectory: path.resolve(__dirname, '..', '..', '.babel-cache')
options: {
cacheDirectory: path.resolve(__dirname, '.babel-cache')
}
}]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ export const BabelTemplate: Template = {
'webpack',
)} to bundle the components for testing.`,
recommendedComponentFolder: 'cypress/component',
dependencies: ['@cypress/webpack-dev-server'],
dependencies: ['webpack', '@cypress/webpack-dev-server'],
getExampleUrl: () => 'https://github.com/cypress-io/cypress/tree/develop/npm/react/examples/babel',
getPluginsCodeAst: () => {
return {
Require: babel.template.ast('const preprocessor = require(\'@cypress/react/plugins/babel\')'),
Require: babel.template.ast('const injectDevServer = require(\'@cypress/react/plugins/babel\')'),
ModuleExportsBody: babel.template.ast([
'preprocessor(on, config)',
'injectDevServer(on, config)',
'return config // IMPORTANT to return the config object',
].join('\n'), { preserveComments: true }),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export const NextTemplate: Template = {
dependencies: ['@cypress/webpack-dev-server'],
getPluginsCodeAst: () => {
return {
Require: babel.template.ast('const preprocessor = require(\'@cypress/react/plugins/next\')'),
Require: babel.template.ast('const injectDevServer = require(\'@cypress/react/plugins/next\')'),
ModuleExportsBody: babel.template.ast([
'preprocessor(on, config)',
'injectDevServer(on, config)',
'return config // IMPORTANT to return the config object',
].join('\n'), { preserveComments: true }),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export const ReactScriptsTemplate: Template = {
},
getPluginsCodeAst: () => {
return {
Require: babel.template.ast('const preprocessor = require(\'@cypress/react/plugins/react-scripts\')'),
Require: babel.template.ast('const injectDevServer = require(\'@cypress/react/plugins/react-scripts\')'),
ModuleExportsBody: babel.template.ast([
'preprocessor(on, config)',
'injectDevServer(on, config)',
'return config // IMPORTANT to return the config object',
].join('\n'), { preserveComments: true }),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export const WebpackTemplate: Template<{ webpackConfigPath: string }> = {
: './webpack.config.js'

return {
Require: babel.template.ast('const preprocessor = require("@cypress/react/plugins/load-webpack")'),
Require: babel.template.ast('const injectDevServer = require("@cypress/react/plugins/load-webpack")'),
ModuleExportsBody: babel.template.ast([
includeWarnComment
? '// TODO replace with valid webpack config path'
: '',
`config.env.webpackFilename = '${webpackConfigPath}'`,
'preprocessor(on, config)',
'injectDevServer(on, config)',
'return config // IMPORTANT to return the config object',
].join('\n'), { preserveComments: true }),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function extractRollupConfigPathFromScript (script: string) {

export const RollupTemplate: Template<{ rollupConfigPath: string }> = {
message:
'It looks like you have custom `rollup.config.js`. We can use it to bundle the components for testing.',
'It looks like you have custom `rollup.config.js`. We can use it to bundle components for testing.',
getExampleUrl: () => {
return 'https://github.com/cypress-io/cypress/tree/develop/npm/react/examples/rollup'
},
Expand All @@ -34,19 +34,21 @@ export const RollupTemplate: Template<{ rollupConfigPath: string }> = {
: 'rollup.config.js'

return {
Require: babel.template.ast('const rollupPreprocessor = require("@bahmutov/cy-rollup")'),
Require: babel.template.ast([
'const path = require("path")',
'const { startDevServer } = require("@cypress/rollup-dev-server")',
].join('\n')),
ModuleExportsBody: babel.template.ast([
`on(`,
` 'file:preprocessor',`,
` rollupPreprocessor({`,
`on("dev-server:start", async (options) => {`,
` return startDevServer({`,
` options,`,
includeWarnComment
? ' // TODO replace with valid rollup config path'
? ' // TODO replace with valid rollup config path'
: '',
` configFile: '${rollupConfigPath}',`,
` }),`,
`)`,
` rollupConfig: path.resolve(__dirname, '${rollupConfigPath}'),`,
` })`,
`})`,
``,
`require('@cypress/code-coverage/task')(on, config)`,
`return config // IMPORTANT to return the config object`,
].join('\n'), { preserveComments: true }),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ const webpackConfig = {
publicPath: '/',
chunkFilename: '[name].bundle.js',
},
// TODO: update with valid configuration for your app
// TODO: update with valid configuration for your components
module: {
rules: [
{
test: /\.(js|jsx|mjs|ts|tsx)$/,
loader: 'babel-loader',
options: { ...babelConfig, cacheDirectory: path.resolve(__dirname, '..', '..', '.babel-cache') },
options: { cacheDirectory: path.resolve(__dirname, '.babel-cache') },
},
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ export const WebpackOptions: Template = {
},
test: () => ({ success: false }),
recommendedComponentFolder: 'src',
dependencies: ['@cypress/webpack-dev-server'],
dependencies: ['webpack', '@cypress/webpack-dev-server'],
getPluginsCodeAst: () => {
return {
Require: babel.template.ast('const webpackPreprocessor = require("@cypress/webpack-preprocessor")'),
Require: babel.template.ast([
'const path = require("path")',
'const { startDevServer } = require("@cypress/webpack-dev-Server")',
].join('\n')),
ModuleExportsBody: babel.template.ast(
fs.readFileSync(path.resolve(__dirname, 'webpack-options-module-exports.template.js'), { encoding: 'utf-8' }),
{ preserveComments: true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export const VueCliTemplate: Template = {
getPluginsCodeAst: () => {
return {
Require: babel.template.ast(
'const preprocessor = require("@cypress/vue/dist/plugins/webpack");',
'const injectDevServer = require("@cypress/vue/dist/plugins/webpack");',
),
ModuleExportsBody: babel.template.ast([
'preprocessor(on, config);',
'injectDevServer(on, config);',
'// IMPORTANT return the config object',
'return config',
].join('\n'), { preserveComments: true }),
Expand Down
4 changes: 3 additions & 1 deletion npm/react/cypress/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ const webpackConfig = {
* @type Cypress.PluginConfig
*/
module.exports = (on, config) => {
on('dev-server:start', (options) => startDevServer({ options, webpackConfig, disableLazyCompilation: false }))
on('dev-server:start', (options) => {
return startDevServer({ options, webpackConfig, disableLazyCompilation: false })
})

return config
}
4 changes: 2 additions & 2 deletions npm/react/examples/a11y/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"description": "Testing component accessibility",
"private": true,
"scripts": {
"cy:open": "node ../../../../scripts/cypress open",
"test": "node ../../../../scripts/cypress run"
"cy:open": "node ../../../../scripts/cypress open-ct",
"test": "node ../../../../scripts/cypress run-ct"
},
"devDependencies": {
"@cypress/react": "file:../../dist",
Expand Down
Loading