code-coverage icon indicating copy to clipboard operation
code-coverage copied to clipboard

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

Open sandram92 opened this issue 1 year ago • 8 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?

sandram92 avatar Oct 24 '23 14:10 sandram92

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/",
    },
});

sandram92 avatar Oct 24 '23 15:10 sandram92

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

help! help! help! help!

ETTTTT avatar Oct 25 '23 07:10 ETTTTT

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

sandram92 avatar Oct 30 '23 09:10 sandram92

@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;
    },
    ```

pascalvos avatar Nov 06 '23 19:11 pascalvos

@sandram92 Has this been resolved?

jennifer-shehane avatar Nov 28 '23 19:11 jennifer-shehane

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

bmulholland avatar Dec 04 '23 18:12 bmulholland

@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.

karolis-kimtys avatar Jun 10 '24 12:06 karolis-kimtys

This doesn't work for me (using React), I get:

TypeError: (0 , task_1.default) is not a function
    at setupNodeEvents (apps/app-e2e/cypress.config.ts:20:19)

If I try

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

Then it works, but TypeScript complains

TS2349: This expression is not callable.
  Type 'typeof import("node_modules/@cypress/code-coverage/task")' has no call signatures.

DanielSchiavini avatar Jun 18 '24 07:06 DanielSchiavini