Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions vitest-v4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules

27 changes: 27 additions & 0 deletions vitest-v4/README.md
Original file line number Diff line number Diff line change
@@ -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=<project_api_key> 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
19 changes: 19 additions & 0 deletions vitest-v4/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
1 change: 1 addition & 0 deletions vitest-v4/src/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const squared = (n: number) => n * n
7 changes: 7 additions & 0 deletions vitest-v4/test/__snapshots__/suite.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`suite name > snapshot 1`] = `
{
"foo": "bar",
}
`;
16 changes: 16 additions & 0 deletions vitest-v4/test/async.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, it, expect } from 'vitest';

async function delayedHello(name: string): Promise<string> {
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!');
});
});
19 changes: 19 additions & 0 deletions vitest-v4/test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -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 }) => {
});
16 changes: 16 additions & 0 deletions vitest-v4/test/callback.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
17 changes: 17 additions & 0 deletions vitest-v4/test/errors.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
69 changes: 69 additions & 0 deletions vitest-v4/test/math.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
32 changes: 32 additions & 0 deletions vitest-v4/test/mock.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
22 changes: 22 additions & 0 deletions vitest-v4/test/promise.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, it } from 'vitest';

function getNumberAsync(isPositive: boolean): Promise<number> {
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');
});
});
13 changes: 13 additions & 0 deletions vitest-v4/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}
10 changes: 10 additions & 0 deletions vitest-v4/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -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'],
},
});
Loading