nuxt / test-utils

🧪 Test utilities for Nuxt
http://nuxt.com/docs/getting-started/testing
MIT License
287 stars 74 forks source link

Coverage report does not get generated if `srcDir` in `nuxt.config.js` is modified. #869

Open cadenzah opened 6 days ago

cadenzah commented 6 days ago

Environment

Working directory: /home/projects/github-h9agvz Nuxt project info:


Reproduction

https://stackblitz.com/edit/github-h9agvz?file=vitest.config.ts,nuxt.config.ts

Describe the bug

The coverage report (by either v8 or istanbul) does not get generated if I change the srcDir configuration in nuxt.config.js. It seems it works well if I use the default setting. You can reproduce the issue with the link I provided.

If I click on the Coverage icon button on vitest-ui web page, It shows the error text: Cannot GET /coverage/index.html.

If I run vitest with --ui and coverage.enabled option to true then the coverage report's html files should be generated in /coverage directory, but what I get is only tmp directory with coverage.json files included. It seems the .html files are not generated properly so that the output directory get empty.

I even tried to change reportsDirectory or root in vitest.config.js but nothing changes.

Additional context

Sometimes if I run vitest --ui then after the preparation is ready, the open browser should show the Vitest UI, but it shows Cannot GET /. After that if I click the URL printed on the console (UI started at http://localhost:51204/__vitest__/), then it goes to the UI page. Also, if I click the Coverage icon on that page, it shows the error text of Cannot GET /coverage/index.html as well.

Logs

No response

dosubot[bot] commented 6 days ago

Hey @cadenzah! I'm here to help you with any bugs, questions, or contributions you have. Let's squash those bugs together!

To generate a coverage report in your Nuxt project when modifying the srcDir configuration in nuxt.config.js, you need to ensure that the coverage configuration in your vitest.config.ts file is correctly set up to include the new source directory.

Here is an example of how you can modify your vitest.config.ts to account for the srcDir configuration:

import { fileURLToPath } from 'node:url'
import { defineVitestConfig } from '@nuxt/test-utils/config'

export default defineVitestConfig({
  test: {
    dir: 'tests',
    coverage: {
      reportsDirectory: 'coverage',
    },
    includeSource: ['srcDir/**'], // Add this line to include your new source directory
    environmentOptions: {
      nuxt: {
        rootDir: fileURLToPath(new URL('./', import.meta.url)),
        domEnvironment: (process.env.VITEST_DOM_ENV as 'happy-dom' | 'jsdom') ?? 'happy-dom',
        mock: {
          indexedDb: true,
        },
      },
    },
    setupFiles: './tests/setup/mocks.ts',
    globals: true,
  },
})

In this example, the includeSource property in the coverage configuration is set to include the new source directory specified by srcDir. Adjust the path in the includeSource array to match the actual path of your source directory [1].

To continue talking to Dosu, mention @dosu.

cadenzah commented 5 days ago

The guide provided by Dosu unfortunately does not fix the issue. Hope the maintainers see this and look into it.

cadenzah commented 5 days ago

FYI @danielroe