webpack 在Cypress组件测试中为React typescript使用别名导入

8mmmxcuj  于 2023-10-19  发布在  Webpack
关注(0)|答案(1)|浏览(182)

我们在应用程序组件文件夹中使用别名导入,在我的组件测试中,我也需要像这样使用导入

  1. import {InputField} from "@company/components/Input"

但是每当我尝试运行cypress时,它都会给我这个错误

我的文件夹结构是这样的

  1. /project
  2. /cypress
  3. /plugins
  4. /index.js
  5. /components
  6. /input.component.tsx
  7. /input.cy.tsx
  8. tsconfig.json
  9. /src
  10. /components
  11. /input.tsx
  12. /button.tsx
  13. /form.tsx
  14. ...
  15. ...
  16. tsconfig.json
  17. tsconfig.paths.json

tsj.paths.json

  1. {
  2. "compilerOptions": {
  3. "baseUrl": ".",
  4. "paths": {
  5. "@company/*": ["./src/*"]
  6. }
  7. }
  8. }

项目根文件夹中的tslog.json具有以下配置

  1. {
  2. "extends": "./tsconfig.paths.json",
  3. "compilerOptions": {
  4. "target": "es5",
  5. "lib": ["dom", "dom.iterable", "esnext"],
  6. "allowJs": true,
  7. "skipLibCheck": true,
  8. "esModuleInterop": true,
  9. "allowSyntheticDefaultImports": true,
  10. "strict": true,
  11. "forceConsistentCasingInFileNames": true,
  12. "noFallthroughCasesInSwitch": true,
  13. "module": "esnext",
  14. "moduleResolution": "node",
  15. "resolveJsonModule": true,
  16. "isolatedModules": true,
  17. "noEmit": true,
  18. "jsx": "react-jsx"
  19. },
  20. "include": ["src", "cypress"],
  21. "exclude": []
  22. }

项目/cypress/tsplus. json

  1. {
  2. "extends": "../tsconfig.json"
  3. }

cypress.config.js

  1. /* eslint-disable @typescript-eslint/no-var-requires */
  2. import { defineConfig } from "cypress";
  3. export default defineConfig({
  4. video: false,
  5. e2e: {
  6. // We've imported your old cypress plugins here.
  7. // You may want to clean this up later by importing these.
  8. setupNodeEvents(on, config) {
  9. return require("./cypress/plugins/index.js")(on, config);
  10. },
  11. baseUrl: "http://localhost:3000",
  12. experimentalSessionAndOrigin: true,
  13. watchForFileChanges: false,
  14. },
  15. viewportWidth: 1920,
  16. viewportHeight: 1080,
  17. component: {
  18. devServer: {
  19. framework: "create-react-app",
  20. bundler: "webpack",
  21. },
  22. setupNodeEvents(on, config) {
  23. return require("./cypress/plugins/index.js")(on, config);
  24. },
  25. },
  26. });

cypress/plugins/index.js

  1. const cypressTypeScriptPreprocessor = require('./cy-ts-preprocessor')
  2. module.exports = on => {
  3. on('file:preprocessor', cypressTypeScriptPreprocessor)
  4. }

和cypress/plugins/cy-ts-preprocessor.js

  1. const wp = require("@cypress/webpack-preprocessor");
  2. const path = require("path");
  3. const webpackOptions = {
  4. resolve: {
  5. extensions: [".ts", ".js", ".tsx"],
  6. alias: {
  7. "@company": path.resolve(__dirname, "../../src"),
  8. },
  9. },
  10. module: {
  11. rules: [
  12. {
  13. test: /\.ts$/,
  14. exclude: [/node_modules/],
  15. use: [
  16. {
  17. loader: "ts-loader",
  18. },
  19. ],
  20. },
  21. ],
  22. },
  23. };
  24. const options = {
  25. webpackOptions,
  26. };
  27. module.exports = wp(options);
5jdjgkvh

5jdjgkvh1#

这个tsconfig在我自己的情况下工作。

  1. {
  2. "compilerOptions": {
  3. "allowJs": true,
  4. "allowSyntheticDefaultImports": true,
  5. "baseUrl": "src",
  6. "esModuleInterop": true,
  7. "forceConsistentCasingInFileNames": true,
  8. "isolatedModules": true,
  9. "jsx": "react-jsx",
  10. "lib": ["es6", "dom", "dom.iterable", "esnext"],
  11. "module": "esnext",
  12. "moduleResolution": "node",
  13. "noEmit": true,
  14. "noImplicitAny": true,
  15. "noImplicitThis": true,
  16. "noFallthroughCasesInSwitch": true,
  17. "paths": {
  18. "alias": ["./path/to-file.ts"]
  19. },
  20. "resolveJsonModule": true,
  21. "skipLibCheck": true,
  22. "strict": true,
  23. "strictNullChecks": true,
  24. "target": "ES2018"
  25. },
  26. "include": ["src", "cypress", "@testing-library/cypress"],
  27. "exclude": []
  28. }
展开查看全部

相关问题