Tests
Les tests vous aident à écrire et maintenir votre code et vous protègent contre les régressions. Les frameworks de test vous aident à ça, vous permettant de décrire des assertions ou des attentes sur le comportement de votre code. Svelte ne vous oriente pas sur tel ou tel framework à utiliser — vous pouvez écrire des tests unitaires, des tests d’intégration, des tests end-to-end en utilisant des solutions comme Vitest, Jasmine, Cypress et Playwright
Tests unitaires et de composants avec Vitest
Les tests unitaires vous permettent de tester des petites parties isolées de votre code. Les tests d’intégration vous permettent de vérifier comment des morceaux de votre application fonctionnent ensemble. Si vous utilisez Vite (notamment via SvelteKit), nous vous recommandons d’utiliser Vitest. Vous pouvez utiliser le CLI de Svelte pour mettre en place Vitest soit lors de la création du projet, soit plus tard.
Pour mettre en place Vitest manuellement, commencez par installer Vitest :
npm install -D vitestPuis ajuster votre fichier vite.config.js :
import { function defineConfig(config: UserConfig): UserConfig (+3 overloads)defineConfig } from 'vitest/config';
export default function defineConfig(config: UserConfig): UserConfig (+3 overloads)defineConfig({
// ...
// Dit à Vitest d'utiliser les points d'entrée `browser` dans les fichiers `package.json`, même si
// Vitest utilise Node
resolve?: AllResolveOptions | undefinedresolve: var process: NodeJS.Processprocess.NodeJS.Process.env: NodeJS.ProcessEnvThe process.env property returns an object containing the user environment.
See environ(7).
An example of this object looks like:
{
TERM: 'xterm-256color',
SHELL: '/usr/local/bin/bash',
USER: 'maciej',
PATH: '~/.bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin',
PWD: '/Users/maciej',
EDITOR: 'vim',
SHLVL: '1',
HOME: '/Users/maciej',
LOGNAME: 'maciej',
_: '/usr/local/bin/node'
}
It is possible to modify this object, but such modifications will not be
reflected outside the Node.js process, or (unless explicitly requested)
to other Worker threads.
In other words, the following example would not work:
node -e 'process.env.foo = "bar"' && echo $foo
While the following will:
import { env } from 'node:process';
env.foo = 'bar';
console.log(env.foo);
Assigning a property on process.env will implicitly convert the value
to a string. This behavior is deprecated. Future versions of Node.js may
throw an error when the value is not a string, number, or boolean.
import { env } from 'node:process';
env.test = null;
console.log(env.test);
// => 'null'
env.test = undefined;
console.log(env.test);
// => 'undefined'
Use delete to delete a property from process.env.
import { env } from 'node:process';
env.TEST = 1;
delete env.TEST;
console.log(env.TEST);
// => undefined
On Windows operating systems, environment variables are case-insensitive.
import { env } from 'node:process';
env.TEST = 1;
console.log(env.test);
// => 1
Unless explicitly specified when creating a Worker instance,
each Worker thread has its own copy of process.env, based on its
parent thread’s process.env, or whatever was specified as the env option
to the Worker constructor. Changes to process.env will not be visible
across Worker threads, and only the main thread can make changes that
are visible to the operating system or to native add-ons. On Windows, a copy of process.env on a Worker instance operates in a case-sensitive manner
unlike the main thread.
env.string | undefinedVITEST
? {
EnvironmentResolveOptions.conditions?: string[] | undefinedconditions: ['browser']
}
: var undefinedundefined
});Si charger la version navigateur de tous vos paquets n’est pas possible, parce que vous testez également des librairies backend, vous pourriez avoir également besoin d’une configuration d’alias
Vous pouvez maintenant écrire vos tests unitaires dans des fichiers .js/.ts :
import { function flushSync<T = void>(fn?: (() => T) | undefined): TSynchronously flush any pending updates.
Returns void if no callback is provided, otherwise returns the result of calling the callback.
flushSync } from 'svelte';
import { const expect: ExpectStaticexpect, const test: TestAPIDefines a test case with a given name and test function. The test function can optionally be configured with test options.
test } from 'vitest';
import { import multipliermultiplier } from './multiplier.svelte.js';
test<object>(name: string | Function, fn?: TestFunction<object> | undefined, options?: number | TestCollectorOptions): void (+2 overloads)Defines a test case with a given name and test function. The test function can optionally be configured with test options.
test('Multiplier', () => {
let let double: anydouble = import multipliermultiplier(0, 2);
expect<any>(actual: any, message?: string): Assertion<any> (+1 overload)expect(let double: anydouble.value).JestAssertion<any>.toEqual: <number>(expected: number) => voidUsed when you want to check that two objects have the same value.
This matcher recursively checks the equality of all fields, rather than checking for object identity.
toEqual(0);
let double: anydouble.set(5);
expect<any>(actual: any, message?: string): Assertion<any> (+1 overload)expect(let double: anydouble.value).JestAssertion<any>.toEqual: <number>(expected: number) => voidUsed when you want to check that two objects have the same value.
This matcher recursively checks the equality of all fields, rather than checking for object identity.
toEqual(10);
});/**
* @param {number} initial
* @param {number} k
*/
export function function multiplier(initial: number, k: number): {
readonly value: number;
set: (c: number) => void;
}
multiplier(initial: numberinitial, k: numberk) {
let let count: numbercount = function $state<number>(initial: number): number (+1 overload)
namespace $state
$state(initial: numberinitial);
return {
get value: numbervalue() {
return let count: numbercount * k: numberk;
},
/** @param {number} c */
set: (c: number) => voidset: (c: numberc) => {
let count: numbercount = c: numberc;
}
};
}export function function multiplier(initial: number, k: number): {
readonly value: number;
set: (c: number) => void;
}
multiplier(initial: numberinitial: number, k: numberk: number) {
let let count: numbercount = function $state<number>(initial: number): number (+1 overload)
namespace $state
$state(initial: numberinitial);
return {
get value: numbervalue() {
return let count: numbercount * k: numberk;
},
set: (c: number) => voidset: (c: numberc: number) => {
let count: numbercount = c: numberc;
}
};
}Utiliser les runes dans vos fichiers de test
Puisque Vitest traite vos fichiers de test de la même façon que vos fichiers source, vous pouvez
utiliser des runes au sein de vos tests tant que leur nom de fichier inclut .svelte :
import { function flushSync<T = void>(fn?: (() => T) | undefined): TSynchronously flush any pending updates.
Returns void if no callback is provided, otherwise returns the result of calling the callback.
flushSync } from 'svelte';
import { const expect: ExpectStaticexpect, const test: TestAPIDefines a test case with a given name and test function. The test function can optionally be configured with test options.
test } from 'vitest';
import { import multipliermultiplier } from './multiplier.svelte.js';
test<object>(name: string | Function, fn?: TestFunction<object> | undefined, options?: number | TestCollectorOptions): void (+2 overloads)Defines a test case with a given name and test function. The test function can optionally be configured with test options.
test('Multiplier', () => {
let let count: numbercount = function $state<0>(initial: 0): 0 (+1 overload)
namespace $state
$state(0);
let let double: anydouble = import multipliermultiplier(() => let count: numbercount, 2);
expect<any>(actual: any, message?: string): Assertion<any> (+1 overload)expect(let double: anydouble.value).JestAssertion<any>.toEqual: <number>(expected: number) => voidUsed when you want to check that two objects have the same value.
This matcher recursively checks the equality of all fields, rather than checking for object identity.
toEqual(0);
let count: numbercount = 5;
expect<any>(actual: any, message?: string): Assertion<any> (+1 overload)expect(let double: anydouble.value).JestAssertion<any>.toEqual: <number>(expected: number) => voidUsed when you want to check that two objects have the same value.
This matcher recursively checks the equality of all fields, rather than checking for object identity.
toEqual(10);
});/**
* @param {() => number} getCount
* @param {number} k
*/
export function function multiplier(getCount: () => number, k: number): {
readonly value: number;
}
multiplier(getCount: () => numbergetCount, k: numberk) {
return {
get value: numbervalue() {
return getCount: () => numbergetCount() * k: numberk;
}
};
}export function function multiplier(getCount: () => number, k: number): {
readonly value: number;
}
multiplier(getCount: () => numbergetCount: () => number, k: numberk: number) {
return {
get value: numbervalue() {
return getCount: () => numbergetCount() * k: numberk;
}
};
}Si le code testé utilise des effets, vous aurez besoin de placer le test dans un $effect.root :
import { function flushSync<T = void>(fn?: (() => T) | undefined): TSynchronously flush any pending updates.
Returns void if no callback is provided, otherwise returns the result of calling the callback.
flushSync } from 'svelte';
import { const expect: ExpectStaticexpect, const test: TestAPIDefines a test case with a given name and test function. The test function can optionally be configured with test options.
test } from 'vitest';
import { import loggerlogger } from './logger.svelte.js';
test<object>(name: string | Function, fn?: TestFunction<object> | undefined, options?: number | TestCollectorOptions): void (+2 overloads)Defines a test case with a given name and test function. The test function can optionally be configured with test options.
test('Effet', () => {
const const cleanup: () => voidcleanup = namespace $effect
function $effect(fn: () => void | (() => void)): void
Runs code when a component is mounted to the DOM, and then whenever its dependencies change, i.e. $state or $derived values.
The timing of the execution is after the DOM has been updated.
Example:
$effect(() => console.log('The count is now ' + count));
If you return a function from the effect, it will be called right before the effect is run again, or when the component is unmounted.
Does not run during server-side rendering.
$effect.function $effect.root(fn: () => void | (() => void)): () => voidThe $effect.root rune is an advanced feature that creates a non-tracked scope that doesn’t auto-cleanup. This is useful for
nested effects that you want to manually control. This rune also allows for creation of effects outside of the component
initialisation phase.
Example:
<script>
let count = $state(0);
const cleanup = $effect.root(() => {
$effect(() => {
console.log(count);
})
return () => {
console.log('effect root cleanup');
}
});
</script>
<button onclick={() => cleanup()}>cleanup</button>
root(() => {
let let count: numbercount = function $state<0>(initial: 0): 0 (+1 overload)
namespace $state
$state(0);
// logger utilise un $effect pour afficher les mises à jour de son input
let let log: anylog = import loggerlogger(() => let count: numbercount);
// les effets sont en général exécutés après une micro-tâche,
// utiliser `flushSync` pour exécuter tous les effets en attente de manière synchrone
flushSync<void>(fn?: (() => void) | undefined): voidSynchronously flush any pending updates.
Returns void if no callback is provided, otherwise returns the result of calling the callback.
flushSync();
expect<any>(actual: any, message?: string): Assertion<any> (+1 overload)expect(let log: anylog).JestAssertion<any>.toEqual: <number[]>(expected: number[]) => voidUsed when you want to check that two objects have the same value.
This matcher recursively checks the equality of all fields, rather than checking for object identity.
toEqual([0]);
let count: numbercount = 1;
flushSync<void>(fn?: (() => void) | undefined): voidSynchronously flush any pending updates.
Returns void if no callback is provided, otherwise returns the result of calling the callback.
flushSync();
expect<any>(actual: any, message?: string): Assertion<any> (+1 overload)expect(let log: anylog).JestAssertion<any>.toEqual: <number[]>(expected: number[]) => voidUsed when you want to check that two objects have the same value.
This matcher recursively checks the equality of all fields, rather than checking for object identity.
toEqual([0, 1]);
});
const cleanup: () => voidcleanup();
});/**
* @param {() => any} getValue
*/
export function function logger(getValue: () => any): any[]logger(getValue: () => anygetValue) {
/** @type {any[]} */
let let log: any[]log = [];
function $effect(fn: () => void | (() => void)): void
namespace $effect
Runs code when a component is mounted to the DOM, and then whenever its dependencies change, i.e. $state or $derived values.
The timing of the execution is after the DOM has been updated.
Example:
$effect(() => console.log('The count is now ' + count));
If you return a function from the effect, it will be called right before the effect is run again, or when the component is unmounted.
Does not run during server-side rendering.
$effect(() => {
let log: any[]log.Array<any>.push(...items: any[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(getValue: () => anygetValue());
});
return let log: any[]log;
}export function function logger(getValue: () => any): any[]logger(getValue: () => anygetValue: () => any) {
let let log: any[]log: any[] = [];
function $effect(fn: () => void | (() => void)): void
namespace $effect
Runs code when a component is mounted to the DOM, and then whenever its dependencies change, i.e. $state or $derived values.
The timing of the execution is after the DOM has been updated.
Example:
$effect(() => console.log('The count is now ' + count));
If you return a function from the effect, it will be called right before the effect is run again, or when the component is unmounted.
Does not run during server-side rendering.
$effect(() => {
let log: any[]log.Array<any>.push(...items: any[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(getValue: () => anygetValue());
});
return let log: any[]log;
}Tests de composants
Il est possible de tester vos composants en isolation, ce qui vous permet de les rendre dans un navigateur (réel ou simulé), de simuler leur comportement, et faire des assertions, sans démarrer toute votre application.
Avant d’écrire des tests de composant, posez-vous la question de si vous avez réellement besoin de tester le composant, ou si vous souhaitez plutôt tester la logique au sein de votre composant. Si c’est le cas, envisagez d’extraire cette logique afin de pouvoir la tester indépendamment du composant.
Pour commencer, installer jsdom (une librairie qui simule les APIs du DOM) :
npm install -D jsdomPuis ajustez votre vite.config.js :
import { function defineConfig(config: UserConfig): UserConfig (+3 overloads)defineConfig } from 'vitest/config';
export default function defineConfig(config: UserConfig): UserConfig (+3 overloads)defineConfig({
UserConfig.plugins?: PluginOption[] | undefinedArray of vite plugins to use.
plugins: [
/* ... */
],
UserConfig.test?: InlineConfig | undefinedOptions for Vitest
test: {
// Si vous testez des composants côté client, vous aurez besoin de mettre en place un
// environnement DOM. Si tous vos fichiers ne sont pas compatibles avec cet environnement, vous
// pouvez plutôt ajouter un commentaire `// @vitest-environment jsdom` en haut des fichiers de
// test.
InlineConfig.environment?: VitestEnvironment | undefinedRunning environment
Supports ‘node’, ‘jsdom’, ‘happy-dom’, ‘edge-runtime’
If used unsupported string, will try to load the package vitest-environment-${env}
environment: 'jsdom'
},
// Dit à Vitest d'utiliser les points d'entrée `browser` dans les fichiers `package.json`, même si
// Vitest utilise Node
resolve?: AllResolveOptions | undefinedresolve: var process: NodeJS.Processprocess.NodeJS.Process.env: NodeJS.ProcessEnvThe process.env property returns an object containing the user environment.
See environ(7).
An example of this object looks like:
{
TERM: 'xterm-256color',
SHELL: '/usr/local/bin/bash',
USER: 'maciej',
PATH: '~/.bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin',
PWD: '/Users/maciej',
EDITOR: 'vim',
SHLVL: '1',
HOME: '/Users/maciej',
LOGNAME: 'maciej',
_: '/usr/local/bin/node'
}
It is possible to modify this object, but such modifications will not be
reflected outside the Node.js process, or (unless explicitly requested)
to other Worker threads.
In other words, the following example would not work:
node -e 'process.env.foo = "bar"' &#x26;&#x26; echo $foo
While the following will:
import { env } from 'node:process';
env.foo = 'bar';
console.log(env.foo);
Assigning a property on process.env will implicitly convert the value
to a string. This behavior is deprecated. Future versions of Node.js may
throw an error when the value is not a string, number, or boolean.
import { env } from 'node:process';
env.test = null;
console.log(env.test);
// => 'null'
env.test = undefined;
console.log(env.test);
// => 'undefined'
Use delete to delete a property from process.env.
import { env } from 'node:process';
env.TEST = 1;
delete env.TEST;
console.log(env.TEST);
// => undefined
On Windows operating systems, environment variables are case-insensitive.
import { env } from 'node:process';
env.TEST = 1;
console.log(env.test);
// => 1
Unless explicitly specified when creating a Worker instance,
each Worker thread has its own copy of process.env, based on its
parent thread’s process.env, or whatever was specified as the env option
to the Worker constructor. Changes to process.env will not be visible
across Worker threads, and only the main thread can make changes that
are visible to the operating system or to native add-ons. On Windows, a copy of process.env on a Worker instance operates in a case-sensitive manner
unlike the main thread.
env.string | undefinedVITEST
? {
EnvironmentResolveOptions.conditions?: string[] | undefinedconditions: ['browser']
}
: var undefinedundefined
});Vous pouvez ensuite créer un fichier de test dans lequel importer le composant à tester, interagir avec lui programmatiquement et définir les résultats attendus :
import { function flushSync<T = void>(fn?: (() => T) | undefined): TSynchronously flush any pending updates.
Returns void if no callback is provided, otherwise returns the result of calling the callback.
flushSync, function mount<Props extends Record<string, any>, Exports extends Record<string, any>>(component: ComponentType<SvelteComponent<Props>> | Component<Props, Exports, any>, options: MountOptions<Props>): ExportsMounts a component to the given target and returns the exports and potentially the props (if compiled with accessors: true) of the component.
Transitions will play during the initial render unless the intro option is set to false.
mount, function unmount(component: Record<string, any>, options?: {
outro?: boolean;
} | undefined): Promise<void>
Unmounts a component that was previously mounted using mount or hydrate.
Since 5.13.0, if options.outro is true, transitions will play before the component is removed from the DOM.
Returns a Promise that resolves after transitions have completed if options.outro is true, or immediately otherwise (prior to 5.13.0, returns void).
import { mount, unmount } from 'svelte';
import App from './App.svelte';
const app = mount(App, { target: document.body });
// later...
unmount(app, { outro: true });
unmount } from 'svelte';
import { const expect: ExpectStaticexpect, const test: TestAPIDefines a test case with a given name and test function. The test function can optionally be configured with test options.
test } from 'vitest';
import type Component = SvelteComponent<Record<string, any>, any, any>
const Component: LegacyComponentType
Component from './Component.svelte';
test<object>(name: string | Function, fn?: TestFunction<object> | undefined, options?: number | TestCollectorOptions): void (+2 overloads)Defines a test case with a given name and test function. The test function can optionally be configured with test options.
test('Component', () => {
// Instantier le composant en utilisant l'API Svelte `mount`
const const component: {
$on?(type: string, callback: (e: any) => void): () => void;
$set?(props: Partial<Record<string, any>>): void;
} & Record<string, any>
component = mount<Record<string, any>, {
$on?(type: string, callback: (e: any) => void): () => void;
$set?(props: Partial<Record<string, any>>): void;
} & Record<...>>(component: ComponentType<...> | Component<...>, options: MountOptions<...>): {
$on?(type: string, callback: (e: any) => void): () => void;
$set?(props: Partial<Record<string, any>>): void;
} & Record<...>
Mounts a component to the given target and returns the exports and potentially the props (if compiled with accessors: true) of the component.
Transitions will play during the initial render unless the intro option is set to false.
mount(const Component: LegacyComponentTypeComponent, {
target: Document | Element | ShadowRootTarget element where the component will be mounted.
target: var document: Documentdocument.Document.body: HTMLElementSpecifies the beginning and end of the document body.
body, // `document` existe grâce à jsdom
props?: Record<string, any> | undefinedComponent properties.
props: { initial: numberinitial: 0 }
});
expect<string>(actual: string, message?: string): Assertion<string> (+1 overload)expect(var document: Documentdocument.Document.body: HTMLElementSpecifies the beginning and end of the document body.
body.Element.innerHTML: stringinnerHTML).JestAssertion<string>.toBe: <string>(expected: string) => voidChecks that a value is what you expect. It calls Object.is to compare values.
Don’t use toBe with floating-point numbers.
toBe('<button>0</button>');
// Clic sur le bouton, puis synchronisation des changements pour définir les attentes de manière
// synchrone
var document: Documentdocument.Document.body: HTMLElementSpecifies the beginning and end of the document body.
body.ParentNode.querySelector<"button">(selectors: "button"): HTMLButtonElement | null (+4 overloads)Returns the first element that is a descendant of node that matches selectors.
querySelector('button').HTMLElement.click(): voidclick();
flushSync<void>(fn?: (() => void) | undefined): voidSynchronously flush any pending updates.
Returns void if no callback is provided, otherwise returns the result of calling the callback.
flushSync();
expect<string>(actual: string, message?: string): Assertion<string> (+1 overload)expect(var document: Documentdocument.Document.body: HTMLElementSpecifies the beginning and end of the document body.
body.Element.innerHTML: stringinnerHTML).JestAssertion<string>.toBe: <string>(expected: string) => voidChecks that a value is what you expect. It calls Object.is to compare values.
Don’t use toBe with floating-point numbers.
toBe('<button>1</button>');
// Suppression du composant du DOM
function unmount(component: Record<string, any>, options?: {
outro?: boolean;
} | undefined): Promise<void>
Unmounts a component that was previously mounted using mount or hydrate.
Since 5.13.0, if options.outro is true, transitions will play before the component is removed from the DOM.
Returns a Promise that resolves after transitions have completed if options.outro is true, or immediately otherwise (prior to 5.13.0, returns void).
import { mount, unmount } from 'svelte';
import App from './App.svelte';
const app = mount(App, { target: document.body });
// later...
unmount(app, { outro: true });
unmount(const component: {
$on?(type: string, callback: (e: any) => void): () => void;
$set?(props: Partial<Record<string, any>>): void;
} & Record<string, any>
component);
});Même le processus est plutôt simple à mettre en place, celui-ci est aussi bas niveau et plutôt fragile, puisque la structure de composant peut beaucoup évoluer. Des outils comme @testing-library/svelte peuvent aider à industrialiser l’écriture de vos tests. Le test ci-dessus peut ainsi être ré-écrit comme ceci :
import { function render<C extends unknown, Q extends Queries = typeof import("/vercel/path0/node_modules/.pnpm/@testing-library+dom@10.4.0/node_modules/@testing-library/dom/types/queries")>(Component: ComponentType<...>, options?: SvelteComponentOptions<C>, renderOptions?: RenderOptions<Q>): RenderResult<C, Q>Render a component into the document.
render, const screen: Screen<typeof import("/vercel/path0/node_modules/.pnpm/@testing-library+dom@10.4.0/node_modules/@testing-library/dom/types/queries")>screen } from '@testing-library/svelte';
import const userEvent: {
readonly setup: typeof setupMain;
readonly clear: typeof clear;
readonly click: typeof click;
readonly copy: typeof copy;
... 12 more ...;
readonly tab: typeof tab;
}
userEvent from '@testing-library/user-event';
import { const expect: ExpectStaticexpect, const test: TestAPIDefines a test case with a given name and test function. The test function can optionally be configured with test options.
test } from 'vitest';
import type Component = SvelteComponent<Record<string, any>, any, any>
const Component: LegacyComponentType
Component from './Component.svelte';
test<object>(name: string | Function, fn?: TestFunction<object> | undefined, options?: number | TestCollectorOptions): void (+2 overloads)Defines a test case with a given name and test function. The test function can optionally be configured with test options.
test('Component', async () => {
const const user: UserEventuser = const userEvent: {
readonly setup: typeof setupMain;
readonly clear: typeof clear;
readonly click: typeof click;
readonly copy: typeof copy;
... 12 more ...;
readonly tab: typeof tab;
}
userEvent.setup: (options?: Options) => UserEventStart a “session” with userEvent.
All APIs returned by this function share an input device state and a default configuration.
setup();
render<SvelteComponent<Record<string, any>, any, any>, typeof import("/vercel/path0/node_modules/.pnpm/@testing-library+dom@10.4.0/node_modules/@testing-library/dom/types/queries")>(Component: ComponentType<...>, options?: SvelteComponentOptions<...> | undefined, renderOptions?: RenderOptions<...> | undefined): RenderResult<...>Render a component into the document.
render(const Component: LegacyComponentTypeComponent);
const const button: HTMLElementbutton = const screen: Screen<typeof import("/vercel/path0/node_modules/.pnpm/@testing-library+dom@10.4.0/node_modules/@testing-library/dom/types/queries")>screen.getByRole<HTMLElement>(role: ByRoleMatcher, options?: ByRoleOptions | undefined): HTMLElement (+1 overload)getByRole('button');
expect<HTMLElement>(actual: HTMLElement, message?: string): Assertion<HTMLElement> (+1 overload)expect(const button: HTMLElementbutton).toHaveTextContent(0);
await const user: UserEventuser.click: (element: Element) => Promise<void>click(const button: HTMLElementbutton);
expect<HTMLElement>(actual: HTMLElement, message?: string): Assertion<HTMLElement> (+1 overload)expect(const button: HTMLElementbutton).toHaveTextContent(1);
});Lorsque vous écrivez des tests de composant qui impliquent des liaisons à double sens, du contexte
ou des props de snippet, il est recommandé de créer un composant parent spécifiquement pour votre
test, et interagir avec ce composant. @testing-library/svelte montre quelques exemples de cette
technique.
Tests de composant avec Storybook
Storybook est un outil permettant de développer et de documenter des composants d’interface, et peut également être utilisé pour tester vos composants. Les tests sont lancés avec le mode ‘browser’ de Vitest, qui rend vos composants dans un vrai navigateur pour obtenir un environnement de test le plus réaliste possible.
Tout d’abord, installez Storybook (en utilisant l’outil CLI de Svelte) dans
votre projet via npx sv add storybooK et choisissez la configuration recommandée incluant les
fonctionnalités de test.
Si vous utilisez déjà Storybook, et pour plus d’information sur les fonctionnalités de test de
Storybook, lisez la documentation de test de
Storybook.
Vous pouvez créer des stories montrant les variations de composant et tester les interactions avec
la fonction
play,
qui vous permet de simuler le comportement et de faire des assertions en utilisant Testing Library
et les API Vitest.
Voici un exemple de deux stories pouvant être testées, une qui rend un composant LoginForm vide et
une qui simule un utilisateur remplissant le formulaire :
<script module>
import { defineMeta } from '@storybook/addon-svelte-csf';
import { expect, fn } from 'storybook/test';
import LoginForm from './LoginForm.svelte';
const { Story } = defineMeta({
component: LoginForm,
args: {
// On passe une fonction de mock à la prop `onSubmit`
onSubmit: fn(),
}
});
</script>
<Story name="Formulaire vide" />
<Story
name="Formulaire rempli"
play={async ({ args, canvas, userEvent }) => {
// On simule un utilisateur qui remplir le formulaire
await userEvent.type(canvas.getByTestId('email'), 'email@provider.com');
await userEvent.type(canvas.getByTestId('password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button'));
// On vérifie les assertions
await expect(args.onSubmit).toHaveBeenCalledTimes(1);
await expect(canvas.getByText('You’re in!')).toBeInTheDocument();
}}
/>Tests end-to-end avec Playwright
Les tests E2E (“end to end” en anglais, qui se traduit par “de bout en bout”) vous permettent de tester votre application toute entière depuis le point de vue de vos utilisateurs. Cette section prend Playwright comme exemple, mais vous pouvez aussi utiliser d’autres solutions comme Cypress ou NightwatchJS.
Vous pouvez utiliser le CLI de Svelte pour mettre en place Playwright soit
pendant la création du projet, soit plus tard. Vous pouvez aussi le mettre en place avec npm init playwright. De plus, vous pourriez également vouloir installer
un plugin d’IDE tel que l’extension VSCode
pour être capable d’exécuter des tests depuis votre IDE.
Si vous avez lancé npm init playwright ou n’utilisez pas Vite, vous pourriez avoir besoin
d’ajuster la configuration de Playwright pour lui dire quoi faire avant de lancer les tests —
principalement lancer votre application sur un certain port. Par exemple :
const const config: {
webServer: {
command: string;
port: number;
};
testDir: string;
testMatch: RegExp;
}
config = {
webServer: {
command: string;
port: number;
}
webServer: {
command: stringcommand: 'npm run build && npm run preview',
port: numberport: 4173
},
testDir: stringtestDir: 'tests',
testMatch: RegExptestMatch: /(.+\.)?(test|spec)\.[jt]s/
};
export default const config: {
webServer: {
command: string;
port: number;
};
testDir: string;
testMatch: RegExp;
}
config;Vous pouvez maintenant commencer à écrire vos tests. Ils n’ont aucune conscience que Svelte existe en tant que framework, il vous faudra donc surtout interagir avec le DOM, et écrire vos assertions.
import { import expectexpect, import testtest } from '@playwright/test';
import testtest("la page d'accueil a le h1 prévu", async ({ page }) => {
await page: anypage.goto('/');
await import expectexpect(page: anypage.locator('h1')).toBeVisible();
});Modifier cette page sur Github llms.txt