Steps to move a node API to Vitest:
- Remove all the references and files related to Jest.
- package.json (Note: Run npm install once you remove from package.json)
- jest.config.js
- launch.js
- To install Vitest, run npm install -D vitest
- 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']);
- 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.jsimport { defineConfig } from 'vitest/config';export default
defineConfig({ test: { globals: true, include: ['test/unit/*.?(c|m)[jt]s?(x)'] }});
- 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.jsshell: { 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']);
- Add Vitest UI.
https://vitest.dev/guide/ui.html
Update package.json and Gruntfile.js commands.
package.json"test:ui": "grunt test:ui",
Gruntfile.jsshell: { 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