Package Exports
- @bemedev/vitest-extended
- @bemedev/vitest-extended/lib/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@bemedev/vitest-extended) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@bemedev/vitest-extended
Extensions pour faciliter la création de tests avec Vitest.
Installation
npm install @bemedev/vitest-extendedou
pnpm add @bemedev/vitest-extendedou
yarn add @bemedev/vitest-extendedUtilisation
Importation des fonctions
import {
useTestFunctionAcceptation,
useTFA /* Alias of useTestFunctionAcceptation*/,
createTests,
doneTest,
useEach,
useEachAsync,
useEachCases,
} from '@bemedev/vitest-extended';Exemples
Création de tests
import { createTests } from '@bemedev/vitest-extended';
const add = (a: number, b: number) => a + b;
describe('Addition', () => {
const { success } = createTests(
add,
{}, // all options (optional)
);
success(
{ invite: 'addition de 1 et 2', parameters: [1, 2], expected: 3 },
{ invite: 'addition de 2 et 3', parameters: [2, 3], expected: 5 },
);
});Utilisation de doneTest
import { doneTest } from '@bemedev/vitest-extended';
import { createMachine, interpret, typings } from 'xstate';
const machine = createMachine(
{
id: 'my-machine',
initial: 'idle',
states: {
idle: {
on: {
START: '/running',
},
},
running: {
on: {
STOP: '/idle',
FINISH: '/done',
},
},
done: { entry: 'onDone' },
},
},
typings({
context: 'boolean',
eventsMap: {
START: 'primitive',
STOP: 'primitive',
FINISH: 'primitive',
},
}),
);
doneTest('test avec done', done => {
const service = interpret(machine, { context: false });
service.addOptions(({ voidAction }) => ({
actions: {
onDone: voidAction(done),
},
}));
service.start();
service.send('START');
service.send('STOP');
service.send('START');
service.send('FINISH');
});API
useTestFunctionAcceptation
Test d'acceptation pour une fonction.
useTFA
Alias de useTFA.
createTests
Crée des tests pour une fonction avec des cas spécifiques.
doneTest
Crée un test avec une fonction done et un timeout.
useEach
Version améliorée de test.each suivant le principe de la librairie.
useEachAsync
Version asynchrone de useEach.
useErrorAsync
Version asynchrone de useError.
NB:
- You can mock the function and assign a value to this function at runtime
import { createTests } from '@bemedev/vitest-extended';
type Add_F = (numb1: number, numb2: number) => number;
const add: Add_F = (numb1, numb2) => numb1 + numb2;
const addTest = undefined as unknown as Add_F;
describe('#2 => CreateTests, funcTest is initialazed after', () => {
const { success } = createTests.withImplementation(addTest, {
instanciation: () => add,
name: 'add', // Add a name because name is not provided at static time
});
describe(
'#01 => success',
success(
{
invite: '0 + 0 = 0',
parameters: [0, 0],
expected: 0,
},
{
invite: '99 + 1 = 0',
parameters: [99, 1],
expected: 100,
},
{
invite: '1 + 1 = 0',
parameters: [1, 1],
expected: 2,
},
),
);
});- And you can transform the function to test
import { createTests } from '@bemedev/vitest-extended';
const noArgs = () => 'no args';
describe('#1 => Transform', () => {
const { success } = createTests(noArgs, { transform: () => 'result' });
describe(
'#01 => success',
success({
invite: 'no args',
parameters: [],
// Here the result is transformed from value "no args" to "result"
expected: 'result',
}),
);
});- And customize the test function
import { createTests } from '@bemedev/vitest-extended';
describe('#01 => Add', () => {
const add = (a: number, b: number) => a + b;
const { success } = createTests(add, x => x.toLocaleString());
describe(
'#01 => success',
success(
{
invite: '1 + 2',
parameters: [1, 2],
expected: '3',
},
{
invite: '1 + 2, with specific test',
parameters: [1, 2],
expected: '31',
test: (value, expected) => {
expect(value + 1).toBe(expected);
},
},
),
);
});Licence
MIT
CHANGE_LOG
Auteur
Charles-Lévi BRI,