nuxt / test-utils

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

Vitest config not used in dev tools #837

Open peerhenry opened 1 month ago

peerhenry commented 1 month ago

Environment

Reproduction

NOTE: No configuration from vitest.config.ts is used when running with the devtools, the following steps will only demonstrate that for a configuration that excludes a test folder, but you can try any configuration you want.

STEPS TO REPRODUCE:

describe('adding', () => { it('should work', async () => { expect(2 + 2).toBe(4) }) })

- Create a file `vitest.config.ts`:
```ts
import { defineVitestConfig } from '@nuxt/test-utils/config'

export const testConfig = {
  environment: 'nuxt',
  exclude: [
    '**/node_modules/**',
    './e2e/**/*'
  ],
  setupFiles: ['./testglobals.js'],
}

export default defineVitestConfig({
  test: {
    exclude: [
      '**/node_modules/**',
      './e2e/**/*'
    ],
  }
})

EXPECTED:

vitest.config.ts is used and the test e2e/dummy.test.ts is not executed.

ACTUAL:

e2e/dummy.test.ts is in the test report.

Describe the bug

When running vitest from the dev tools, vitest.config.ts is not loaded.

Additional context

I was able to get a vitest config to work with the devtools by manipulating the nuxt config as follows:

// nuxt.config.ts
const testConfig = {
  environment: 'nuxt',
  exclude: [
    '**/node_modules/**',
    './e2e/**/*'
  ],
}

export default defineNuxtConfig({
  devtools: { enabled: true },
  modules: [
    ['@nuxt/test-utils', { vitestConfig: testConfig }],
  ],
})

I did not find any documentation about this and only discovered this option by painstakingly diving into the source of the test utils module.

Logs

No response

dosubot[bot] commented 1 month ago

To address the issue where your vitest.config.ts is not being used when running Vitest from the Nuxt dev tools, follow these targeted steps:

  1. Confirm the Location of vitest.config.ts: Ensure this file is in the root of your Nuxt project. Its correct placement is crucial for Vitest to recognize and use it.

  2. Explicitly Specify Configuration File: If automatic detection fails, run Vitest with the --config flag followed by the path to your vitest.config.ts, like so:

    vitest run --config vitest.config.ts
  3. Check for Compatibility: Make sure the versions of Nuxt and Vitest you're using are compatible. Incompatibilities could lead to issues with configuration loading.

  4. Review Your Configuration for Errors: Ensure there are no syntax errors or typos in your vitest.config.ts that could prevent it from being loaded.

  5. Consult Documentation: Both Vitest and Nuxt are actively developed, and their integration points can change. Review the latest documentation for any updates or changes that might affect configuration loading.

Additionally, consider defining Nuxt configuration overrides in your vitest.config.ts via test.environmentOptions.nuxt for specifying additional configurations needed for running tests. This approach can help in cases where you need to adjust the Nuxt environment for your tests without altering the default nuxt.config.

By following these steps, you should be able to resolve the issue with vitest.config.ts not being recognized during test runs from the Nuxt dev tools.

#### Details **Sources** - [`defineVitestConfig` not respecting custom `configFile`](https://github.com/nuxt/test-utils/issues/498) - [examples/content/vitest.config.ts](https://github.com/nuxt/test-utils/blob/main/examples/content/vitest.config.ts) - [package.json](https://github.com/nuxt/test-utils/blob/main/package.json) - [src/config.ts](https://github.com/nuxt/test-utils/blob/main/src/config.ts) - [CHANGELOG.md](https://github.com/nuxt/test-utils/blob/main/CHANGELOG.md) - [src/module.ts](https://github.com/nuxt/test-utils/blob/main/src/module.ts)
**About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.