nuxt / test-utils

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

Doesn't load `vitest.config.ts` automatically. #872

Open jinyongp opened 3 days ago

jinyongp commented 3 days ago

Environment


Reproduction

Reproduction on CodeSandbox

Describe the bug

After adding @nuxt/test-utils/module to nuxt.config.ts, the vitest.config.ts file in the root directory is not automatically loaded. Even when explicitly specifying vitestConfig.config in the testUtils property, the vitest.config.ts is not being applied.

Here is the configuration:

// nuxt.config.ts
export default defineNuxtConfig({
  devtools: { enabled: true },

  modules: [
    '@nuxt/test-utils/module'
  ],

  testUtils: {
    startOnBoot: true,
    logToConsole: true,
    vitestConfig: {
      // config: path.join(__dirname, 'vitest.config.ts') // not working
    }
  }
})
// vitest.config.ts
import { defineVitestConfig } from '@nuxt/test-utils/config'

export default defineVitestConfig({
  test: {
    globals: true,
    silent: true,
    environment: 'nuxt',

    include: [
      'server/**/*.test.ts',
    ],

    exclude: [
      '**/-*.*',
    ],
  }
})

I have observed a difference in the results when running tests directly with vitest and running them via nuxt dev.

$ vitest run

 ✓ server/tests/included-test.test.ts (1)
   ✓ included-test (1)
     ✓ should work

 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  03:26:12
   Duration  374ms (transform 94ms, setup 118ms, collect 3ms, tests 1ms, environment 68ms, prepare 99ms)
$ nuxt dev

      UI started at http://localhost:15555/__vitest__/

ℹ Vite server warmed up in 661ms                                                                                                                                   5:19:46 AM
 ❯ server/tests/-ignored-test.test.ts (1)
   ❯ ignored-test (1)
     × this test should be ignored
 ✓ server/tests/included-test.test.ts (1)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  server/tests/-ignored-test.test.ts > ignored-test > this test should be ignored
AssertionError: expected true to be false // Object.is equality

- Expected
+ Received

- false
+ true

 ❯ server/tests/-ignored-test.test.ts:5:18
      3| describe("ignored-test", () => {
      4|   it("this test should be ignored", () => {
      5|     expect(true).toBe(false);
       |                  ^
      6|   });
      7| });

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

 Test Files  1 failed | 1 passed (2)
      Tests  1 failed | 1 passed (2)
   Start at  05:19:46
   Duration  433ms (transform 443ms, setup 73ms, collect 438ms, tests 6ms, environment 0ms, prepare 115ms)

Additional context

No response

Logs

No response

dosubot[bot] commented 3 days ago

Hey there, @jinyongp! 👋 I'm here to help you out with any bugs, questions, or becoming a contributor. Let's squash those bugs together!

To ensure that the vitest.config.ts file is automatically loaded when using @nuxt/test-utils/module in your Nuxt 3 project, you should define the configuration using defineVitestConfig from @nuxt/test-utils/config. Here is an example configuration:

// vitest.config.ts
import { defineVitestConfig } from '@nuxt/test-utils/config'

export default defineVitestConfig({
  test: {
    globals: true,
    silent: true,
    environment: 'nuxt',
    include: [
      'server/**/*.test.ts',
    ],
    exclude: [
      '**/-*.*',
    ],
  }
})

Additionally, ensure that your nuxt.config.ts is correctly set up to include the @nuxt/test-utils/module and the testUtils property:

// nuxt.config.ts
export default defineNuxtConfig({
  devtools: { enabled: true },

  modules: [
    '@nuxt/test-utils/module'
  ],

  testUtils: {
    startOnBoot: true,
    logToConsole: true,
    vitestConfig: {
      // No need to specify the config path here
    }
  }
})

If the vitest.config.ts is still not being applied, make sure that your vitest.config.ts file is named correctly and located in the root directory of your project. Also, ensure that your package.json has "type": "module" specified, or rename your configuration file to vitest.config.mts if you are using TypeScript.

If you continue to experience issues, it might be related to a known issue where the vitest.config.ts file is not automatically loaded. In such cases, you can explicitly import and apply the configuration in your nuxt.config.ts:

// nuxt.config.ts
import { defineNuxtConfig } from 'nuxt'
import vitestConfig from './vitest.config.ts'

export default defineNuxtConfig({
  devtools: { enabled: true },

  modules: [
    '@nuxt/test-utils/module'
  ],

  testUtils: {
    startOnBoot: true,
    logToConsole: true,
    vitestConfig: vitestConfig
  }
})

This should ensure that the vitest.config.ts configuration is applied correctly when running your tests with Nuxt [1].

To continue talking to Dosu, mention @dosu.

jinyongp commented 3 days ago

duplicate #837

the issue has not been solved yet, so I will keep it open.