简体   繁体   English

Cypress 测试 - 在 typescript 中运行 cypress 测试时,Cucumber 功能文件给出错误“您可能需要适当的加载程序来处理此文件”

[英]Cypress Test - Cucumber feature files giving error "You may need an appropriate loader to handle this file" while running cypress tests in typescript

I am running a cucumber feature file in a cypress runner.我在柏树跑步者中运行黄瓜功能文件。

db.feature db.feature

Feature: DB

    Scenario: db test
        When i try to connect to db
        Then i get the code

My step definitions are as follows我的步骤定义如下

db.ts数据库.ts

import { Given, When, Then, Before } from "cypress-cucumber-preprocessor/steps";

When(/^i try to connect to db$/, () => {
  cy.task('log', 'This is the config task')

});

Then(/^i get the code$/, () => {
    return true;
});

my index.js file under plugins folder is我的插件文件夹下的index.js文件是

const browserify = require('@cypress/browserify-preprocessor');
const cucumber = require('cypress-cucumber-preprocessor').default;

module.exports = (on, config) => {
  const options = {
    ...browserify.defaultOptions,
    typescript: require.resolve('typescript'),
  };

  on('file:preprocessor', cucumber(options));
};

module.exports = (on, config) => {
  on('task', {
    log(message) {
      console.log(message)

      return null
    },
  })
}

I have come across some solutions which use webpack-preprocessor but i am not using webpack-preprocessor but instead using browserify-preprocessor with typescript.我遇到了一些使用 webpack-preprocessor 的解决方案,但我没有使用 webpack-preprocessor,而是使用带有 typescript 的 browserify-preprocessor。 (Gave the webpack-preprocessor a try as well but to no avail.) (也尝试了 webpack-preprocessor,但无济于事。)

When i run this in the cypress runner i get the following error当我在 cypress runner 中运行它时,出现以下错误在此处输入图像描述

When i remove the config task, log, from the index.js file and comment out the cy.task call in the db.ts file the code works smoothly.当我从index.js文件中删除配置任务、记录并注释掉db.ts文件中的cy.task调用时,代码运行顺利。 Its only when i add a config task in the index.js file that i get the following error.只有当我在index.js文件中添加配置任务时,我才会收到以下错误。

Error: Webpack Compilation Error
./cypress/integration/db.feature 3:17
Module parse failed: Unexpected token (3:17)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| Feature: DB
| 
>     Scenario: db test
|         When i try to connect to db
|         Then i get the code
    at Watching.handle [as handler] (C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\packages\server\node_modules\@cypress\webpack-preprocessor\dist\index.js:180:23)
    at C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\webpack\lib\Watching.js:99:9
    at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\tapable\lib\Hook.js:154:20)
    at Watching._done (C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\webpack\lib\Watching.js:98:28)
    at C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\webpack\lib\Watching.js:73:19
    at Compiler.emitRecords (C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\webpack\lib\Compiler.js:499:39)
    at C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\webpack\lib\Watching.js:54:20
    at C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\webpack\lib\Compiler.js:485:14
    at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\tapable\lib\Hook.js:154:20)
    at C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\webpack\lib\Compiler.js:482:27
    at C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\neo-async\async.js:2818:7
    at done (C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\neo-async\async.js:3522:9)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\tapable\lib\Hook.js:154:20)
    at C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\webpack\lib\Compiler.js:464:33
    at C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\graceful-fs\graceful-fs.js:143:16
    at C:\Users\visha\AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\node_modules\graceful-fs\graceful-fs.js:61:14
    at FSReqCallback.oncomplete (node:fs:188:23)

This is my package.json这是我的 package.json


  "cypress-cucumber-preprocessor": {
    "cucumberJson": {
      "generate": true,
      "outputFolder": "cypress/cucumber-json",
      "filePrefix": "",
      "fileSuffix": ".cucumber",
      "nonGlobalStepDefinitions": false,
      "nonGlobalStepBaseDir": "cypress/support/step_definitions"
    }
  },
  "dependencies": {
    "cosmiconfig": "^7.0.1"
  },
  "devDependencies": {
    "@cypress/browserify-preprocessor": "^3.0.2",
    "@cypress/webpack-preprocessor": "^5.11.1",
    "@types/cypress-cucumber-preprocessor": "^4.0.1",
    "cypress": "^9.5.3",
    "cypress-cucumber-preprocessor": "^4.3.1",
    "typescript": "^4.6.3"
  }

I faced the same issue, but in my case the solution was to pass the content of the plugin/index.js into setupNodeEvents from cypress.config.ts file.我遇到了同样的问题,但在我的情况下,解决方案是将 plugin/index.js 的内容从cypress.config.ts文件传递​​到setupNodeEvents中。

I'm using cypress 10.0.0 version and based on the oficial documentation , the plugins file was deprecated.我使用的是 cypress 10.0.0 版本,根据官方文档,插件文件已被弃用。

  e2e: {
    setupNodeEvents(on, config) {
      const cucumber = require('cypress-cucumber-preprocessor').default
      const browserify = require('@cypress/browserify-preprocessor');
      const options = {
        ...browserify.defaultOptions,
        typescript: require.resolve('typescript'),
      };
      on('file:preprocessor', cucumber(options));
    },
   ...
  }

The fix was having the custom task under the same module.export as the browserify.修复是在与 browserify 相同的 module.export 下有自定义任务。 Having two differed module.export in the index.js file caused issue.index.js文件中有两个不同的 module.export 会导致问题。

This is my final index.js file这是我的最终index.js文件


const browserify = require('@cypress/browserify-preprocessor');
const cucumber = require('cypress-cucumber-preprocessor').default;

module.exports = (on, config) => {
  const options = {
    ...browserify.defaultOptions,
    typescript: require.resolve('typescript'),
  };

  on('file:preprocessor', cucumber(options));
  on('task', {
    log(message) {
      console.log(message)

      return null
    },
  })
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您可能需要适当的加载程序来处理此文件类型 typescript - you may need an appropriate loader to handle this file type typescript webpack错误:您可能需要适当的加载程序来处理此文件类型 - webpack error : You may need an appropriate loader to handle this file type Laravel-mix 编译错误:将 javascript 与 typescript 组件库捆绑在一起:您可能需要适当的加载器来处理此文件类型 - Laravel-mix compile error: bundling javascript with typescript components library: You may need an appropriate loader to handle this file type react-typescript-您可能需要适当的加载程序来处理此文件类型,当前未配置任何加载程序来处理此文件 - react-typescript - You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file Typescript with Webpack - 你可能需要一个合适的加载器来处理这个文件类型,目前没有配置加载器来处理这个文件 - Typescript with Webpack - You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file Webpack:您可能需要适当的加载器来处理此文件 - Webpack: You may need an appropriate loader to handle this file 如何修复 NextJS webpack 错误:您可能需要适当的加载器来处理此文件类型,目前没有配置加载器 - How to fix NextJS webpack error: You may need an appropriate loader to handle this file type, currently no loaders are configured Angular 2 - Karma 错误 - 模块解析失败“您可能需要合适的加载程序来处理此文件类型” - Angular 2 - Karma Error - Module Parse Failed 'You may need an appropriate loader to handle this file type' 模块解析失败:意外标记 (214:33) 您可能需要适当的加载程序来处理此文件类型。 TypeScript CRA项目 - Module parse failed: Unexpected token (214:33) You may need an appropriate loader to handle this file type. TypeScript CRA project Angular您可能需要适当的加载程序错误 - Angular You may need an appropriate loader error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM