Search code examples
node.jssegmentation-faultvuejs3vitest

Segmentation fault during running unit test cases using Vitest


I am on the Windows 10 operating system using Git Bash. I am using the v18.18.0 node version and the 9.8.1 npm version. The project is implemented in the Vue.js framework using the 3.3.4 version. For testing purposes, the vitest testing framework executes the unit test cases. The scripts' content of the package.json file:

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

When I ran the npm run test:unit command, I got the segmentation fault error.

 ✓ 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
/c/Program Files/nodejs/npm: line 64:   321 Segmentation fault      "$NODE_EXE" "$NPM_CLI_JS" "$@"

As you can see all tests passed but there is a segmentation fault error at the end of the test run. After that, I also lost the terminal prompt. Therefore I need to restart the Git bash terminal every test run to regain the terminal prompt.


Solution

  • I have solved the issue by adding the threads: false, configuration to the vitest.config.js configuration file. The complete content of the configuration file:

    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
        }
      })
    )
    

    Alternatively, you can also run the vitest without using threads:

    vitest --no-threads
    

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