Skip to content
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

Writing seperate loader for *.cucumber files results in esbuild-preprocessor error (headless mode only) #1254

Open
thomasatdiscoverage opened this issue Oct 31, 2024 · 4 comments

Comments

@thomasatdiscoverage
Copy link

thomasatdiscoverage commented Oct 31, 2024

Hi,

Not too sure that this thread should belong in this forum or the one for the cypress-cucumber-preprocessor plugin, but here's the problem.

So basically, I'm trying to modify the es-build-preprocessor to convert *.cucumber files instead of *.feature to the needed .spec.ts files and I hope I'm doing it completely wrong and there's a better solution/approach than the following:

According to the readme's, I should write my own loader to handle *.cucumber files. This looks as follows (based on the existing cucumber-processor code):

import * as fs from 'fs';
// @ts-expect-error will error
import { compile } from '@badeball/cypress-cucumber-preprocessor/browserify';

export function createCucumberESBuildPlugin(config) {
  return {
    name: 'cucumber',
    setup(build) {
      // @ts-expect-error will error
      build.onLoad({ filter: /\.cucumber$/ }, async (args) => {
        const content = fs.readFileSync(args.path, 'utf8');
        return {
          contents: await compile(config, content, args.path),
          loader: 'js',
        };
      });
    },
  };
}

I'm using this plugin as follows:

import { addCucumberPreprocessorPlugin, } from "@badeball/cypress-cucumber-preprocessor";
import { createCucumberESBuildPlugin } from "./cucumber-plugin";
import createBundler = require("@bahmutov/cypress-esbuild-preprocessor");

export async function setupNodeEvents(
    cypressOn: Cypress.PluginEvents,
    config: Cypress.PluginConfigOptions,
): Promise<Cypress.PluginConfigOptions> {

    // https://github.com/bahmutov/cypress-on-fix
    // eslint-disable-next-line @typescript-eslint/no-var-requires
    const on = require('cypress-on-fix')(cypressOn)

    await addCucumberPreprocessorPlugin(on, config);

    on(
        'file:preprocessor',
        createBundler({
            plugins: [createCucumberESBuildPlugin(config)],
        },),
    );

As you can see, I'm using the cypress-on-fix package to fix possible errors for the 'on' listeners.

When executing the Cypress tests in headless mode, I can see that the *.cucumber files are being converted to the correct format in the generated Cypress bundles, but I get the following error:

CypressError: `cy.task('cypress-cucumber-preprocessor:spec-envelopes')` failed with the following error:
> Unexpected state in specEnvelopesHandler: before-run. This almost always means that you or some other plugin, are overwriting this plugin's event handlers. For more information & workarounds, see https://github.com/badeball/cypress-cucumber-preprocessor/blob/master/docs/event-handlers.md (if neither workaround work, please report at https://github.com/badeball/cypress-cucumber-preprocessor)
https://on.cypress.io/api/task

I have tried both workaround, but without succes. The same error keeps popping up, but only in headless mode, perhaps because that timings in headless mode are conflicting with eachother but not in headed mode.

I'm very curious what is going wrong so some help in this would be very appreciated!

Cheers,

Thomas

@thomasatdiscoverage thomasatdiscoverage changed the title Writing seperate plugin for *.cucumber files results in esbuild-preprocessor error Writing seperate loader for *.cucumber files results in esbuild-preprocessor error Oct 31, 2024
@thomasatdiscoverage thomasatdiscoverage changed the title Writing seperate loader for *.cucumber files results in esbuild-preprocessor error Writing seperate loader for *.cucumber files results in esbuild-preprocessor error (headless mode only) Oct 31, 2024
@badeball
Copy link
Owner

You need to provide a minimal, reproducible example that illustrates the issue.

@thomasatdiscoverage
Copy link
Author

Thanks for your response. I will work on an example ASAP.

@hamza-amami
Copy link

Thanks for your response. I will work on an example ASAP.

Hello, Have you been able to find a workaround for this please ?

I'm also using cypress-on-fix to be able to register cucumber and allure as plugin, but i'm stuck with this error

@thomasatdiscoverage
Copy link
Author

thomasatdiscoverage commented Dec 18, 2024

At our project, we have chosen to rename the .feature files not to .cucumber files, but to .gherkin.feature files so we can configure IntelliJ to use .gherkin.feature files so the preprocessor plugin does not need to be altered. Hereby we solved our challenge in to use Gherkin feature files together with Karate feature files.

No really a solution for our problem, but the workaround is OK by now.

This issue can be closed (how do I do that? :D).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants