From 38f920a6e3fe9af27117b1768f73b31436a0bb89 Mon Sep 17 00:00:00 2001 From: Denys Kuchma Date: Tue, 17 Mar 2026 13:54:12 +0200 Subject: [PATCH] add vitest-v4 example with testomatio reporter setup --- vitest-v4/.gitignore | 2 + vitest-v4/README.md | 27 ++++++++ vitest-v4/package.json | 19 +++++ vitest-v4/src/basic.ts | 1 + .../test/__snapshots__/suite.test.ts.snap | 7 ++ vitest-v4/test/async.test.ts | 16 +++++ vitest-v4/test/basic.test.ts | 19 +++++ vitest-v4/test/callback.test.ts | 16 +++++ vitest-v4/test/errors.test.ts | 17 +++++ vitest-v4/test/math.test.ts | 69 +++++++++++++++++++ vitest-v4/test/mock.test.ts | 32 +++++++++ vitest-v4/test/promise.test.ts | 22 ++++++ vitest-v4/tsconfig.json | 13 ++++ vitest-v4/vitest.config.ts | 10 +++ 14 files changed, 270 insertions(+) create mode 100644 vitest-v4/.gitignore create mode 100644 vitest-v4/README.md create mode 100644 vitest-v4/package.json create mode 100644 vitest-v4/src/basic.ts create mode 100644 vitest-v4/test/__snapshots__/suite.test.ts.snap create mode 100644 vitest-v4/test/async.test.ts create mode 100644 vitest-v4/test/basic.test.ts create mode 100644 vitest-v4/test/callback.test.ts create mode 100644 vitest-v4/test/errors.test.ts create mode 100644 vitest-v4/test/math.test.ts create mode 100644 vitest-v4/test/mock.test.ts create mode 100644 vitest-v4/test/promise.test.ts create mode 100644 vitest-v4/tsconfig.json create mode 100644 vitest-v4/vitest.config.ts diff --git a/vitest-v4/.gitignore b/vitest-v4/.gitignore new file mode 100644 index 0000000..a56a7ef --- /dev/null +++ b/vitest-v4/.gitignore @@ -0,0 +1,2 @@ +node_modules + diff --git a/vitest-v4/README.md b/vitest-v4/README.md new file mode 100644 index 0000000..1cb7218 --- /dev/null +++ b/vitest-v4/README.md @@ -0,0 +1,27 @@ +# Vitest v4 + Testomatio Reporter Example + +Quick smoke project for validating `@testomatio/reporter` on Vitest 4. + +## Install + +```bash +npm install +``` + +## Run locally + +```bash +npm run test:run +``` + +## Run with Testomatio reporting + +```bash +TESTOMATIO_URL=https://beta.testomat.io TESTOMATIO= npm run test:run +``` + +## Notes + +- Reporter import must be: `@testomatio/reporter/vitest` +- This project uses Vitest `4.0.18` +- Test set mirrors the main `examples/vitest` scenario for quick regression checks diff --git a/vitest-v4/package.json b/vitest-v4/package.json new file mode 100644 index 0000000..02805eb --- /dev/null +++ b/vitest-v4/package.json @@ -0,0 +1,19 @@ +{ + "name": "vitest-v4-testomatio-example", + "private": true, + "license": "MIT", + "type": "module", + "scripts": { + "test": "vitest --config vitest.config.ts", + "test:run": "vitest run --config vitest.config.ts", + "test:watch": "vitest --config vitest.config.ts", + "test:report": "vitest run --config vitest.config.ts --reporter=verbose" + }, + "devDependencies": { + "@testomatio/reporter": "^2.7.1", + "@types/node": "^20.14.8", + "typescript": "^5.9.3", + "vite": "^7.2.2", + "vitest": "4.0.18" + } +} diff --git a/vitest-v4/src/basic.ts b/vitest-v4/src/basic.ts new file mode 100644 index 0000000..d937051 --- /dev/null +++ b/vitest-v4/src/basic.ts @@ -0,0 +1 @@ +export const squared = (n: number) => n * n diff --git a/vitest-v4/test/__snapshots__/suite.test.ts.snap b/vitest-v4/test/__snapshots__/suite.test.ts.snap new file mode 100644 index 0000000..8e09188 --- /dev/null +++ b/vitest-v4/test/__snapshots__/suite.test.ts.snap @@ -0,0 +1,7 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`suite name > snapshot 1`] = ` +{ + "foo": "bar", +} +`; diff --git a/vitest-v4/test/async.test.ts b/vitest-v4/test/async.test.ts new file mode 100644 index 0000000..30a2fd0 --- /dev/null +++ b/vitest-v4/test/async.test.ts @@ -0,0 +1,16 @@ +import { describe, it, expect } from 'vitest'; + +async function delayedHello(name: string): Promise { + return new Promise((resolve) => { + setTimeout(() => { + resolve(`Hello, ${name}!`); + }, 1000); + }); +} + +describe('delayedHello', () => { + it('should return a greeting message after a delay', async () => { + const result = await delayedHello('Alice'); + expect(result).toBe('Hello, Alice!'); + }); +}); diff --git a/vitest-v4/test/basic.test.ts b/vitest-v4/test/basic.test.ts new file mode 100644 index 0000000..d6da3c0 --- /dev/null +++ b/vitest-v4/test/basic.test.ts @@ -0,0 +1,19 @@ +import { describe, expect, it, test } from 'vitest'; + +describe('suite name', () => { + describe('nested suite', () => { + it('test inside nested suite', () => { + expect(1 + 1).eq(2); + }); + }); + + test('failing test', () => { + expect(1 + 1).eq(3) + }) +}); + +test('Test in file @42033b7c', ({ task }) => { +}); + +test('Test with console.log', ({ task }) => { +}); diff --git a/vitest-v4/test/callback.test.ts b/vitest-v4/test/callback.test.ts new file mode 100644 index 0000000..5b8406e --- /dev/null +++ b/vitest-v4/test/callback.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it, vi } from 'vitest'; + +function doSomething(callback: (message: string) => void): void { + const message = 'Hello from callback'; + callback(message); +} + +describe('doSomething', () => { + it('should call the callback with the correct message', () => { + const mockCallback = vi.fn(); + + doSomething(mockCallback); + + expect(mockCallback).toHaveBeenCalledWith('Hello from callback'); + }); +}); diff --git a/vitest-v4/test/errors.test.ts b/vitest-v4/test/errors.test.ts new file mode 100644 index 0000000..da7d726 --- /dev/null +++ b/vitest-v4/test/errors.test.ts @@ -0,0 +1,17 @@ +import { describe, expect, it } from 'vitest'; + +function throwErrorIfNeeded(shouldThrow: boolean): void { + if (shouldThrow) { + throw new Error('An error occurred'); + } +} + +describe('throwErrorIfNeeded', () => { + it('should throw an error if shouldThrow is true', () => { + expect(() => throwErrorIfNeeded(true)).toThrow('An error occurred'); + }); + + it('should not throw an error if shouldThrow is false', () => { + expect(() => throwErrorIfNeeded(false)).not.toThrow(); + }); +}); diff --git a/vitest-v4/test/math.test.ts b/vitest-v4/test/math.test.ts new file mode 100644 index 0000000..65d42c1 --- /dev/null +++ b/vitest-v4/test/math.test.ts @@ -0,0 +1,69 @@ +import { describe, expect, it } from 'vitest'; + +function add(a: number, b: number): number { + return a + b; +} + +function subtract(a: number, b: number): number { + return a - b; +} + +function multiply(a: number, b: number): number { + return a * b; +} + +function divide(a: number, b: number): number { + if (b === 0) throw new Error('Division by zero'); + return a / b; +} + +describe('Arithmetic Functions', () => { + it('should correctly add two numbers', () => { + expect(add(2, 3)).toBe(5); + expect(add(-1, 1)).toBe(0); + }); + + it('should correctly subtract two numbers', () => { + expect(subtract(5, 3)).toBe(2); + expect(subtract(0, 5)).toBe(-5); + }); + + it('should correctly multiply two numbers', () => { + expect(multiply(2, 3)).toBe(6); + expect(multiply(-2, 3)).toBe(-6); + }); + + it('should correctly divide two numbers', () => { + expect(divide(6, 3)).toBe(2); + expect(divide(5, 2)).toBe(2.5); + }); + + it('should throw an error when dividing by zero', () => { + expect(() => divide(6, 0)).toThrow('Division by zero'); + }); +}); + +function power(base: number, exponent: number): number { + return Math.pow(base, exponent); +} + +function squareRoot(value: number): number { + if (value < 0) throw new Error('Square root of negative number'); + return Math.sqrt(value); +} + +describe('Power and Square Root Functions', () => { + it('should correctly calculate the power of a number', () => { + expect(power(2, 3)).toBe(8); + expect(power(5, 0)).toBe(1); + }); + + it('should correctly calculate the square root of a number', () => { + expect(squareRoot(4)).toBe(2); + expect(squareRoot(16)).toBe(4); + }); + + it('should throw an error when calculating the square root of a negative number', () => { + expect(() => squareRoot(-1)).toThrow('Square root of negative number'); + }); +}); \ No newline at end of file diff --git a/vitest-v4/test/mock.test.ts b/vitest-v4/test/mock.test.ts new file mode 100644 index 0000000..410ab92 --- /dev/null +++ b/vitest-v4/test/mock.test.ts @@ -0,0 +1,32 @@ +import { describe, expect, it, vi } from 'vitest'; + +function getRandomNumber(): number { + return Math.floor(Math.random() * 100); +} + +function isNumberEven(): boolean { + const num = getRandomNumber(); + return num % 2 === 0; +} + +describe('isNumberEven', () => { + it('should return true if the random number is even', () => { + const mockGetRandomNumber = vi.fn(() => 4); + vi.stubGlobal('getRandomNumber', mockGetRandomNumber); + + const result = isNumberEven(); + expect(result).toBe(true); + + vi.restoreAllMocks(); + }); + + it('should return false if the random number is odd', () => { + const mockGetRandomNumber = vi.fn(() => 3); + vi.stubGlobal('getRandomNumber', mockGetRandomNumber); + + const result = isNumberEven(); + expect(result).toBe(false); + + vi.restoreAllMocks(); + }); +}); diff --git a/vitest-v4/test/promise.test.ts b/vitest-v4/test/promise.test.ts new file mode 100644 index 0000000..9939c17 --- /dev/null +++ b/vitest-v4/test/promise.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from 'vitest'; + +function getNumberAsync(isPositive: boolean): Promise { + return new Promise((resolve, reject) => { + if (isPositive) { + resolve(42); + } else { + reject(new Error('Negative number')); + } + }); +} + +describe('getNumberAsync', () => { + it('should resolve with 42 if isPositive is true', async () => { + const result = await getNumberAsync(true); + expect(result).toBe(42); + }); + + it('should reject with an error if isPositive is false', async () => { + await expect(getNumberAsync(false)).rejects.toThrow('Negative number'); + }); +}); diff --git a/vitest-v4/tsconfig.json b/vitest-v4/tsconfig.json new file mode 100644 index 0000000..1d25015 --- /dev/null +++ b/vitest-v4/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "es2020", + "module": "node16", + "strict": true, + "declaration": true, + "declarationMap": true, + "sourceMap": true, + "verbatimModuleSyntax": true + }, + "include": ["src", "test"], + "exclude": ["node_modules"] +} diff --git a/vitest-v4/vitest.config.ts b/vitest-v4/vitest.config.ts new file mode 100644 index 0000000..14c9b83 --- /dev/null +++ b/vitest-v4/vitest.config.ts @@ -0,0 +1,10 @@ +import { defineConfig } from 'vitest/config'; +import TestomatioReporter from '@testomatio/reporter/vitest'; + +export default defineConfig({ + test: { + reporters: ['verbose', new TestomatioReporter({})], + watch: false, + include: ['test/**/*.test.ts'], + }, +});