cypress-io / code-coverage

Saves the code coverage collected during Cypress tests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cypress Code Coverage cypress.config.ts file - setupNodeEvents(on, config) error required is not defined

sandram92 opened this issue · comments

I wanted to set up cypress code coverage on my sveltekit but I get an error in my cypress.config.ts file -

setupNodeEvents(on, config) {
      
      import { defineConfig } from 'cypress'

export default defineConfig({

  e2e: {
    setupNodeEvents(on, config) {
      require('@cypress/code-coverage/task')(on, config)
      return config
    },
  },
})

and when I run cypress test I get error :Your configFile threw an error from: C:\Code\me-ui\cypress.config.ts
The error was thrown while executing your e2e.setupNodeEvents() function:
ReferenceError: require is not defined
at setupNodeEvents (file:///C:/Code/me-ui/cypress.config.ts:10:13)

Anyone experienced this before?

I did npm install -D @cypress/code-coverage and

import '@cypress/code-coverage/support'

in cypress/support/e2e.js

my cypress.config.ts looks like this  

import { defineConfig } from "cypress";

export default defineConfig({
    projectId: '...'
    viewportWidth: 1500,
    viewportHeight: 1500,
    video: false,
    screenshotOnRunFailure: false,

    e2e: {
        setupNodeEvents(on, config) {
            require("@cypress/code-coverage/task")(on, config)
            return config;
        },
        baseUrl: "https://localhost:5173/",
    },
});

I had the same problem,I desperately need a solution!

help! help! help! help!

I still couldn't figure out where is the problem...

@sandram92

if you look in the function

export default function registerCodeCoverageTasks(
  on: Cypress.PluginEvents,
  config: Cypress.PluginConfigOptions,
): void;

try
import coverageTask from "@cypress/code-coverage/task";

  component: {
    setupNodeEvents(on, config) {
      coverageTask(on, config);
      return config;
    },
    ```

@sandram92 Has this been resolved?

The import coverageTask from "@cypress/code-coverage/task"; solution works for me.

Would have found this a lot sooner if it was on https://docs.cypress.io/guides/tooling/code-coverage#E2E-code-coverage

@sandram92

if you look in the function

export default function registerCodeCoverageTasks(
  on: Cypress.PluginEvents,
  config: Cypress.PluginConfigOptions,
): void;

try import coverageTask from "@cypress/code-coverage/task";

  component: {
    setupNodeEvents(on, config) {
      coverageTask(on, config);
      return config;
    },
    ```

This solution worked.
Thanks.