| Level Navigation: 1 | (2ℹ️) | 3 | 4 | 5 | 6 | (7ℹ️) | (8ℹ️) | (9ℹ️) | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | (37ℹ️) | 38⚡ | 39⚡ | 40⚡ | 41⚡ | 42 | 43⚡ | 44⚡ | 45 | 46 | (47ℹ️) |
We add .test. to the filename (like utils.test.js) to indicate this is a test file. Vitest automatically finds and runs files that match the pattern *.test.js or *.spec.js.
Create utils.test.js:
// utils.test.js
import { describe, it, expect } from 'vitest';
import { add, toSnakeCase } from './utils.js';
describe('add function', () => {
it('should add two positive numbers', () => {
const result = add(2, 3);
expect(result).toBe(5);
});
it('should add negative numbers', () => {
const result = add(-1, -2);
expect(result).toBe(-3);
});
});
describe('toSnakeCase function', () => {
it('should convert text with spaces to snake_case', () => {
const result = toSnakeCase('Hello World');
expect(result).toBe('hello_world');
});
it('should convert to lowercase', () => {
const result = toSnakeCase('HELLO WORLD');
expect(result).toBe('hello_world');
});
});
it should focus on one behavior.