How to add component testing without affecting E2E testing? #16940
-
Question1There is a setting about Question2Here's my desired project structure:
This is my current setting, only the test files in the cyprss.json
How do I set the Question3In addition, there is a question about how to write the cypress/plugins/cy-ts-preprocessor.jsconst wp = require('@cypress/webpack-preprocessor')
const webpackOptions = {
resolve: {
extensions: ['.ts', '.js']
},
module: {
rules: [
{
test: /\.ts$/,
exclude: [/node_modules/],
use: [
{
loader: 'ts-loader'
}
]
}
]
}
}
const options = {
webpackOptions
}
module.exports = wp(options) cypress/plugins/index.jsconst { startDevServer } = require('@cypress/webpack-dev-server')
// Your project's Webpack configuration
const webpackConfig = require('../../scripts/webpack/webpack.local')
const cypressTypeScriptPreprocessor = require('./cy-ts-preprocessor');
module.exports = (on, config) => {
if (config.testingType === 'component') {
on('dev-server:start', (options) =>
startDevServer({ options, webpackConfig })
)
}
if (config.testingType === 'e2e') {
on('file:preprocessor', cypressTypeScriptPreprocessor);
// https://docs.cypress.io/api/plugins/browser-launch-api.html#Modify-args-based-on-browser
on('before:browser:launch', (browser = {}, launchOptions) => {
if (browser.name === 'chrome') {
launchOptions.args.push('--auto-open-devtools-for-tabs');
}
// whatever you return here becomes the new args
return launchOptions;
});
}
return config
} I didn't find an E2E compatible profile in the documentation. This is the way I saw it in this issue #16424. Is this method of writing recommended? Question4Install the dependencies according to the documentation: However, when writing the test file, I encountered an ESLint error: Solved it by adding this to my .eslintrc:
But is this the right solution? Question5Do I have to rerun the Looking forward to your reply! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
Hello @liby Those are great questions indeed. Q1: To avoid confusion between e2e and component tests we made this example specific to component testing. It is supposed to help users migrate from Jest to Cypress, which we take will be pretty common. You can have your spec files in any folder you would like, with any name you prefer. Make sure the glob here reflects your choices. Q2: Q3: Because I find clearer to have multiple smaller files, I always recommend to use the To be continued... |
Beta Was this translation helpful? Give feedback.
-
Q4: This is indeed the right solution. Q5: Unfortunately, yes. Changes to cypress.json require a restart of the cypress server. We are working on making it a leaner start and also automate refreshes when cypress.json is updated. Coming soon 🎉 |
Beta Was this translation helpful? Give feedback.
-
I do rarely write test code, so I have some questions that you think are general knowledge. I'm glad to receive such a detailed reply.
Are there any existing examples like webpack-file?
I'm really looking forward to it! Finally, I'm sorry for taking up your time. Have a nice day. |
Beta Was this translation helpful? Give feedback.
-
Is it technically possible to have the component folder set to src but then have the spec files for the components outside of the src folder? I cannot seem to get Cypress to ever pick the specs up unless they sit within the src folder. Is this by design? |
Beta Was this translation helpful? Give feedback.
Q4: This is indeed the right solution.
The
devDependencies
are only useful if you are planing on publishing the work you are doing on npmjs. In which case, when someone installs the library you have published the devDependencies will be ignored. Theno-extraneous-dependency
rule is there to make sure that, in the published files (not the test files), there is no dependency that we forgot to put in thedependencies
field.Q5: Unfortunately, yes. Changes to cypress.json require a restart of the cypress server. We are working on making it a leaner start and also automate refreshes when cypress.json is updated. Coming soon 🎉