Cypress 中的路径别名
文章标签
别名
我不久前遇到了路径别名。提到开发人员如何在他们的 react 和 node 项目中导入的文章看起来很好。我想知道我是否可以在我的赛普拉斯自动化中使用此功能。
让我给你一些背景信息。因此,我的项目中的导入通常如下所示:import productspage from '../../pages/productspage';import navigation from '../../pages/navigation';import testdata from '../../../../fixtures/product-listing.json'
import productspage from '@pages/productspage';import navigation from '@pages/navigation';import testdata from '@fixtures/product-listing.json'
按照说明在您的 cypress 项目中设置路径别名。
npm install --save-dev @cypress/webpack-preprocessor
const wpoptions = { webpackoptions: { resolve: { alias: { '@pages': path.resolve(__dirname, './cypress/e2e/pages'), '@fixtures': path.resolve(__dirname, './cypress/fixtures'), '@': __dirname, }, }, }, watchoptions: {},};
setupnodeevents(on, config) { on('file:preprocessor', webpackpreprocessor(wpoptions));}
{ "compileroptions": { "paths": { "@pages/*": [ "./cypress/e2e/pages/*" ], "@fixtures/*": [ "./cypress/fixtures/*" ], "@/*": [ "./*" ] } }}
import productspage from '@pages/productspage';import navigation from '@pages/navigation';import testdata from '@fixtures/product-listing.json'