Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jinyongp opened this issue Jun 25, 2024 · 2 comments
Open

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

jinyongp opened this issue Jun 25, 2024 · 2 comments
Labels

Comments

@jinyongp
Copy link

Environment


  • Operating System: Darwin
  • Node Version: v20.12.0
  • Nuxt Version: 3.12.2
  • CLI Version: 3.12.0
  • Nitro Version: 2.9.6
  • Package Manager: [email protected]
  • Builder: -
  • User Config: devtools, modules
  • Runtime Modules: @nuxt/test-utils/[email protected]
  • Build Modules: -

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 dosubot bot added bug Something isn't working vitest-environment labels Jun 25, 2024
Copy link

dosubot bot commented Jun 25, 2024

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
Copy link
Author

duplicate #837

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant