@vitest/browser 2.0.0-beta.1 → 2.0.0-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/providers.js CHANGED
@@ -1,111 +1,8 @@
1
- const playwrightBrowsers = ["firefox", "webkit", "chromium"];
2
- class PlaywrightBrowserProvider {
3
- name = "playwright";
4
- cachedBrowser = null;
5
- cachedPage = null;
6
- browser;
7
- ctx;
8
- options;
9
- getSupportedBrowsers() {
10
- return playwrightBrowsers;
11
- }
12
- initialize(project, { browser, options }) {
13
- this.ctx = project;
14
- this.browser = browser;
15
- this.options = options;
16
- }
17
- async openBrowserPage() {
18
- if (this.cachedPage)
19
- return this.cachedPage;
20
- const options = this.ctx.config.browser;
21
- const playwright = await import('playwright');
22
- const browser = await playwright[this.browser].launch({
23
- ...this.options?.launch,
24
- headless: options.headless
25
- });
26
- this.cachedBrowser = browser;
27
- this.cachedPage = await browser.newPage(this.options?.page);
28
- return this.cachedPage;
29
- }
30
- async openPage(url) {
31
- const browserPage = await this.openBrowserPage();
32
- await browserPage.goto(url);
33
- }
34
- async close() {
35
- const page = this.cachedPage;
36
- this.cachedPage = null;
37
- const browser = this.cachedBrowser;
38
- this.cachedBrowser = null;
39
- await page?.close();
40
- await browser?.close();
41
- }
42
- }
43
-
44
- const webdriverBrowsers = ["firefox", "chrome", "edge", "safari"];
45
- class WebdriverBrowserProvider {
46
- name = "webdriverio";
47
- cachedBrowser = null;
48
- browser;
49
- ctx;
50
- options;
51
- getSupportedBrowsers() {
52
- return webdriverBrowsers;
53
- }
54
- async initialize(ctx, { browser, options }) {
55
- this.ctx = ctx;
56
- this.browser = browser;
57
- this.options = options;
58
- }
59
- async openBrowser() {
60
- if (this.cachedBrowser)
61
- return this.cachedBrowser;
62
- const options = this.ctx.config.browser;
63
- if (this.browser === "safari") {
64
- if (options.headless)
65
- throw new Error("You've enabled headless mode for Safari but it doesn't currently support it.");
66
- }
67
- const { remote } = await import('webdriverio');
68
- this.cachedBrowser = await remote({
69
- ...this.options,
70
- logLevel: "error",
71
- capabilities: this.buildCapabilities()
72
- });
73
- return this.cachedBrowser;
74
- }
75
- buildCapabilities() {
76
- const capabilities = {
77
- ...this.options?.capabilities,
78
- browserName: this.browser
79
- };
80
- const headlessMap = {
81
- chrome: ["goog:chromeOptions", ["headless", "disable-gpu"]],
82
- firefox: ["moz:firefoxOptions", ["-headless"]],
83
- edge: ["ms:edgeOptions", ["--headless"]]
84
- };
85
- const options = this.ctx.config.browser;
86
- const browser = this.browser;
87
- if (browser !== "safari" && options.headless) {
88
- const [key, args] = headlessMap[browser];
89
- const currentValues = this.options?.capabilities?.[key] || {};
90
- const newArgs = [...currentValues.args || [], ...args];
91
- capabilities[key] = { ...currentValues, args: newArgs };
92
- }
93
- return capabilities;
94
- }
95
- async openPage(url) {
96
- const browserInstance = await this.openBrowser();
97
- await browserInstance.url(url);
98
- }
99
- async close() {
100
- await Promise.all([
101
- this.cachedBrowser?.sessionId ? this.cachedBrowser?.deleteSession?.() : null
102
- ]);
103
- process.exit();
104
- }
105
- }
1
+ import { W as WebdriverBrowserProvider, P as PlaywrightBrowserProvider } from './webdriver-BRud6NtS.js';
106
2
 
107
- class NoneBrowserProvider {
108
- name = "none";
3
+ class PreviewBrowserProvider {
4
+ name = "preview";
5
+ supportsParallelism = false;
109
6
  ctx;
110
7
  open = false;
111
8
  getSupportedBrowsers() {
@@ -114,20 +11,27 @@ class NoneBrowserProvider {
114
11
  isOpen() {
115
12
  return this.open;
116
13
  }
14
+ getCommandsContext() {
15
+ return {};
16
+ }
117
17
  async initialize(ctx) {
118
18
  this.ctx = ctx;
119
19
  this.open = false;
120
- if (ctx.config.browser.headless)
121
- throw new Error(`You've enabled headless mode for "none" provider but it doesn't support it.`);
20
+ if (ctx.config.browser.headless) {
21
+ throw new Error(
22
+ `You've enabled headless mode for "preview" provider but it doesn't support it. Use "playwright" or "webdriverio" instead: https://vitest.dev/guide/browser#configuration`
23
+ );
24
+ }
122
25
  }
123
- async openPage(_url) {
26
+ async openPage(_contextId, url) {
124
27
  this.open = true;
125
- if (!this.ctx.browser)
28
+ if (!this.ctx.browser) {
126
29
  throw new Error("Browser is not initialized");
127
- const options = this.ctx.browser.config.server;
30
+ }
31
+ const options = this.ctx.browser.vite.config.server;
128
32
  const _open = options.open;
129
- options.open = _url;
130
- this.ctx.browser.openBrowser();
33
+ options.open = url;
34
+ this.ctx.browser.vite.openBrowser();
131
35
  options.open = _open;
132
36
  }
133
37
  async close() {
@@ -136,6 +40,6 @@ class NoneBrowserProvider {
136
40
 
137
41
  const webdriverio = WebdriverBrowserProvider;
138
42
  const playwright = PlaywrightBrowserProvider;
139
- const none = NoneBrowserProvider;
43
+ const preview = PreviewBrowserProvider;
140
44
 
141
- export { none, playwright, webdriverio };
45
+ export { playwright, preview, webdriverio };
package/dist/state.js ADDED
@@ -0,0 +1 @@
1
+ const{parse:$parse,stringify:$stringify}=JSON;const{keys}=Object;const Primitive=String;const primitive="string";const ignore={};const object="object";const noop=(_,value)=>value;const primitives=value=>value instanceof Primitive?Primitive(value):value;const Primitives=(_,value)=>typeof value===primitive?new Primitive(value):value;const revive=(input,parsed,output,$)=>{const lazy=[];for(let ke=keys(output),{length}=ke,y=0;y<length;y++){const k=ke[y];const value=output[k];if(value instanceof Primitive){const tmp=input[value];if(typeof tmp===object&&!parsed.has(tmp)){parsed.add(tmp);output[k]=ignore;lazy.push({k,a:[input,parsed,tmp,$]})}else output[k]=$.call(output,k,tmp)}else if(output[k]!==ignore)output[k]=$.call(output,k,value)}for(let{length}=lazy,i=0;i<length;i++){const{k,a}=lazy[i];output[k]=$.call(output,k,revive.apply(null,a))}return output};const parse=(text,reviver)=>{const input=$parse(text,Primitives).map(primitives);const value=input[0];const $=noop;const tmp=typeof value===object&&value?revive(input,new Set,value,$):value;return $.call({"":tmp},"",tmp)};function getBrowserState(){return window.__vitest_browser_runner__}const config=getBrowserState().config;const providedContext=parse(getBrowserState().providedContext);const state={ctx:{pool:"browser",worker:"./browser.js",workerId:1,config,projectName:config.name||"",files:[],environment:{name:"browser",options:null},providedContext,invalidates:[]},onCancel:null,mockMap:new Map,config,environment:{name:"browser",transformMode:"web",setup(){throw new Error("Not called in the browser")}},moduleCache:getBrowserState().moduleCache,rpc:null,durations:{environment:0,prepare:performance.now()},providedContext};globalThis.__vitest_browser__=true;globalThis.__vitest_worker__=state;
@@ -0,0 +1,180 @@
1
+ const playwrightBrowsers = ["firefox", "webkit", "chromium"];
2
+ class PlaywrightBrowserProvider {
3
+ name = "playwright";
4
+ supportsParallelism = true;
5
+ browser = null;
6
+ browserName;
7
+ ctx;
8
+ options;
9
+ contexts = /* @__PURE__ */ new Map();
10
+ pages = /* @__PURE__ */ new Map();
11
+ browserPromise = null;
12
+ getSupportedBrowsers() {
13
+ return playwrightBrowsers;
14
+ }
15
+ initialize(project, { browser, options }) {
16
+ this.ctx = project;
17
+ this.browserName = browser;
18
+ this.options = options;
19
+ }
20
+ async openBrowser() {
21
+ if (this.browserPromise) {
22
+ return this.browserPromise;
23
+ }
24
+ if (this.browser) {
25
+ return this.browser;
26
+ }
27
+ this.browserPromise = (async () => {
28
+ const options = this.ctx.config.browser;
29
+ const playwright = await import('playwright');
30
+ const browser = await playwright[this.browserName].launch({
31
+ ...this.options?.launch,
32
+ headless: options.headless
33
+ });
34
+ this.browser = browser;
35
+ this.browserPromise = null;
36
+ return this.browser;
37
+ })();
38
+ return this.browserPromise;
39
+ }
40
+ async createContext(contextId) {
41
+ if (this.contexts.has(contextId)) {
42
+ return this.contexts.get(contextId);
43
+ }
44
+ const browser = await this.openBrowser();
45
+ const context = await browser.newContext({
46
+ ...this.options?.context,
47
+ ignoreHTTPSErrors: true,
48
+ serviceWorkers: "allow"
49
+ });
50
+ this.contexts.set(contextId, context);
51
+ return context;
52
+ }
53
+ getPage(contextId) {
54
+ const page = this.pages.get(contextId);
55
+ if (!page) {
56
+ throw new Error(`Page "${contextId}" not found`);
57
+ }
58
+ return page;
59
+ }
60
+ getCommandsContext(contextId) {
61
+ const page = this.getPage(contextId);
62
+ return {
63
+ page,
64
+ context: this.contexts.get(contextId),
65
+ get frame() {
66
+ return page.frame("vitest-iframe");
67
+ }
68
+ };
69
+ }
70
+ async openBrowserPage(contextId) {
71
+ if (this.pages.has(contextId)) {
72
+ const page2 = this.pages.get(contextId);
73
+ await page2.close();
74
+ this.pages.delete(contextId);
75
+ }
76
+ const context = await this.createContext(contextId);
77
+ const page = await context.newPage();
78
+ this.pages.set(contextId, page);
79
+ return page;
80
+ }
81
+ async openPage(contextId, url) {
82
+ const browserPage = await this.openBrowserPage(contextId);
83
+ await browserPage.goto(url);
84
+ }
85
+ async close() {
86
+ const browser = this.browser;
87
+ this.browser = null;
88
+ await Promise.all([...this.pages.values()].map((p) => p.close()));
89
+ this.pages.clear();
90
+ await Promise.all([...this.contexts.values()].map((c) => c.close()));
91
+ this.contexts.clear();
92
+ await browser?.close();
93
+ }
94
+ }
95
+
96
+ const webdriverBrowsers = ["firefox", "chrome", "edge", "safari"];
97
+ class WebdriverBrowserProvider {
98
+ name = "webdriverio";
99
+ supportsParallelism = false;
100
+ browser = null;
101
+ browserName;
102
+ ctx;
103
+ options;
104
+ getSupportedBrowsers() {
105
+ return webdriverBrowsers;
106
+ }
107
+ async initialize(ctx, { browser, options }) {
108
+ this.ctx = ctx;
109
+ this.browserName = browser;
110
+ this.options = options;
111
+ }
112
+ async beforeCommand() {
113
+ const page = this.browser;
114
+ const iframe = await page.findElement(
115
+ "css selector",
116
+ "iframe[data-vitest]"
117
+ );
118
+ await page.switchToFrame(iframe);
119
+ }
120
+ async afterCommand() {
121
+ await this.browser.switchToParentFrame();
122
+ }
123
+ getCommandsContext() {
124
+ return {
125
+ browser: this.browser
126
+ };
127
+ }
128
+ async openBrowser() {
129
+ if (this.browser) {
130
+ return this.browser;
131
+ }
132
+ const options = this.ctx.config.browser;
133
+ if (this.browserName === "safari") {
134
+ if (options.headless) {
135
+ throw new Error(
136
+ "You've enabled headless mode for Safari but it doesn't currently support it."
137
+ );
138
+ }
139
+ }
140
+ const { remote } = await import('webdriverio');
141
+ this.browser = await remote({
142
+ ...this.options,
143
+ logLevel: "error",
144
+ capabilities: this.buildCapabilities()
145
+ });
146
+ return this.browser;
147
+ }
148
+ buildCapabilities() {
149
+ const capabilities = {
150
+ ...this.options?.capabilities,
151
+ browserName: this.browserName
152
+ };
153
+ const headlessMap = {
154
+ chrome: ["goog:chromeOptions", ["headless", "disable-gpu"]],
155
+ firefox: ["moz:firefoxOptions", ["-headless"]],
156
+ edge: ["ms:edgeOptions", ["--headless"]]
157
+ };
158
+ const options = this.ctx.config.browser;
159
+ const browser = this.browserName;
160
+ if (browser !== "safari" && options.headless) {
161
+ const [key, args] = headlessMap[browser];
162
+ const currentValues = this.options?.capabilities?.[key] || {};
163
+ const newArgs = [...currentValues.args || [], ...args];
164
+ capabilities[key] = { ...currentValues, args: newArgs };
165
+ }
166
+ return capabilities;
167
+ }
168
+ async openPage(_contextId, url) {
169
+ const browserInstance = await this.openBrowser();
170
+ await browserInstance.url(url);
171
+ }
172
+ async close() {
173
+ await Promise.all([
174
+ this.browser?.sessionId ? this.browser?.deleteSession?.() : null
175
+ ]);
176
+ process.exit();
177
+ }
178
+ }
179
+
180
+ export { PlaywrightBrowserProvider as P, WebdriverBrowserProvider as W };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/browser",
3
3
  "type": "module",
4
- "version": "2.0.0-beta.1",
4
+ "version": "2.0.0-beta.11",
5
5
  "description": "Browser running for Vitest",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -24,6 +24,10 @@
24
24
  "types": "./providers.d.ts",
25
25
  "default": "./dist/providers.js"
26
26
  },
27
+ "./context": {
28
+ "types": "./context.d.ts",
29
+ "default": "./context.js"
30
+ },
27
31
  "./providers/webdriverio": {
28
32
  "types": "./providers/webdriverio.d.ts"
29
33
  },
@@ -37,13 +41,14 @@
37
41
  "types": "./dist/index.d.ts",
38
42
  "files": [
39
43
  "*.d.ts",
44
+ "context.js",
40
45
  "dist",
41
46
  "providers"
42
47
  ],
43
48
  "peerDependencies": {
44
49
  "playwright": "*",
45
50
  "webdriverio": "*",
46
- "vitest": "2.0.0-beta.1"
51
+ "vitest": "2.0.0-beta.11"
47
52
  },
48
53
  "peerDependenciesMeta": {
49
54
  "playwright": {
@@ -57,22 +62,29 @@
57
62
  }
58
63
  },
59
64
  "dependencies": {
65
+ "@testing-library/dom": "^10.1.0",
66
+ "@testing-library/user-event": "^14.5.2",
60
67
  "magic-string": "^0.30.10",
68
+ "msw": "^2.3.1",
61
69
  "sirv": "^2.0.4",
62
- "@vitest/utils": "2.0.0-beta.1"
70
+ "ws": "^8.17.1",
71
+ "@vitest/utils": "2.0.0-beta.11"
63
72
  },
64
73
  "devDependencies": {
65
74
  "@types/ws": "^8.5.10",
66
- "@wdio/protocols": "^8.32.0",
75
+ "@wdio/protocols": "^8.38.0",
76
+ "birpc": "0.2.17",
77
+ "flatted": "^3.3.1",
78
+ "pathe": "^1.1.2",
67
79
  "periscopic": "^4.0.2",
68
- "playwright": "^1.44.0",
69
- "playwright-core": "^1.44.0",
80
+ "playwright": "^1.44.1",
81
+ "playwright-core": "^1.44.1",
70
82
  "safaridriver": "^0.1.2",
71
- "webdriverio": "^8.36.1",
72
- "@vitest/ws-client": "2.0.0-beta.1",
73
- "@vitest/ui": "2.0.0-beta.1",
74
- "@vitest/runner": "2.0.0-beta.1",
75
- "vitest": "2.0.0-beta.1"
83
+ "webdriverio": "^8.38.2",
84
+ "@vitest/ws-client": "2.0.0-beta.11",
85
+ "@vitest/runner": "2.0.0-beta.11",
86
+ "vitest": "2.0.0-beta.11",
87
+ "@vitest/ui": "2.0.0-beta.11"
76
88
  },
77
89
  "scripts": {
78
90
  "build": "rimraf dist && pnpm build:node && pnpm build:client",
@@ -1,8 +1,42 @@
1
- import type { Browser, LaunchOptions } from 'playwright'
1
+ import type {
2
+ BrowserContext,
3
+ BrowserContextOptions,
4
+ Frame,
5
+ LaunchOptions,
6
+ Page,
7
+ } from 'playwright'
2
8
 
3
9
  declare module 'vitest/node' {
4
10
  interface BrowserProviderOptions {
5
11
  launch?: LaunchOptions
6
- page?: Parameters<Browser['newPage']>[0]
12
+ context?: Omit<
13
+ BrowserContextOptions,
14
+ 'ignoreHTTPSErrors' | 'serviceWorkers'
15
+ >
7
16
  }
17
+
18
+ export interface BrowserCommandContext {
19
+ page: Page
20
+ frame: Frame
21
+ context: BrowserContext
22
+ }
23
+ }
24
+
25
+ type PWHoverOptions = Parameters<Page['hover']>[1]
26
+ type PWClickOptions = Parameters<Page['click']>[1]
27
+ type PWDoubleClickOptions = Parameters<Page['dblclick']>[1]
28
+ type PWFillOptions = Parameters<Page['fill']>[2]
29
+ type PWScreenshotOptions = Parameters<Page['screenshot']>[0]
30
+ type PWSelectOptions = Parameters<Page['selectOption']>[2]
31
+ type PWDragAndDropOptions = Parameters<Page['dragAndDrop']>[2]
32
+
33
+ declare module '@vitest/browser/context' {
34
+ export interface UserEventHoverOptions extends PWHoverOptions {}
35
+ export interface UserEventClickOptions extends PWClickOptions {}
36
+ export interface UserEventDoubleClickOptions extends PWDoubleClickOptions {}
37
+ export interface UserEventFillOptions extends PWFillOptions {}
38
+ export interface UserEventSelectOptions extends PWSelectOptions {}
39
+ export interface UserEventDragOptions extends UserEventDragAndDropOptions {}
40
+
41
+ export interface ScreenshotOptions extends PWScreenshotOptions {}
8
42
  }
@@ -2,4 +2,8 @@ import type { RemoteOptions } from 'webdriverio'
2
2
 
3
3
  declare module 'vitest/node' {
4
4
  interface BrowserProviderOptions extends RemoteOptions {}
5
+
6
+ export interface BrowserCommandContext {
7
+ browser: WebdriverIO.Browser
8
+ }
5
9
  }
package/providers.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import type { BrowserProvider } from 'vitest/node'
1
+ import type { BrowserProviderModule } from 'vitest/node'
2
2
 
3
- declare const webdriverio: BrowserProvider
4
- declare const playwright: BrowserProvider
5
- declare const none: BrowserProvider
3
+ declare const webdriverio: BrowserProviderModule
4
+ declare const playwright: BrowserProviderModule
5
+ declare const preview: BrowserProviderModule
6
6
 
7
- export { webdriverio, playwright, none }
7
+ export { webdriverio, playwright, preview }