【问题标题】:Webpack Compilation Error with Cypress 10 and Cucumber in AngularCypress 10 和 Angular 中的 Cucumber 的 Webpack 编译错误
【发布时间】:2023-01-05 20:57:08
【问题描述】:

我在这个网站上看到过其他类似的问题,但没有一个能给我一个令人满意的解决方案。我没有任何 webpack.config.js 文件,因为我们从 angular 获取默认配置。请查看下面相应的图片,以便更好地了解我的问题。

我项目中的其他配置:

包.json

  "cypress-cucumber-preprocessor": {
"nonGlobalStepDefinitions": true,
"json": {
  "enabled": true
},
"stepDefinitions": "**/cypress/e2e/**/*.js",
"step_definitions": "**/cypress/e2e/**/*.js"
},

赛普拉斯.config.ts

import { defineConfig } from 'cypress'
export default defineConfig({
e2e: {
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
  return require('./cypress/plugins/index.js')(on, config)
},
specPattern: '**/e2e/**/*.feature',
"supportFile": false,
chromeWebSecurity: false
},
})

更新 1:您好@Wirtuald,感谢您回复我。 我开始在一个相当复杂的角度项目中遇到这个问题。所以,我从头开始创建了一个基本项目我仍然有同样的问题.然后,我在下面给你这个新项目的所有信息:

  • 我没有“插件”文件夹

  • package.json 上的版本:

      "devDependencies": {
      "@badeball/cypress-cucumber-preprocessor": "^11.2.0",
      "cypress": "^10.2.0"
      },
    
  • package.json 上的预处理器配置

    "cypress-cucumber-preprocessor": {
    "nonGlobalStepDefinitions": true,
    "json": {
    "enabled": true
    },
    "stepDefinitions": [
    "[filepath].{js,ts}",
    "cypress/e2e/**/*.{js,ts}"
    ]
    

    }

  • 赛普拉斯.config.js:

      const { defineConfig } = require("cypress");
      module.exports = defineConfig({
      e2e: {
      specPattern: "**/*.feature",
      chromeWebSecurity: false,
      setupNodeEvents(on, config) {
      // implement node event listeners here
      },
     },
    });
    

-项目结构

  • direct-attention-steps.js
    import { Given, When, Then } from "@badeball/cypress-cucumber-preprocessor";
    Given("Access to NXSuite", () => {
    cy.visit("https://www.nxsuite.com");
    })

【问题讨论】:

标签: angular webpack cucumber cypress cypress-cucumber-preprocessor


【解决方案1】:

你只是错过了 webpack 进程,from quick-start

然后配置您的首选捆绑器来处理功能文件。有关如何使用 Browserify、Esbuild 或 Webpack 的信息,请参阅示例/。

此处给出了 webpack.ts 示例cypress-cucumber-preprocessor/examples/webpack-ts/cypress.config.ts。

@cypress/webpack-preprocessor 和 ts-loader 需要额外安装。

import { defineConfig } from "cypress";
import * as webpack from "@cypress/webpack-preprocessor";
import { addCucumberPreprocessorPlugin } from "@badeball/cypress-cucumber-preprocessor";

async function setupNodeEvents(
  on: Cypress.PluginEvents,
  config: Cypress.PluginConfigOptions
): Promise<Cypress.PluginConfigOptions> {
  await addCucumberPreprocessorPlugin(on, config);

  on(
    "file:preprocessor",
    webpack({
      webpackOptions: {
        resolve: {
          extensions: [".ts", ".js"],
        },
        module: {
          rules: [
            {
              test: /.ts$/,
              exclude: [/node_modules/],
              use: [
                {
                  loader: "ts-loader",
                },
              ],
            },
            {
              test: /.feature$/,
              use: [
                {
                  loader: "@badeball/cypress-cucumber-preprocessor/webpack",
                  options: config,
                },
              ],
            },
          ],
        },
      },
    })
  );

  // Make sure to return the config object as it might have been modified by the plugin.
  return config;
}

export default defineConfig({
  e2e: {
    specPattern: "**/*.feature",
    supportFile: false,
    setupNodeEvents,
  },
});

在更新1cypress.config.js 你打错字了

module.exports = defineConfig({

应该

export default defineConfig({

【讨论】:

  • 我尝试使用推荐的示例来遵循示例,我的意思是:“esbuild”,但我仍然遇到问题:“错误:Webpack 编译错误 ./cypress/e2e/nxsuite/client-attention/direct-attention.feature 1:16模块解析失败:意外令牌 (1:16) 您可能需要一个合适的加载器来处理此文件类型,目前没有配置加载器来处理此文件。请参阅webpack.js.org/concepts#loaders“我将尝试使用 webpack 进行 nex,但不确定我要做什么我做错了。非常感谢
【解决方案2】:

尝试安装@cypress/webpack-preprocessor

【讨论】:

  • 谢谢安迪,但仍然无法正常工作
【解决方案3】:

就我而言,这是最新版本cypress 的问题

我正在使用

"@badeball/cypress-cucumber-preprocessor": "^11.2.0",
"cypress": "^10.2.0",

首先,确保您使用最新版本的@badeball/cypress-cucumber-preprocessor,它修复了与 cypress 10 的一些兼容性问题。有关更多信息,请参阅https://github.com/badeball/cypress-cucumber-preprocessor/issues/722#。

此外,最新版本的 cypress 已删除插件文件。就我而言,将cypress/plugins/index.js中的所有代码迁移到setupNodeEvents方法中解决了这个问题。见https://docs.cypress.io/guides/references/migration-guide#Plugins-File-Removed

细节决定成败,它也可能与您的插件文件本身有关。也许您可以分享它,以便我们更好地帮助您。您还可以分享您使用的“@badeball/cypress-cucumber-preprocessor”版本吗

希望对你有帮助

【讨论】:

  • 你好@Wirtuald,感谢你回复我,我很感激。请查看我在主要问题中提出的“更新 1”:)
【解决方案4】:

我修改了上面的解决方案:

import webpack from "@cypress/webpack-preprocessor";

我删除了 .ts 文件的规则,只在 webpack 配置中保留 .feature 规则。

之后一切正常

【讨论】:

    【解决方案5】:

    我有类似的问题。除了调整脚本,我的解决方法是将 Node 从 18 降级到 16.13.2

    【讨论】:

      猜你喜欢
      • 2020-10-31
      • 2017-08-21
      • 2019-10-31
      • 2022-07-07
      • 2016-07-01
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      相关资源
      最近更新 更多