使用 Vitest 运行单元测试用例期间出现分段错误

问题描述 投票:0回答:1

我在 Windows 10 操作系统上使用 Git Bash。我使用的是 v18.18.0 node 版本和 9.8.1 npm 版本。该项目使用3.3.4版本在Vue.js框架中实现。出于测试目的,

vitest
测试框架执行单元测试用例。
package.json
文件的脚本内容:

  "scripts": {
    "serve": "vite",
    "test:unit": "vitest",
    "coverage": "vitest run --coverage",
  },

当我运行

npm run test:unit
命令时,出现分段错误错误。

 ✓ src/views/__tests__/SockView.spec.js (3)
   ✓ StockView.vue (3)
     ✓ renders a title
     ✓ does not render the Bar chart when not loaded
     ✓ renders the Bar chart when loaded
/<location_to_your_nodejs>/nodejs/npm: line 64:   321 Segmentation fault      "$NODE_EXE" "$NPM_CLI_JS" "$@"

正如您所看到的,所有测试都已通过,但在测试运行结束时出现分段错误错误。之后我也失去了终端提示。因此,我需要在每次测试运行时重新启动 Git bash 终端以重新获得终端提示符。

node.js segmentation-fault vuejs3 vitest
1个回答
0
投票

我通过将

threads: false,
配置添加到
vitest.config.js
配置文件中解决了该问题。配置文件完整内容:

import { fileURLToPath } from 'node:url'
import { mergeConfig, defineConfig, configDefaults } from 'vitest/config'
import viteConfig from './vite.config'

export default mergeConfig(
  viteConfig,
  defineConfig({
    test: {
      environment: 'jsdom',
      exclude: [...configDefaults.exclude, 'e2e/*'],
      root: fileURLToPath(new URL('./', import.meta.url)),
      threads: false, // Add this option to avoid segmentation fault
    }
  })
)

或者,您也可以不使用线程来运行

vitest

vitest --no-threads

参考:https://github.com/vitest-dev/vitest/issues/317#issuecomment-1542319622

© www.soinside.com 2019 - 2024. All rights reserved.