question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to get a running env with `nuxt/test-utils`

See original GitHub issue

Environment

- Operating System: `Darwin`
- Node Version:     `v16.16.0`
- Nuxt Version:     `3.0.0-rc.11`
- Nitro Version:    `0.5.4`
- Package Manager:  `yarn@1.22.11`
- Builder:          `vite`

Though I have also tested with rc 10, 8, 5, & 4

Reproduction

This is fairly short so ill avoid reproduction unless theres a strong need

From a fresh Nuxt 3 Project:

// package.json

{
  "scripts": {
    ...
    "build": "nuxt build",
    "test": "vitest run"
  },
  "devDependencies": {
    "nuxt": "3.0.0-rc.11"
  },
  "dependencies": {
    "@nuxt/test-utils": "3.0.0-rc.11",
    "vitest": "^0.23.4"
  }
}
// vite.config.ts

import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    globals: true,
  },
});
// pages/index.vue

<template>
  <h1>Hello World!</h1>
</template>
// pages/index.spec.ts

import { describe, test, expect } from 'vitest'
import { setup, $fetch } from '@nuxt/test-utils'

describe('My test', async () => {
  await setup({
    runner: 'vitest',
  })

  test('my test', () => {
    const html = $fetch('/')

    expect(html).toContain('Hello World!')
  })
})

Describe the bug

During the beforeAll section of the setup in my test it crashes with this result:

Error: [vite]: Rollup failed to resolve import "jest" from "node_modules/@nuxt/test-utils/dist/index.mjs".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`

This is strange since the runner is clearly vitest, so it shouldnt be trying to import jest.

Took a brief look at the index.mjs and my hooks.ctx.options.runner is properly recieving vitest.

The error ends with:

 Segmentfault Error Detected 
  FATAL ERROR: v8::ToLocalChecked Empty MaybeLocal.
  Refer to https://github.com/nodejs/node/issues/42407

Additional context

I havent been able to find a solid working example to base my own code. I was hoping this issue was just the RC version that I was using, but so far I havent had any luck with changing it.

I tried cloning the nuxt-ionic repository as I saw it was on a later RC version and using vitest, however even with that I ran into issues. Is there a good working example I can be pointed in the direction of?

Logs

$ vitest run

 RUN  v0.23.4 /Users/*********************/test-utils

 ❯ pages/index.spec.ts (1)
 1: 0x103ab6ef5 node::Abort() (.cold.1) [/Users/*********************/.nvm/versions/node/v16.16.0/bin/node]
 ❯ pages/index.spec.ts (1) 4104ms
   ❯ My test (1) 4103ms
     ⠙ [ beforeAll ]
     · my test

 2: 0x1027aa869 node::Abort() [/Users/*********************/.nvm/versions/node/v16.16.0/bin/node]
 3: 0x1027aa9df node::OnFatalError(char const*, char const*) [/Users/*********************/.nvm/versions/node/v16.16.0/bin/node]
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  pages/index.spec.ts > My test
 4: 0x10292ed00 v8::V8::ToLocalEmpty() [/Users/*********************/.nvm/versions/node/v16.16.0/bin/node]
Error: [vite]: Rollup failed to resolve import "jest" from "node_modules/@nuxt/test-utils/dist/index.mjs".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
 ❯ onRollupWarning node_modules/vite/dist/node/chunks/dep-6b3a5aff.js:45908:19
 5: 0x1027a596f node::fs::FSReqPromise<node::AliasedBufferBase<double, v8::Float64Array, void> >::Resolve(v8::Local<v8::Value>) [/Users/*********************/.nvm/versions/node/v16.16.0/bin/node]
 ❯ onwarn node_modules/vite/dist/node/chunks/dep-6b3a5aff.js:45706:13
 ❯ Object.onwarn node_modules/vite/node_modules/rollup/dist/es/shared/rollup.js:23225:13
 ❯ ModuleLoader.handleResolveId node_modules/vite/node_modules/rollup/dist/es/shared/rollup.js:22352:26
 ❯ ModuleLoader.resolveDynamicImport node_modules/vite/node_modules/rollup/dist/es/shared/rollup.js:22404:120
 ❯ async file:/Users/*********************/test-utils/node_modules/vite/node_modules/rollup/dist/es/shared/rollup.js:22299:32

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
 6: 0x1027afa40 node::fs::AfterStat(uv_fs_s*) [/Users/*********************/.nvm/versions/node/v16.16.0/bin/node]
 7: 0x103176598 uv__work_done [/Users/*********************/.nvm/versions/node/v16.16.0/bin/node]
 8: 0x10317b75b uv__async_io [/Users/*********************/.nvm/versions/node/v16.16.0/bin/node]
 9: 0x10318f54b uv__io_poll [/Users/*********************/.nvm/versions/node/v16.16.0/bin/node]
Serialized Error: Object {
  "watchFiles": [Array],
}
10: 0x10317bce1 uv_run [/Users/*********************/.nvm/versions/node/v16.16.0/bin/node]


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

11: 0x1026dbf9f node::SpinEventLoop(node::Environment*) [/Users/*********************/.nvm/versions/node/v16.16.0/bin/node]
Test Files  1 failed (1)
     Tests   (1)
12: 0x102852cae node::worker::Worker::Run() [/Users/*********************/.nvm/versions/node/v16.16.0/bin/node]
13: 0x1028566a2 node::worker::Worker::StartThread(v8::FunctionCallbackInfo<v8::Value> const&)::$_3::__invoke(void*) [/Users/*********************/.nvm/versions/node/v16.16.0/bin/node]
14: 0x7ff8042ef4e1 _pthread_start [/usr/lib/system/libsystem_pthread.dylib]
15: 0x7ff8042eaf6b thread_start [/usr/lib/system/libsystem_pthread.dylib]

 Segmentfault Error Detected 
  FATAL ERROR: v8::ToLocalChecked Empty MaybeLocal.
  Refer to https://github.com/nodejs/node/issues/42407

It seems to be an upstream bug of Node.js. To improve the test stability,
you could pass --segfault-retry=3 or set env VITEST_SEGFAULT_RETRY=3 to
have Vitest auto retries on flaky segfaults.

error Command failed with exit code 1.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
danielroecommented, Oct 25, 2022
0reactions
noookcommented, Dec 1, 2022

@danielroe Where exactly would you put that piece of config ?

I’m having this issue when my spec file is in the pages directory

edit: I found out it was in nuxt config, but actually I have this issue when I run the tests

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access `process.env` property when testing nuxt.js app with jest
Imports are hoisted so the import statements all run before process. env is set in beforeAll . If an import -ed module needs...
Read more >
Setup - nuxt/test-utils
Setting up your Nuxt test environment. ... The name of the Nuxt configuration file that will be read to get the configuration for...
Read more >
Introduction to Nuxt Test Utils - Reflect.run
Here we provide an overview of Nuxt Test Utils, its features, and how to set up a test environment with `nuxt/test-utils`.
Read more >
An ultimate guide to CICD for your Nuxt.js Application
These changes are validated by creating a build and running automated tests against the build. If these tests fail, the changes are not...
Read more >
Creating Server-side Rendered Vue.js Apps Using Nuxt.js
Then send this complete page to the browser along with the ability to make further changes and still have dynamic page content (the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found