code-coverage
code-coverage copied to clipboard
Cypress Code Coverage cypress.config.ts file - setupNodeEvents(on, config) error required is not defined
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.
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.