@vitest/browser 3.1.0-beta.1 → 3.1.0-beta.2

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.
@@ -1,157 +1,154 @@
1
1
  import { page, server } from '@vitest/browser/context';
2
- import { g as getByTitleSelector, a as getByTextSelector, b as getByPlaceholderSelector, c as getByAltTextSelector, d as getByTestIdSelector, e as getByRoleSelector, f as getByLabelSelector, h as getElementError } from '../public-utils-J4vwTaki.js';
3
- import { s as selectorEngine, L as Locator, g as getBrowserState } from '../index-DrTP5i7N.js';
4
- import { c as convertElementToCssSelector, g as getIframeScale } from '../utils-VCysLhWp.js';
2
+ import { g as getByTitleSelector, a as getByTextSelector, b as getByPlaceholderSelector, c as getByAltTextSelector, d as getByTestIdSelector, e as getByRoleSelector, f as getByLabelSelector, h as getElementError } from '../public-utils-4WiYB3_6.js';
3
+ import { s as selectorEngine, L as Locator, c as convertElementToCssSelector, g as getBrowserState, a as getIframeScale } from '../index-VvsEiykv.js';
5
4
  import 'vitest/utils';
6
5
 
7
6
  page.extend({
8
- getByLabelText(text, options) {
9
- return new WebdriverIOLocator(getByLabelSelector(text, options));
10
- },
11
- getByRole(role, options) {
12
- return new WebdriverIOLocator(getByRoleSelector(role, options));
13
- },
14
- getByTestId(testId) {
15
- return new WebdriverIOLocator(getByTestIdSelector(server.config.browser.locators.testIdAttribute, testId));
16
- },
17
- getByAltText(text, options) {
18
- return new WebdriverIOLocator(getByAltTextSelector(text, options));
19
- },
20
- getByPlaceholder(text, options) {
21
- return new WebdriverIOLocator(getByPlaceholderSelector(text, options));
22
- },
23
- getByText(text, options) {
24
- return new WebdriverIOLocator(getByTextSelector(text, options));
25
- },
26
- getByTitle(title, options) {
27
- return new WebdriverIOLocator(getByTitleSelector(title, options));
28
- },
29
- elementLocator(element) {
30
- return new WebdriverIOLocator(selectorEngine.generateSelectorSimple(element));
31
- }
7
+ getByLabelText(text, options) {
8
+ return new WebdriverIOLocator(getByLabelSelector(text, options));
9
+ },
10
+ getByRole(role, options) {
11
+ return new WebdriverIOLocator(getByRoleSelector(role, options));
12
+ },
13
+ getByTestId(testId) {
14
+ return new WebdriverIOLocator(getByTestIdSelector(server.config.browser.locators.testIdAttribute, testId));
15
+ },
16
+ getByAltText(text, options) {
17
+ return new WebdriverIOLocator(getByAltTextSelector(text, options));
18
+ },
19
+ getByPlaceholder(text, options) {
20
+ return new WebdriverIOLocator(getByPlaceholderSelector(text, options));
21
+ },
22
+ getByText(text, options) {
23
+ return new WebdriverIOLocator(getByTextSelector(text, options));
24
+ },
25
+ getByTitle(title, options) {
26
+ return new WebdriverIOLocator(getByTitleSelector(title, options));
27
+ },
28
+ elementLocator(element) {
29
+ return new WebdriverIOLocator(selectorEngine.generateSelectorSimple(element));
30
+ }
32
31
  });
33
32
  class WebdriverIOLocator extends Locator {
34
- constructor(_pwSelector, _container) {
35
- super();
36
- this._pwSelector = _pwSelector;
37
- this._container = _container;
38
- }
39
- get selector() {
40
- const selectors = this.elements().map((element) => convertElementToCssSelector(element));
41
- if (!selectors.length) {
42
- throw getElementError(this._pwSelector, this._container || document.body);
43
- }
44
- return selectors.join(", ");
45
- }
46
- click(options) {
47
- return super.click(processClickOptions(options));
48
- }
49
- dblClick(options) {
50
- return super.dblClick(processClickOptions(options));
51
- }
52
- tripleClick(options) {
53
- return super.tripleClick(processClickOptions(options));
54
- }
55
- selectOptions(value, options) {
56
- const values = getWebdriverioSelectOptions(this.element(), value);
57
- return this.triggerCommand("__vitest_selectOptions", this.selector, values, options);
58
- }
59
- hover(options) {
60
- return super.hover(processHoverOptions(options));
61
- }
62
- dropTo(target, options) {
63
- return super.dropTo(target, processDragAndDropOptions(options));
64
- }
65
- locator(selector) {
66
- return new WebdriverIOLocator(`${this._pwSelector} >> ${selector}`, this._container);
67
- }
68
- elementLocator(element) {
69
- return new WebdriverIOLocator(selectorEngine.generateSelectorSimple(element), element);
70
- }
33
+ constructor(_pwSelector, _container) {
34
+ super();
35
+ this._pwSelector = _pwSelector;
36
+ this._container = _container;
37
+ }
38
+ get selector() {
39
+ const selectors = this.elements().map((element) => convertElementToCssSelector(element));
40
+ if (!selectors.length) {
41
+ throw getElementError(this._pwSelector, this._container || document.body);
42
+ }
43
+ return selectors.join(", ");
44
+ }
45
+ click(options) {
46
+ return super.click(processClickOptions(options));
47
+ }
48
+ dblClick(options) {
49
+ return super.dblClick(processClickOptions(options));
50
+ }
51
+ tripleClick(options) {
52
+ return super.tripleClick(processClickOptions(options));
53
+ }
54
+ selectOptions(value, options) {
55
+ const values = getWebdriverioSelectOptions(this.element(), value);
56
+ return this.triggerCommand("__vitest_selectOptions", this.selector, values, options);
57
+ }
58
+ hover(options) {
59
+ return super.hover(processHoverOptions(options));
60
+ }
61
+ dropTo(target, options) {
62
+ return super.dropTo(target, processDragAndDropOptions(options));
63
+ }
64
+ locator(selector) {
65
+ return new WebdriverIOLocator(`${this._pwSelector} >> ${selector}`, this._container);
66
+ }
67
+ elementLocator(element) {
68
+ return new WebdriverIOLocator(selectorEngine.generateSelectorSimple(element), element);
69
+ }
71
70
  }
72
71
  function getWebdriverioSelectOptions(element, value) {
73
- const options = [...element.querySelectorAll("option")];
74
- const arrayValues = Array.isArray(value) ? value : [value];
75
- if (!arrayValues.length) {
76
- return [];
77
- }
78
- if (arrayValues.length > 1) {
79
- throw new Error(`Provider "webdriverio" doesn't support selecting multiple values at once`);
80
- }
81
- const optionValue = arrayValues[0];
82
- if (typeof optionValue !== "string") {
83
- const element2 = "element" in optionValue ? optionValue.element() : optionValue;
84
- const index = options.indexOf(element2);
85
- if (index === -1) {
86
- throw new Error(`The element ${selectorEngine.previewNode(element2)} was not found in the "select" options.`);
87
- }
88
- return [{ index }];
89
- }
90
- const valueIndex = options.findIndex((option) => option.value === optionValue);
91
- if (valueIndex !== -1) {
92
- return [{ index: valueIndex }];
93
- }
94
- const labelIndex = options.findIndex(
95
- (option) => option.textContent?.trim() === optionValue || option.ariaLabel === optionValue
96
- );
97
- if (labelIndex === -1) {
98
- throw new Error(`The option "${optionValue}" was not found in the "select" options.`);
99
- }
100
- return [{ index: labelIndex }];
72
+ const options = [...element.querySelectorAll("option")];
73
+ const arrayValues = Array.isArray(value) ? value : [value];
74
+ if (!arrayValues.length) {
75
+ return [];
76
+ }
77
+ if (arrayValues.length > 1) {
78
+ throw new Error("Provider \"webdriverio\" doesn't support selecting multiple values at once");
79
+ }
80
+ const optionValue = arrayValues[0];
81
+ if (typeof optionValue !== "string") {
82
+ const element = "element" in optionValue ? optionValue.element() : optionValue;
83
+ const index = options.indexOf(element);
84
+ if (index === -1) {
85
+ throw new Error(`The element ${selectorEngine.previewNode(element)} was not found in the "select" options.`);
86
+ }
87
+ return [{ index }];
88
+ }
89
+ const valueIndex = options.findIndex((option) => option.value === optionValue);
90
+ if (valueIndex !== -1) {
91
+ return [{ index: valueIndex }];
92
+ }
93
+ const labelIndex = options.findIndex((option) => option.textContent?.trim() === optionValue || option.ariaLabel === optionValue);
94
+ if (labelIndex === -1) {
95
+ throw new Error(`The option "${optionValue}" was not found in the "select" options.`);
96
+ }
97
+ return [{ index: labelIndex }];
101
98
  }
102
99
  function processClickOptions(options_) {
103
- if (!options_ || !getBrowserState().config.browser.ui) {
104
- return options_;
105
- }
106
- const options = options_;
107
- if (options.x != null || options.y != null) {
108
- const cache = {};
109
- if (options.x != null) {
110
- options.x = scaleCoordinate(options.x, cache);
111
- }
112
- if (options.y != null) {
113
- options.y = scaleCoordinate(options.y, cache);
114
- }
115
- }
116
- return options_;
100
+ if (!options_ || !getBrowserState().config.browser.ui) {
101
+ return options_;
102
+ }
103
+ const options = options_;
104
+ if (options.x != null || options.y != null) {
105
+ const cache = {};
106
+ if (options.x != null) {
107
+ options.x = scaleCoordinate(options.x, cache);
108
+ }
109
+ if (options.y != null) {
110
+ options.y = scaleCoordinate(options.y, cache);
111
+ }
112
+ }
113
+ return options_;
117
114
  }
118
115
  function processHoverOptions(options_) {
119
- if (!options_ || !getBrowserState().config.browser.ui) {
120
- return options_;
121
- }
122
- const options = options_;
123
- const cache = {};
124
- if (options.xOffset != null) {
125
- options.xOffset = scaleCoordinate(options.xOffset, cache);
126
- }
127
- if (options.yOffset != null) {
128
- options.yOffset = scaleCoordinate(options.yOffset, cache);
129
- }
130
- return options_;
116
+ if (!options_ || !getBrowserState().config.browser.ui) {
117
+ return options_;
118
+ }
119
+ const options = options_;
120
+ const cache = {};
121
+ if (options.xOffset != null) {
122
+ options.xOffset = scaleCoordinate(options.xOffset, cache);
123
+ }
124
+ if (options.yOffset != null) {
125
+ options.yOffset = scaleCoordinate(options.yOffset, cache);
126
+ }
127
+ return options_;
131
128
  }
132
129
  function processDragAndDropOptions(options_) {
133
- if (!options_ || !getBrowserState().config.browser.ui) {
134
- return options_;
135
- }
136
- const cache = {};
137
- const options = options_;
138
- if (options.sourceX != null) {
139
- options.sourceX = scaleCoordinate(options.sourceX, cache);
140
- }
141
- if (options.sourceY != null) {
142
- options.sourceY = scaleCoordinate(options.sourceY, cache);
143
- }
144
- if (options.targetX != null) {
145
- options.targetX = scaleCoordinate(options.targetX, cache);
146
- }
147
- if (options.targetY != null) {
148
- options.targetY = scaleCoordinate(options.targetY, cache);
149
- }
150
- return options_;
130
+ if (!options_ || !getBrowserState().config.browser.ui) {
131
+ return options_;
132
+ }
133
+ const cache = {};
134
+ const options = options_;
135
+ if (options.sourceX != null) {
136
+ options.sourceX = scaleCoordinate(options.sourceX, cache);
137
+ }
138
+ if (options.sourceY != null) {
139
+ options.sourceY = scaleCoordinate(options.sourceY, cache);
140
+ }
141
+ if (options.targetX != null) {
142
+ options.targetX = scaleCoordinate(options.targetX, cache);
143
+ }
144
+ if (options.targetY != null) {
145
+ options.targetY = scaleCoordinate(options.targetY, cache);
146
+ }
147
+ return options_;
151
148
  }
152
149
  function scaleCoordinate(coordinate, cache) {
153
- return Math.round(coordinate * getCachedScale(cache));
150
+ return Math.round(coordinate * getCachedScale(cache));
154
151
  }
155
152
  function getCachedScale(cache) {
156
- return cache.scale ??= getIframeScale();
153
+ return cache.scale ??= getIframeScale();
157
154
  }
package/dist/providers.js CHANGED
@@ -1,42 +1,39 @@
1
- import { W as WebdriverBrowserProvider, P as PlaywrightBrowserProvider } from './webdriver-C5-VI7VH.js';
1
+ import { W as WebdriverBrowserProvider, P as PlaywrightBrowserProvider } from './webdriver-BP2w_ajA.js';
2
2
 
3
3
  class PreviewBrowserProvider {
4
- name = "preview";
5
- supportsParallelism = false;
6
- project;
7
- open = false;
8
- getSupportedBrowsers() {
9
- return [];
10
- }
11
- isOpen() {
12
- return this.open;
13
- }
14
- getCommandsContext() {
15
- return {};
16
- }
17
- async initialize(project) {
18
- this.project = project;
19
- this.open = false;
20
- if (project.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
- }
25
- project.vitest.logger.printBrowserBanner(project);
26
- }
27
- async openPage(_sessionId, url) {
28
- this.open = true;
29
- if (!this.project.browser) {
30
- throw new Error("Browser is not initialized");
31
- }
32
- const options = this.project.browser.vite.config.server;
33
- const _open = options.open;
34
- options.open = url;
35
- this.project.browser.vite.openBrowser();
36
- options.open = _open;
37
- }
38
- async close() {
39
- }
4
+ name = "preview";
5
+ supportsParallelism = false;
6
+ project;
7
+ open = false;
8
+ getSupportedBrowsers() {
9
+ return [];
10
+ }
11
+ isOpen() {
12
+ return this.open;
13
+ }
14
+ getCommandsContext() {
15
+ return {};
16
+ }
17
+ async initialize(project) {
18
+ this.project = project;
19
+ this.open = false;
20
+ if (project.config.browser.headless) {
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");
22
+ }
23
+ project.vitest.logger.printBrowserBanner(project);
24
+ }
25
+ async openPage(_sessionId, url) {
26
+ this.open = true;
27
+ if (!this.project.browser) {
28
+ throw new Error("Browser is not initialized");
29
+ }
30
+ const options = this.project.browser.vite.config.server;
31
+ const _open = options.open;
32
+ options.open = url;
33
+ this.project.browser.vite.openBrowser();
34
+ options.open = _open;
35
+ }
36
+ async close() {}
40
37
  }
41
38
 
42
39
  const webdriverio = WebdriverBrowserProvider;
@@ -5510,52 +5510,50 @@ function getByRoleSelector(role, options = {}) {
5510
5510
  }
5511
5511
 
5512
5512
  function getElementLocatorSelectors(element) {
5513
- const locator = page.elementLocator(element);
5514
- return {
5515
- getByAltText: (altText, options) => locator.getByAltText(altText, options),
5516
- getByLabelText: (labelText, options) => locator.getByLabelText(labelText, options),
5517
- getByPlaceholder: (placeholderText, options) => locator.getByPlaceholder(placeholderText, options),
5518
- getByRole: (role, options) => locator.getByRole(role, options),
5519
- getByTestId: (testId) => locator.getByTestId(testId),
5520
- getByText: (text, options) => locator.getByText(text, options),
5521
- getByTitle: (title, options) => locator.getByTitle(title, options)
5522
- };
5513
+ const locator = page.elementLocator(element);
5514
+ return {
5515
+ getByAltText: (altText, options) => locator.getByAltText(altText, options),
5516
+ getByLabelText: (labelText, options) => locator.getByLabelText(labelText, options),
5517
+ getByPlaceholder: (placeholderText, options) => locator.getByPlaceholder(placeholderText, options),
5518
+ getByRole: (role, options) => locator.getByRole(role, options),
5519
+ getByTestId: (testId) => locator.getByTestId(testId),
5520
+ getByText: (text, options) => locator.getByText(text, options),
5521
+ getByTitle: (title, options) => locator.getByTitle(title, options)
5522
+ };
5523
5523
  }
5524
5524
  function debug(el, maxLength, options) {
5525
- if (Array.isArray(el)) {
5526
- el.forEach((e) => console.log(prettyDOM(e, maxLength, options)));
5527
- } else {
5528
- console.log(prettyDOM(el, maxLength, options));
5529
- }
5525
+ if (Array.isArray(el)) {
5526
+ el.forEach((e) => console.log(prettyDOM(e, maxLength, options)));
5527
+ } else {
5528
+ console.log(prettyDOM(el, maxLength, options));
5529
+ }
5530
5530
  }
5531
5531
  function prettyDOM(dom, maxLength = Number(import.meta.env.DEBUG_PRINT_LIMIT ?? 7e3), prettyFormatOptions = {}) {
5532
- if (maxLength === 0) {
5533
- return "";
5534
- }
5535
- if (!dom) {
5536
- dom = document.body;
5537
- }
5538
- if ("element" in dom && "all" in dom) {
5539
- dom = dom.element();
5540
- }
5541
- const type = typeof dom;
5542
- if (type !== "object" || !dom.outerHTML) {
5543
- const typeName = type === "object" ? dom.constructor.name : type;
5544
- throw new TypeError(`Expecting a valid DOM element, but got ${typeName}.`);
5545
- }
5546
- const pretty = stringify(dom, Number.POSITIVE_INFINITY, {
5547
- maxLength,
5548
- highlight: true,
5549
- ...prettyFormatOptions
5550
- });
5551
- return dom.outerHTML.length > maxLength ? `${pretty.slice(0, maxLength)}...` : pretty;
5532
+ if (maxLength === 0) {
5533
+ return "";
5534
+ }
5535
+ if (!dom) {
5536
+ dom = document.body;
5537
+ }
5538
+ if ("element" in dom && "all" in dom) {
5539
+ dom = dom.element();
5540
+ }
5541
+ const type = typeof dom;
5542
+ if (type !== "object" || !dom.outerHTML) {
5543
+ const typeName = type === "object" ? dom.constructor.name : type;
5544
+ throw new TypeError(`Expecting a valid DOM element, but got ${typeName}.`);
5545
+ }
5546
+ const pretty = stringify(dom, Number.POSITIVE_INFINITY, {
5547
+ maxLength,
5548
+ highlight: true,
5549
+ ...prettyFormatOptions
5550
+ });
5551
+ return dom.outerHTML.length > maxLength ? `${pretty.slice(0, maxLength)}...` : pretty;
5552
5552
  }
5553
5553
  function getElementError(selector, container) {
5554
- const error = new Error(`Cannot find element with locator: ${asLocator("javascript", selector)}
5555
-
5556
- ${prettyDOM(container)}`);
5557
- error.name = "VitestBrowserElementError";
5558
- return error;
5554
+ const error = new Error(`Cannot find element with locator: ${asLocator("javascript", selector)}\n\n${prettyDOM(container)}`);
5555
+ error.name = "VitestBrowserElementError";
5556
+ return error;
5559
5557
  }
5560
5558
 
5561
5559
  export { Ivya as I, getByTextSelector as a, getByPlaceholderSelector as b, getByAltTextSelector as c, getByTestIdSelector as d, getByRoleSelector as e, getByLabelSelector as f, getByTitleSelector as g, getElementError as h, getElementLocatorSelectors as i, debug as j, prettyDOM as p };
package/dist/state.js CHANGED
@@ -1 +1,170 @@
1
- (function(){"use strict";const{parse:$parse}=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 sessionId=getBrowserState().sessionId;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,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;getBrowserState().cdp=createCdp();function rpc(){return state.rpc}function createCdp(){const listenersMap=new WeakMap;function getId(listener){const id=listenersMap.get(listener)||crypto.randomUUID();listenersMap.set(listener,id);return id}const listeners={};const cdp={send(method,params){return rpc().sendCdpEvent(sessionId,method,params)},on(event,listener){const listenerId=getId(listener);listeners[event]=listeners[event]||[];listeners[event].push(listener);rpc().trackCdpEvent(sessionId,"on",event,listenerId).catch(error);return cdp},once(event,listener){const listenerId=getId(listener);const handler=data=>{listener(data);cdp.off(event,listener)};listeners[event]=listeners[event]||[];listeners[event].push(handler);rpc().trackCdpEvent(sessionId,"once",event,listenerId).catch(error);return cdp},off(event,listener){const listenerId=getId(listener);if(listeners[event]){listeners[event]=listeners[event].filter(l=>l!==listener)}rpc().trackCdpEvent(sessionId,"off",event,listenerId).catch(error);return cdp},emit(event,payload){if(listeners[event]){listeners[event].forEach(l=>{try{l(payload)}catch(err){error(err)}})}}};return cdp}function error(err){window.dispatchEvent(new ErrorEvent("error",{error:err}))}})();
1
+ (function () {
2
+ 'use strict';
3
+
4
+ /// <reference types="../types/index.d.ts" />
5
+
6
+ // (c) 2020-present Andrea Giammarchi
7
+
8
+ const {parse: $parse} = JSON;
9
+ const {keys} = Object;
10
+
11
+ const Primitive = String; // it could be Number
12
+ const primitive = 'string'; // it could be 'number'
13
+
14
+ const ignore = {};
15
+ const object = 'object';
16
+
17
+ const noop = (_, value) => value;
18
+
19
+ const primitives = value => (
20
+ value instanceof Primitive ? Primitive(value) : value
21
+ );
22
+
23
+ const Primitives = (_, value) => (
24
+ typeof value === primitive ? new Primitive(value) : value
25
+ );
26
+
27
+ const revive = (input, parsed, output, $) => {
28
+ const lazy = [];
29
+ for (let ke = keys(output), {length} = ke, y = 0; y < length; y++) {
30
+ const k = ke[y];
31
+ const value = output[k];
32
+ if (value instanceof Primitive) {
33
+ const tmp = input[value];
34
+ if (typeof tmp === object && !parsed.has(tmp)) {
35
+ parsed.add(tmp);
36
+ output[k] = ignore;
37
+ lazy.push({k, a: [input, parsed, tmp, $]});
38
+ }
39
+ else
40
+ output[k] = $.call(output, k, tmp);
41
+ }
42
+ else if (output[k] !== ignore)
43
+ output[k] = $.call(output, k, value);
44
+ }
45
+ for (let {length} = lazy, i = 0; i < length; i++) {
46
+ const {k, a} = lazy[i];
47
+ output[k] = $.call(output, k, revive.apply(null, a));
48
+ }
49
+ return output;
50
+ };
51
+
52
+ /**
53
+ * Converts a specialized flatted string into a JS value.
54
+ * @param {string} text
55
+ * @param {(this: any, key: string, value: any) => any} [reviver]
56
+ * @returns {any}
57
+ */
58
+ const parse = (text, reviver) => {
59
+ const input = $parse(text, Primitives).map(primitives);
60
+ const value = input[0];
61
+ const $ = noop;
62
+ const tmp = typeof value === object && value ?
63
+ revive(input, new Set, value, $) :
64
+ value;
65
+ return $.call({'': tmp}, '', tmp);
66
+ };
67
+
68
+ /* @__NO_SIDE_EFFECTS__ */
69
+ function getBrowserState() {
70
+ return window.__vitest_browser_runner__;
71
+ }
72
+
73
+ const config = getBrowserState().config;
74
+ const sessionId = getBrowserState().sessionId;
75
+ const providedContext = parse(getBrowserState().providedContext);
76
+ const state = {
77
+ ctx: {
78
+ pool: "browser",
79
+ worker: "./browser.js",
80
+ workerId: 1,
81
+ config,
82
+ projectName: config.name || "",
83
+ files: [],
84
+ environment: {
85
+ name: "browser",
86
+ options: null
87
+ },
88
+ providedContext,
89
+ invalidates: []
90
+ },
91
+ onCancel: null,
92
+ config,
93
+ environment: {
94
+ name: "browser",
95
+ transformMode: "web",
96
+ setup() {
97
+ throw new Error("Not called in the browser");
98
+ }
99
+ },
100
+ moduleCache: getBrowserState().moduleCache,
101
+ rpc: null,
102
+ durations: {
103
+ environment: 0,
104
+ prepare: performance.now()
105
+ },
106
+ providedContext
107
+ };
108
+ globalThis.__vitest_browser__ = true;
109
+ globalThis.__vitest_worker__ = state;
110
+ getBrowserState().cdp = createCdp();
111
+ function rpc() {
112
+ return state.rpc;
113
+ }
114
+ function createCdp() {
115
+ const listenersMap = new WeakMap();
116
+ function getId(listener) {
117
+ const id = listenersMap.get(listener) || crypto.randomUUID();
118
+ listenersMap.set(listener, id);
119
+ return id;
120
+ }
121
+ const listeners = {};
122
+ const cdp = {
123
+ send(method, params) {
124
+ return rpc().sendCdpEvent(sessionId, method, params);
125
+ },
126
+ on(event, listener) {
127
+ const listenerId = getId(listener);
128
+ listeners[event] = listeners[event] || [];
129
+ listeners[event].push(listener);
130
+ rpc().trackCdpEvent(sessionId, "on", event, listenerId).catch(error);
131
+ return cdp;
132
+ },
133
+ once(event, listener) {
134
+ const listenerId = getId(listener);
135
+ const handler = (data) => {
136
+ listener(data);
137
+ cdp.off(event, listener);
138
+ };
139
+ listeners[event] = listeners[event] || [];
140
+ listeners[event].push(handler);
141
+ rpc().trackCdpEvent(sessionId, "once", event, listenerId).catch(error);
142
+ return cdp;
143
+ },
144
+ off(event, listener) {
145
+ const listenerId = getId(listener);
146
+ if (listeners[event]) {
147
+ listeners[event] = listeners[event].filter((l) => l !== listener);
148
+ }
149
+ rpc().trackCdpEvent(sessionId, "off", event, listenerId).catch(error);
150
+ return cdp;
151
+ },
152
+ emit(event, payload) {
153
+ if (listeners[event]) {
154
+ listeners[event].forEach((l) => {
155
+ try {
156
+ l(payload);
157
+ } catch (err) {
158
+ error(err);
159
+ }
160
+ });
161
+ }
162
+ }
163
+ };
164
+ return cdp;
165
+ }
166
+ function error(err) {
167
+ window.dispatchEvent(new ErrorEvent("error", { error: err }));
168
+ }
169
+
170
+ })();
package/dist/utils.js CHANGED
@@ -1,3 +1,3 @@
1
1
  import '@vitest/browser/context';
2
- export { j as debug, h as getElementError, i as getElementLocatorSelectors, p as prettyDOM } from './public-utils-J4vwTaki.js';
2
+ export { j as debug, h as getElementError, i as getElementLocatorSelectors, p as prettyDOM } from './public-utils-4WiYB3_6.js';
3
3
  import 'vitest/utils';