-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Cypress component testing cannot import from alias #24951
Comments
I think the issue here is files served via dev-server do NOT go through the file:preprocessor - that's only used for E2E. The problem you will run into here is that because you are using Create React App, there's not really an easy way to extend the config. The webpack config for CRA is buried in node_modules, we get it here: One option would be not using component: {
devServer: {
framework: 'react',
bundler: 'webpack',
webpackConfig: () => {
const defaultConfig = require('react-scripts/config/webpack.config.js')
return { ...defaultConfig, alias: /* your alias here */ }
}
}
} One other alternative would be to just eject your CRA config. The core issue is you need to modify the Alternatively (rough, private API, untested, but might work) you might be able to piggy-back off our CRA adapter. You would install import CRA from "@cypress/webpack-dev-server/dist/helpers/createReactAppHandler";
import { devServer } from "@cypress/webpack-dev-server";
import { defineConfig } from "cypress";
export default defineConfig({
component: {
devServer: (cypressConfig) => {
const handler = CRA.createReactAppHandler(cypressConfig);
const webpackConfig = handler.frameworkConfig
return devServer({
...cypressConfig,
webpackConfig: {
...webpackConfig,
alias: /* CUSTOM ALIAS HERE!!! */
},
})
},
},
}); Can provide more guidance if needed! Let me know if you need more ideas. |
Unfortunately we have to close this issue due to inactivity. Please comment if there is new information to provide concerning the original issue and we can reopen. |
I am also facing the same issue with the typescript alias.
|
@jainankit102 If you have a reproducible example, please open a new ticket and we will be happy to investigate. |
I had this similar issue with aliasing with a CRA project (non-ejected) and Cypress "component" testing. I was able to solve it. My versions:
Solution:
tsconfig.json (in root directory; already existed in my CRA project, just showing the alias):
cypress.config.ts (in root directory):
I hope this helps! |
It helped! Thank you @aaron-mota ! |
Current behavior
We use alias imports in the application component folder, and in my component tests I need to use imports as well like this
But whenever I try to run cypress, it gives me this error
I have my folder structure like this
The tsconfig.paths.json
The tsconfig.json in the project root folder has the below configuration
The project/cypress/tsconfig.json
cypress.config.js
cypress/plugins/index.js
And cypress/plugins/cy-ts-preprocessor.js
Desired behavior
The component should be mounted correctly by importing the module '@xl/containers/GenericForm'
Test code to reproduce
Cypress Version
10.1.0
Node version
v16.5.0
Operating System
macos 13.0.1 (22A400)
Debug Logs
No response
Other
No response
The text was updated successfully, but these errors were encountered: