Steps to move a node API to Vitest:

  1. Remove all the references and files related to Jest.
    1. package.json (Note: Run npm install once you remove from package.json)
    2. jest.config.js
    3. launch.js 
  2. To install Vitest, run npm install -D vitest 
  3. Update grunt file command from jest to: Gruntfile.js
    shell:  {            vitest: {                command: 'vitest'            },            // still have to run spec validation with mocha            specValidation: {                command: 'mocha --check-leaks --exit --reporter=spec test/specValidation.js'            }        }........................grunt.registerTask('test', ['env:test', 'eslint', 'shell:specValidation', 'shell:vitest']);
  4. Add Vitest Config vitest.config.js 
    globals: is set to true so that it picks up keywords such as it, describe, etc. that we used in Jest. This will help us not to make any changes to the unit test files.

    Include: files based on how the naming convention used in the existing projec.
    https://vitest.dev/config/#include
    Default: [‘**/*.{test,spec}.?(c|m)[jt]s?(x)’]

    vitest.config.js
    import { defineConfig } from 'vitest/config';export default defineConfig({    test: {        globals: true,        include: ['test/unit/*.?(c|m)[jt]s?(x)']    }});
  5. Update test code coverage.
    Vitest supports Native code coverage via v8. Install npm i -D @vitest/coverage-v8
    Update package.json and Gruntfile.js commands. 

    package.json
    "coverage": "grunt test:coverage",
    Gruntfile.js
    shell: {            vitest: {                command: 'vitest'            },            vitestCoverage: 'vitest run --coverage',            // still have to run spec validation with mocha            specValidation: {                command: 'mocha --check-leaks --exit --reporter=spec test/specValidation.js'            }        },........................grunt.registerTask('test:coverage', ['env:test', 'eslint', 'shell:specValidation', 'shell:vitestCoverage']);
  6. Add Vitest UI. 
    https://vitest.dev/guide/ui.html
    Update package.json and Gruntfile.js commands. 

    package.json
    "test:ui": "grunt test:ui",
    Gruntfile.js
    shell: {            vitest: {                command: 'vitest'            },            vitestUI: {                command: 'vitest --ui'            },            vitestCoverage: 'vitest run --coverage',            // still have to run spec validation with mocha            specValidation: {                command: 'mocha --check-leaks --exit --reporter=spec test/specValidation.js'            }        },........................grunt.registerTask('test:ui', ['env:test', 'eslint', 'shell:specValidation', 'shell:vitestUI']);

    Test UI path: http://localhost:51204/__vitest__/

Note:

  • Moving Vitest commands to Gruntfile.js will be based on how the existing project is setup. This could also be done within the package.json files as well.

Leave a comment

I’m Harith

Enthusiastic Full Stack Software Engineer with 11 years of experience in IT and Software Development. Utilising expertise in managing all aspects of the software development lifecycle to collaborate with and guide globally dispersed cross functional Agile teams in building cutting edge software solutions across Banking, Patent Management, Healthcare, Manufacturing and Hospitality for clients in Canada, USA, Scandinavia, Australia, Malaysia and Sri Lanka. Approach situations with a solution mindset and flexibility to quickly learn new technologies, adapting to the needs and technology stack of any project to deliver high quality software solutions.

Let’s connect