@vitest/browser 1.6.0 → 2.0.0-beta.10

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-CXn0ag9T.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,19 +11,22 @@ 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
20
  if (ctx.config.browser.headless)
121
- throw new Error(`You've enabled headless mode for "none" provider but it doesn't support it.`);
21
+ throw new Error(`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`);
122
22
  }
123
- async openPage(_url) {
23
+ async openPage(_contextId, url) {
124
24
  this.open = true;
125
25
  if (!this.ctx.browser)
126
26
  throw new Error("Browser is not initialized");
127
27
  const options = this.ctx.browser.config.server;
128
28
  const _open = options.open;
129
- options.open = _url;
29
+ options.open = url;
130
30
  this.ctx.browser.openBrowser();
131
31
  options.open = _open;
132
32
  }
@@ -136,6 +36,6 @@ class NoneBrowserProvider {
136
36
 
137
37
  const webdriverio = WebdriverBrowserProvider;
138
38
  const playwright = PlaywrightBrowserProvider;
139
- const none = NoneBrowserProvider;
39
+ const preview = PreviewBrowserProvider;
140
40
 
141
- export { none, playwright, webdriverio };
41
+ export { playwright, preview, webdriverio };
@@ -0,0 +1,170 @@
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
+ if (this.browser)
24
+ return this.browser;
25
+ this.browserPromise = (async () => {
26
+ const options = this.ctx.config.browser;
27
+ const playwright = await import('playwright');
28
+ const browser = await playwright[this.browserName].launch({
29
+ ...this.options?.launch,
30
+ headless: options.headless
31
+ });
32
+ this.browser = browser;
33
+ this.browserPromise = null;
34
+ return this.browser;
35
+ })();
36
+ return this.browserPromise;
37
+ }
38
+ async createContext(contextId) {
39
+ if (this.contexts.has(contextId))
40
+ return this.contexts.get(contextId);
41
+ const browser = await this.openBrowser();
42
+ const context = await browser.newContext({
43
+ ...this.options?.context,
44
+ ignoreHTTPSErrors: true,
45
+ serviceWorkers: "allow"
46
+ });
47
+ this.contexts.set(contextId, context);
48
+ return context;
49
+ }
50
+ getPage(contextId) {
51
+ const page = this.pages.get(contextId);
52
+ if (!page)
53
+ throw new Error(`Page "${contextId}" not found`);
54
+ return page;
55
+ }
56
+ getCommandsContext(contextId) {
57
+ const page = this.getPage(contextId);
58
+ const tester = page.frameLocator("iframe[data-vitest]");
59
+ return {
60
+ page,
61
+ tester,
62
+ get body() {
63
+ return page.frameLocator("iframe[data-vitest]").locator("body");
64
+ }
65
+ };
66
+ }
67
+ async openBrowserPage(contextId) {
68
+ if (this.pages.has(contextId)) {
69
+ const page2 = this.pages.get(contextId);
70
+ await page2.close();
71
+ this.pages.delete(contextId);
72
+ }
73
+ const context = await this.createContext(contextId);
74
+ const page = await context.newPage();
75
+ this.pages.set(contextId, page);
76
+ return page;
77
+ }
78
+ async openPage(contextId, url) {
79
+ const browserPage = await this.openBrowserPage(contextId);
80
+ await browserPage.goto(url);
81
+ }
82
+ async close() {
83
+ const browser = this.browser;
84
+ this.browser = null;
85
+ await Promise.all([...this.pages.values()].map((p) => p.close()));
86
+ this.pages.clear();
87
+ await Promise.all([...this.contexts.values()].map((c) => c.close()));
88
+ this.contexts.clear();
89
+ await browser?.close();
90
+ }
91
+ }
92
+
93
+ const webdriverBrowsers = ["firefox", "chrome", "edge", "safari"];
94
+ class WebdriverBrowserProvider {
95
+ name = "webdriverio";
96
+ supportsParallelism = false;
97
+ browser = null;
98
+ browserName;
99
+ ctx;
100
+ options;
101
+ getSupportedBrowsers() {
102
+ return webdriverBrowsers;
103
+ }
104
+ async initialize(ctx, { browser, options }) {
105
+ this.ctx = ctx;
106
+ this.browserName = browser;
107
+ this.options = options;
108
+ }
109
+ async beforeCommand() {
110
+ const page = this.browser;
111
+ const iframe = await page.findElement("css selector", "iframe[data-vitest]");
112
+ await page.switchToFrame(iframe);
113
+ }
114
+ async afterCommand() {
115
+ await this.browser.switchToParentFrame();
116
+ }
117
+ getCommandsContext() {
118
+ return {
119
+ browser: this.browser
120
+ };
121
+ }
122
+ async openBrowser() {
123
+ if (this.browser)
124
+ return this.browser;
125
+ const options = this.ctx.config.browser;
126
+ if (this.browserName === "safari") {
127
+ if (options.headless)
128
+ throw new Error("You've enabled headless mode for Safari but it doesn't currently support it.");
129
+ }
130
+ const { remote } = await import('webdriverio');
131
+ this.browser = await remote({
132
+ ...this.options,
133
+ logLevel: "error",
134
+ capabilities: this.buildCapabilities()
135
+ });
136
+ return this.browser;
137
+ }
138
+ buildCapabilities() {
139
+ const capabilities = {
140
+ ...this.options?.capabilities,
141
+ browserName: this.browserName
142
+ };
143
+ const headlessMap = {
144
+ chrome: ["goog:chromeOptions", ["headless", "disable-gpu"]],
145
+ firefox: ["moz:firefoxOptions", ["-headless"]],
146
+ edge: ["ms:edgeOptions", ["--headless"]]
147
+ };
148
+ const options = this.ctx.config.browser;
149
+ const browser = this.browserName;
150
+ if (browser !== "safari" && options.headless) {
151
+ const [key, args] = headlessMap[browser];
152
+ const currentValues = this.options?.capabilities?.[key] || {};
153
+ const newArgs = [...currentValues.args || [], ...args];
154
+ capabilities[key] = { ...currentValues, args: newArgs };
155
+ }
156
+ return capabilities;
157
+ }
158
+ async openPage(_contextId, url) {
159
+ const browserInstance = await this.openBrowser();
160
+ await browserInstance.url(url);
161
+ }
162
+ async close() {
163
+ await Promise.all([
164
+ this.browser?.sessionId ? this.browser?.deleteSession?.() : null
165
+ ]);
166
+ process.exit();
167
+ }
168
+ }
169
+
170
+ 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": "1.6.0",
4
+ "version": "2.0.0-beta.10",
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
  },
@@ -43,7 +47,7 @@
43
47
  "peerDependencies": {
44
48
  "playwright": "*",
45
49
  "webdriverio": "*",
46
- "vitest": "1.6.0"
50
+ "vitest": "2.0.0-beta.10"
47
51
  },
48
52
  "peerDependenciesMeta": {
49
53
  "playwright": {
@@ -57,22 +61,28 @@
57
61
  }
58
62
  },
59
63
  "dependencies": {
60
- "magic-string": "^0.30.5",
64
+ "@testing-library/dom": "^10.1.0",
65
+ "@testing-library/user-event": "^14.5.2",
66
+ "magic-string": "^0.30.10",
67
+ "msw": "^2.3.1",
61
68
  "sirv": "^2.0.4",
62
- "@vitest/utils": "1.6.0"
69
+ "@vitest/utils": "2.0.0-beta.10"
63
70
  },
64
71
  "devDependencies": {
65
- "@types/ws": "^8.5.9",
66
- "@wdio/protocols": "^8.29.7",
72
+ "@types/ws": "^8.5.10",
73
+ "@wdio/protocols": "^8.38.0",
74
+ "birpc": "0.2.17",
75
+ "flatted": "^3.3.1",
76
+ "pathe": "^1.1.2",
67
77
  "periscopic": "^4.0.2",
68
- "playwright": "^1.41.0",
69
- "playwright-core": "^1.41.0",
78
+ "playwright": "^1.44.1",
79
+ "playwright-core": "^1.44.1",
70
80
  "safaridriver": "^0.1.2",
71
- "webdriverio": "^8.32.2",
72
- "@vitest/runner": "1.6.0",
73
- "@vitest/ui": "1.6.0",
74
- "@vitest/ws-client": "1.6.0",
75
- "vitest": "1.6.0"
81
+ "webdriverio": "^8.38.2",
82
+ "@vitest/runner": "2.0.0-beta.10",
83
+ "@vitest/ui": "2.0.0-beta.10",
84
+ "@vitest/ws-client": "2.0.0-beta.10",
85
+ "vitest": "2.0.0-beta.10"
76
86
  },
77
87
  "scripts": {
78
88
  "build": "rimraf dist && pnpm build:node && pnpm build:client",
@@ -1,8 +1,20 @@
1
- import type { Browser, LaunchOptions } from 'playwright'
1
+ import type {
2
+ BrowserContextOptions,
3
+ FrameLocator,
4
+ LaunchOptions,
5
+ Locator,
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<BrowserContextOptions, 'ignoreHTTPSErrors' | 'serviceWorkers'>
13
+ }
14
+
15
+ export interface BrowserCommandContext {
16
+ page: Page
17
+ tester: FrameLocator
18
+ body: Locator
7
19
  }
8
20
  }
@@ -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 }