From 2295737d32c44b9b08e401c849131487dfc89864 Mon Sep 17 00:00:00 2001 From: Daniel Hatas Date: Sat, 30 Dec 2023 22:15:14 +0100 Subject: [PATCH] [client-svelte] Test env fixes Configure @testing-library/jest-dom correctly to resolve the type definitions in the test files, even the plugin is extended in a global setup file. The Svelte component's onMount hooks right now executed properly. Unfortunately vitest running in node but Svelte render's the components in SSR mode, so the onMount callbacks didn't executed in tests. An ugly workaround was needed to solve this: https://github.com/vitest-dev/vitest/issues/2834#issuecomment-1439576110 --- client-svelte/src/lib/DisplayTodoItem.test.ts | 1 - client-svelte/tsconfig.json | 1 + client-svelte/tsconfig.node.json | 1 - client-svelte/vite.config.ts | 2 +- client-svelte/vitest.config.ts | 4 +++- 5 files changed, 5 insertions(+), 4 deletions(-) diff --git a/client-svelte/src/lib/DisplayTodoItem.test.ts b/client-svelte/src/lib/DisplayTodoItem.test.ts index 0fc4f34..5abb39d 100644 --- a/client-svelte/src/lib/DisplayTodoItem.test.ts +++ b/client-svelte/src/lib/DisplayTodoItem.test.ts @@ -1,4 +1,3 @@ -import '@testing-library/jest-dom' import { describe, it, expect, afterEach, vi } from 'vitest' import { render, fireEvent, screen } from '@testing-library/svelte' diff --git a/client-svelte/tsconfig.json b/client-svelte/tsconfig.json index d310f9c..a4dbb70 100644 --- a/client-svelte/tsconfig.json +++ b/client-svelte/tsconfig.json @@ -14,6 +14,7 @@ "allowJs": true, "checkJs": true, "isolatedModules": true, + "types": ["@testing-library/jest-dom"] }, "include": ["src/**/*.ts", "src/**/*.js", "src/**/*.svelte"], "references": [{ "path": "./tsconfig.node.json" }] diff --git a/client-svelte/tsconfig.node.json b/client-svelte/tsconfig.node.json index 9caf126..548af5e 100644 --- a/client-svelte/tsconfig.node.json +++ b/client-svelte/tsconfig.node.json @@ -4,7 +4,6 @@ "skipLibCheck": true, "module": "ESNext", "moduleResolution": "bundler", - "types": ["vitest/global"] }, "include": ["vite.config.ts"] } diff --git a/client-svelte/vite.config.ts b/client-svelte/vite.config.ts index 70b83f9..09450e6 100644 --- a/client-svelte/vite.config.ts +++ b/client-svelte/vite.config.ts @@ -8,5 +8,5 @@ export default defineConfig({ proxy: { '/api': 'http://localhost:4400' } - } + }, }) diff --git a/client-svelte/vitest.config.ts b/client-svelte/vitest.config.ts index 372d19b..1d84ec4 100644 --- a/client-svelte/vitest.config.ts +++ b/client-svelte/vitest.config.ts @@ -6,5 +6,7 @@ export default defineConfig({ test: { globals: true, environment: 'jsdom', - }, + alias: [{ find: /^svelte$/, replacement: 'svelte/internal' }], + setupFiles: ['./vitest-setup.ts'] + } }) \ No newline at end of file