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

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

Closed
joesobo opened this issue Sep 30, 2022 · 6 comments
Closed

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

joesobo opened this issue Sep 30, 2022 · 6 comments

Comments

@joesobo
Copy link

joesobo commented Sep 30, 2022

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.
@danielroe
Copy link
Member

Here is an example: https://stackblitz.com/edit/github-jfeanw.

@danielroe
Copy link
Member

With that repro I can't actually reproduce your error. If you're still experiencing it, would you provide a reproduction? 🙏

@joesobo
Copy link
Author

joesobo commented Oct 27, 2022

Thanks for getting back to me on this @danielroe!

I just tested it out and it seems like the issue I was running into was due to colocating my tests within the pages/ dir. Works as expected when I move all tests out into another dir like tests/

Copy link
Member

Great. You may find that adding the test pattern to ignore in your config also resolves the issue for you.

@danielroe danielroe closed this as not planned Won't fix, can't repro, duplicate, stale Oct 27, 2022
@noook
Copy link

noook commented 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

@DengSihan
Copy link

Same issue here. It may be caused by the wrong location of your test file just as @noook mentioned.


To test SSR pages/index.vue

describe('index page', async () => {
    await setup();
    test('should has a title', async () => {
        const htmlString = await $fetch('/');
        console.log(htmlString);
    });
});

Minimum issue reproduction: locate this file as pages/index.spec.js

How to fix: pages/index.spec.js -> tests/{anywhere}/index.spec.js

@danielroe danielroe added the 3.x label Jan 19, 2023
@danielroe danielroe transferred this issue from nuxt/framework Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants