Skip to content

Can not setup and configure with Vite / React / TS+SWC #1093

@mpavel

Description

@mpavel

Current behavior

Unable to configure and run after creating new project with vite.

Desired behavior

The configuration should work for new projects created with vite.

Test code to reproduce

$ npm create vite
-> test
-> React
-> Typescript + SWC

$ npm i

$ npm i -D cypress # running, working, all good with the demo tests

$ npm i -D @badeball/cypress-cucumber-preprocessor

$ npm i -D @bahmutov/cypress-esbuild-preprocessor

Cypress configuration according to docs:

import { defineConfig } from "cypress";
import createBundler from "@bahmutov/cypress-esbuild-preprocessor";
import { addCucumberPreprocessorPlugin } from "@badeball/cypress-cucumber-preprocessor";
import createEsbuildPlugin from "@badeball/cypress-cucumber-preprocessor/esbuild";

async function setupNodeEvents(
    on: Cypress.PluginEvents,
    config: Cypress.PluginConfigOptions
): Promise<Cypress.PluginConfigOptions> {
    // This is required for the preprocessor to be able to generate JSON reports after each run, and more,
    await addCucumberPreprocessorPlugin(on, config);

    on(
        "file:preprocessor",
        createBundler({
            plugins: [createEsbuildPlugin(config)],
        })
    );

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

export default defineConfig({
    e2e: {
        baseUrl: "https://duckduckgo.com",
        specPattern: "**/*.feature",
        setupNodeEvents,
    },
});

When I start Cypress, there are two errors:

import { addCucumberPreprocessorPlugin } from "@badeball/cypress-cucumber-preprocessor";
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Named export 'addCucumberPreprocessorPlugin' not found.

This can be fixed easily by modifying the import as follows:

import preprocessor from "@badeball/cypress-cucumber-preprocessor";
const { addCucumberPreprocessorPlugin } = preprocessor

Once fixed, we move to the next error:

TypeError: createEsbuildPlugin is not a function
    at setupNodeEvents

Link to repository to reproduce: https://github.com/mpavel/vite-react-ts-swc-cypress-bdd

tsconfig has been updated to contain node16 and it didn't work.

    "module": "node16",
    "moduleResolution": "node16",

The original values coming from vite are:

    "module": "ESNext",
    "moduleResolution": "bundler",

I've added the "paths" to tsconfig and it didn't work:

    "paths": {
      "@badeball/cypress-cucumber-preprocessor/*": [
        "./node_modules/@badeball/cypress-cucumber-preprocessor/dist/subpath-entrypoints/*"
      ]
    }

I've renamed cypress.config.ts to cypress.config.js and it still didn't work.

Versions

  • Cypress version: ^13.1.0
  • Preprocessor version: ^18.0.5
  • Node version: v18.16.1

Checklist

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions