cypress-io / code-coverage

Saves the code coverage collected during Cypress tests
MIT License
430 stars 108 forks source link

CypressError: `cy.task('coverageReport')` failed - 'path' must be a string or Uint8Array #582

Open xrutayisire opened 2 years ago

xrutayisire commented 2 years ago

Logs and screenshots

DEBUG=code-coverage yarn cypress:run

App test suite
  code-coverage parsed sent coverage +0ms
  code-coverage wrote coverage file /Volumes/Projects/work/example/.nyc_output/out.json +9ms
  code-coverage NYC file /Volumes/Projects/work/example/.nyc_output/out.json has 4 key(s) +4s
  code-coverage 1 key /Volumes/Projects/work/example/commonjsHelpers.js file path /Volumes/Projects/work/example/commonjsHelpers.js +0ms
  code-coverage 2 key /Volumes/Projects/work/example/src/routes/App.tsx file path /Volumes/Projects/work/example/src/routes/App.tsx +0ms
  code-coverage 3 key /Volumes/Projects/work/example/src/assets/styles/index.ts file path /Volumes/Projects/work/example/src/assets/styles/index.ts +0ms
  code-coverage in file /Volumes/Projects/work/example/.nyc_output/out.json all files are not found? false +4ms
  code-coverage NYC file /Volumes/Projects/work/example/.nyc_output/out.json has 4 key(s) +1ms
  code-coverage ⚠️ cannot find file /Volumes/Projects/work/example/commonjsHelpers.js with hash 9651d8954bbfc1d463a64b666e5bd9644ac85cc1 +1ms
  code-coverage calling NYC reporter with options { 'report-dir': '/Volumes/Projects/work/example/coverage', reporter: [ 'lcov', 'clover', 'json', 'json-summary' ], extension: [ '.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx' ], excludeAfterRemap: false, 'temp-dir': '/Volumes/Projects/work/example/.nyc_output', tempDir: '/Volumes/Projects/work/example/.nyc_output', reportDir: '/Volumes/Projects/work/example/coverage' } +58ms
  code-coverage current working directory is /Volumes/Projects/work/example +2ms
    1) "after all" hook: generateReport for "should check app is rendering correctly"

  0 passing (679ms)
  1 failing

  1) App test suite
       "after all" hook: generateReport for "should check app is rendering correctly":
     CypressError: `cy.task('coverageReport')` failed with the following error:

> The argument 'path' must be a string or Uint8Array without null bytes. Received '/Volumes/Projects/work/example/coverage/lcov-report/example/\x00commonjsHelpers.js.html'

https://on.cypress.io/api/task

Because this error occurred during a `after all` hook we are skipping all of the remaining tests.

Although you have test retries enabled, we do not retry tests when `before all` or `after all` hooks fail

Versions

vite.config.js

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { visualizer } from 'rollup-plugin-visualizer';

/**
 * Vite configuration
 *
 * https://vitejs.dev/config/
 */
export default defineConfig({
  plugins: [
    react({
      // Exclude storybook stories
      exclude: /\.stories\.tsx?$/,
      babel: {
        plugins: ['istanbul'],
      },
    }),
    visualizer({
      filename: '.source-map-stats.html',
      sourcemap: true,
    }),
  ],
  base: '/',
  build: {
    sourcemap: true,
  },
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true,
      },
    },
  },
});

cypress.config.js

const { defineConfig } = require('cypress');

module.exports = defineConfig({
  e2e: {
    baseUrl: 'http://localhost:4242',
    // Test files
    specPattern: 'cypress/e2e/**/*.spec.ts',
    // Prevent flaky tests
    retries: {
      runMode: 2,
      openMode: 0,
    },
    env: {
      appPath: '/',
    },
    setupNodeEvents(on, config) {
      require('@cypress/code-coverage/task')(on, config);
      // include any other plugin code...

      // It's IMPORTANT to return the config object
      // with any changed environment variables
      return config;
    },
  },
});

Describe the bug Terminal 1:

  1. yarn
  2. yarn build
  3. npx serve -s dist -p 4242

Terminal 2:

  1. DEBUG=code-coverage yarn cypress:run

⇒ Cypress Error appear

Link to the repo https://github.com/xrutayisire/cypress-react-typescript-vite-example

vinnymicale commented 2 years ago

@xrutayisire Were you able to resolve this? I'm running into a similar issue myself

federico-ntr commented 1 year ago

@vinnymicale did you manage to solve that issue?

sawmurai commented 1 year ago

I am receiving the exact same error. Would be nice if someone from cypress could at least respond.

lmiller1990 commented 1 year ago

Hello all. I tried it out, same issue.

> The argument 'path' must be a string or Uint8Array without null bytes.

I logged the path, one is like this:

/Users/lachlanmiller/code/dump/cypress-react-typescript-vite-example/coverage/lcov-report/Users/lachlanmiller/code/dump/cypress-react-typescript-vite-example/index.html

It's duplicating the directory.

Coming from istanbul-lib-report/lib/file-writer.js. I guess this is config issue.... hmm... we call it in two places:

https://github.com/search?q=repo%3Acypress-io%2Fcode-coverage+istanbul-lib-coverage&type=code

lmiller1990 commented 1 year ago

Error occurs when we get here: https://github.com/istanbuljs/nyc/blob/ab7c53b2f340b458789a746dff2abd3e2e4790c3/index.js#L456-L463

I am still trying to find the part where the path is joined incorrectly, but I am not sure I will be able to get to the bottom of it. Anyone else interested in helping? Is this only happening on certain projects/configuration?

sawmurai commented 1 year ago

I was able to nail it down to this line: https://github.com/istanbuljs-archived-repos/istanbul-lib-report/blob/master/lib/file-writer.js#L156 and hot-fix it with this.baseDir = this.baseDir.replace("\x00", "").

Our setup is cypress + vite + vue3 and our project is a monorepo where the e2e tests executed by cypress and collecting the coverage are in a dedicated package. The path to that dedicated package is what somehow ends up with the null-byte at the end of the path.

The weird thing is that it does not happen anymore now. I removed by hotfix and it still works. Somewhere along the lines I must have made a config change to prevent that nullbyte.

Edit: Btw, sorry for my unfriendly comment last time. I was frustrated with the configuration and it came out harsher than I wanted to.

lmiller1990 commented 1 year ago

path to that dedicated package is what somehow ends up with the null-byte at the end of the path.

What the heck 🤔

We have updated our RWA https://github.com/cypress-io/cypress-realworld-app/pull/1381 to Vite + code coverage and it's working. This might be a useful example for anyone running into issues. The problem is not really clear here, I am unsure what's going on.