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

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

Open
sandram92 opened this issue Oct 24, 2023 · 6 comments

Comments

@sandram92
Copy link

sandram92 commented Oct 24, 2023

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 sandram92 changed the title Cypress Code Coverage Cypress Code Coverage cypress.config.ts file - setupNodeEvents(on, config) error required is not defined Oct 24, 2023
@sandram92
Copy link
Author

sandram92 commented Oct 24, 2023

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

@ETTTTT
Copy link

ETTTTT commented Oct 25, 2023

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

help! help! help! help!

@sandram92
Copy link
Author

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

@pascalvos
Copy link

pascalvos commented Nov 6, 2023

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

@jennifer-shehane
Copy link
Member

@sandram92 Has this been resolved?

@bmulholland
Copy link

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

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

No branches or pull requests

5 participants