-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vitest): add Vite plugin for Svelte browser import and autoclean…
- Loading branch information
Showing
6 changed files
with
141 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
import '@testing-library/jest-dom/vitest' | ||
import '../vitest' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { dirname, join } from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
|
||
/** | ||
* Vite plugin to configure @testing-library/svelte. | ||
* | ||
* Ensures Svelte is imported correctly in tests | ||
* and that the DOM is cleaned up after each test. | ||
* | ||
* @param {{resolveBrowser?: boolean, autoCleanup?: boolean}} options | ||
* @returns {import('vite').Plugin} | ||
*/ | ||
export const svelteTesting = ({ | ||
resolveBrowser = true, | ||
autoCleanup = true, | ||
} = {}) => ({ | ||
name: 'vite-plugin-svelte-testing-library', | ||
config: (config) => { | ||
if (!process.env.VITEST) { | ||
return | ||
} | ||
|
||
if (resolveBrowser) { | ||
addBrowserCondition(config) | ||
} | ||
|
||
if (autoCleanup) { | ||
addAutoCleanup(config) | ||
} | ||
}, | ||
}) | ||
|
||
/** | ||
* Add `browser` to `resolve.conditions` before `node`. | ||
* | ||
* This ensures that Svelte's browser code is used in tests, | ||
* rather than its SSR code. | ||
* | ||
* @param {import('vitest/config').UserConfig} config | ||
*/ | ||
const addBrowserCondition = (config) => { | ||
const resolve = config.resolve ?? {} | ||
const conditions = resolve.conditions ?? [] | ||
const nodeConditionIndex = conditions.indexOf('node') | ||
const browserConditionIndex = conditions.indexOf('browser') | ||
|
||
if ( | ||
nodeConditionIndex >= 0 && | ||
(nodeConditionIndex < browserConditionIndex || browserConditionIndex < 0) | ||
) { | ||
conditions.splice(nodeConditionIndex, 0, 'browser') | ||
} | ||
|
||
resolve.conditions = conditions | ||
config.resolve = resolve | ||
} | ||
|
||
/** | ||
* Add auto-cleanup file to Vitest's setup files. | ||
* | ||
* @param {import('vitest/config').UserConfig} config | ||
*/ | ||
const addAutoCleanup = (config) => { | ||
const test = config.test ?? {} | ||
let setupFiles = test.setupFiles ?? [] | ||
|
||
if (typeof setupFiles === 'string') { | ||
setupFiles = [setupFiles] | ||
} | ||
|
||
setupFiles.push(join(dirname(fileURLToPath(import.meta.url)), './vitest.js')) | ||
|
||
test.setupFiles = setupFiles | ||
config.test = test | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { Plugin } from 'vite' | ||
|
||
/** | ||
* Vite plugin to configure @testing-library/svelte. | ||
* | ||
* Ensures Svelte is imported correctly in tests | ||
* and that the DOM is cleaned up after each test. | ||
*/ | ||
export function svelteTesting(options?: { | ||
resolveBrowser?: boolean | ||
autoCleanup?: boolean | ||
}): Plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters