-
Notifications
You must be signed in to change notification settings - Fork 33
feat(vitest): add Vite plugin for Svelte browser import and autocleanup #362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
64648fe
feat(vitest): add Vite plugin for Svelte browser import and autocleanup
mcous fe2e387
fixup: review feedback + README
mcous a5e362a
fixup: s/vite-plugin/vite/
mcous 023882b
fixup: fix type exports and peer dependencies
mcous 6961573
fixup: ensure `setupFiles` is an array
mcous 15a997b
fixup: do something a little more obvious
mcous File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,71 @@ | ||
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 ?? {} | ||
const setupFiles = test.setupFiles ?? [] | ||
|
||
setupFiles.push(join(dirname(fileURLToPath(import.meta.url)), './vitest.js')) | ||
|
||
test.setupFiles = setupFiles | ||
config.test = test | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setupFiles
may bestring | string[]
, need to account for that case