Skip to content

Commit e9fb4c5

Browse files
fix: Split wxt/testing into separate modules to fix issues with jsdom and happy-dom (#1844)
Co-authored-by: Aaron <[email protected]>
1 parent bc54f52 commit e9fb4c5

File tree

7 files changed

+35
-4
lines changed

7 files changed

+35
-4
lines changed

docs/guide/essentials/unit-testing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ WXT provides first class support for Vitest for unit testing:
99
```ts
1010
// vitest.config.ts
1111
import { defineConfig } from 'vitest/config';
12-
import { WxtVitest } from 'wxt/testing';
12+
import { WxtVitest } from 'wxt/testing/vitest-plugin';
1313

1414
export default defineConfig({
1515
plugins: [WxtVitest()],
@@ -36,7 +36,7 @@ This example demonstrates that you don't have to mock `browser.storage` (used by
3636

3737
```ts
3838
import { describe, it, expect } from 'vitest';
39-
import { fakeBrowser } from 'wxt/testing';
39+
import { fakeBrowser } from 'wxt/testing/fake-browser';
4040

4141
const accountStorage = storage.defineItem<Account>('local:account');
4242

packages/wxt/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@
179179
"types": "./dist/browser.d.ts",
180180
"default": "./dist/browser.mjs"
181181
},
182+
"./testing/fake-browser": {
183+
"types": "./dist/testing/fake-browser.d.ts",
184+
"default": "./dist/testing/fake-browser.mjs"
185+
},
186+
"./testing/vitest-plugin": {
187+
"types": "./dist/testing/wxt-vitest-plugin.d.ts",
188+
"default": "./dist/testing/wxt-vitest-plugin.mjs"
189+
},
182190
"./testing": {
183191
"types": "./dist/testing/index.d.ts",
184192
"default": "./dist/testing/index.mjs"

packages/wxt/src/core/builders/vite/plugins/extensionApiMock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function extensionApiMock(config: ResolvedConfig): vite.PluginOption {
4343

4444
const setupTemplate = `
4545
import { vi } from 'vitest';
46-
import { fakeBrowser } from 'wxt/testing';
46+
import { fakeBrowser } from 'wxt/testing/fake-browser';
4747
4848
vi.stubGlobal("chrome", fakeBrowser);
4949
vi.stubGlobal("browser", fakeBrowser);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1+
/**
2+
* The fake browser is automatically used as a mock for the `wxt/browser` import
3+
* when using `wxt/testing/vitest-plugin` with Vitest. It is also setup to
4+
* reset all state before each test.
5+
*
6+
* This module is just a re-export of [@webext-core/fake-browser](https://webext-core.aklinker1.io/fake-browser/triggering-events).
7+
*
8+
* @module wxt/testing/fake-browser
9+
*/
10+
111
export { fakeBrowser, type FakeBrowser } from '@webext-core/fake-browser';

packages/wxt/src/testing/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/**
22
* Utilities for unit testing WXT extensions.
3+
*
4+
* @deprecated Use `wxt/testing/*` instead to prevent issues with JSDOM or
5+
* HappyDOM environments. Will be removed in the next major version of WXT.
6+
*
37
* @module wxt/testing
48
*/
59
export * from './fake-browser';

packages/wxt/src/testing/wxt-vitest-plugin.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Contains a Vitest plugin that configures your test environment to work with
3+
* WXT projects.
4+
*
5+
* @module wxt/testing/vitest
6+
*/
7+
18
import type * as vite from 'vite';
29
import {
310
download,
@@ -16,7 +23,7 @@ import { registerWxt, wxt } from '../core/wxt';
1623
* ```ts
1724
* // vitest.config.ts
1825
* import { defineConfig } from 'vitest/config';
19-
* import { WxtVitest } from 'wxt/testing';
26+
* import { WxtVitest } from 'wxt/testing/vitest-plugin';
2027
*
2128
* export default defineConfig({
2229
* plugins: [WxtVitest()],

packages/wxt/typedoc.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"src/utils/split-shadow-root-css.ts",
1818
"src/utils/storage.ts",
1919
"src/testing/index.ts",
20+
"src/testing/fake-browser.ts",
21+
"src/testing/wxt-vitest-plugin.ts",
2022
"src/modules.ts"
2123
]
2224
}

0 commit comments

Comments
 (0)