@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/context.d.ts +127 -0
- package/dist/client/.vite/manifest.json +24 -0
- package/dist/client/__vitest__/assets/index-BMAciMM5.js +51 -0
- package/dist/client/__vitest__/assets/index-BcFb8Rbc.css +1 -0
- package/dist/client/__vitest__/index.html +2 -2
- package/dist/client/__vitest_browser__/orchestrator-BCpOi5ot.js +301 -0
- package/dist/client/__vitest_browser__/{rpc-slP7oy1q.js → rpc-CImfI7bO.js} +288 -392
- package/dist/client/__vitest_browser__/{tester-RmfypyZ0.js → tester-e70VCOgC.js} +351 -44
- package/dist/client/esm-client-injector.js +14 -28
- package/dist/client/{index.html → orchestrator.html} +8 -7
- package/dist/client/tester.html +4 -3
- package/dist/context.js +87 -0
- package/dist/index.d.ts +6 -3
- package/dist/index.js +406 -268
- package/dist/providers.js +12 -112
- package/dist/webdriver-CXn0ag9T.js +170 -0
- package/package.json +23 -13
- package/providers/playwright.d.ts +14 -2
- package/providers/webdriverio.d.ts +4 -0
- package/providers.d.ts +5 -5
- package/dist/client/__vitest__/assets/index-TpLatVz2.js +0 -35
- package/dist/client/__vitest__/assets/index-fUmMsp0O.css +0 -1
- package/dist/client/__vitest_browser__/main-v2hiOVax.js +0 -102
package/dist/context.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
function convertElementToXPath(element) {
|
|
2
|
+
if (!element || !(element instanceof Element))
|
|
3
|
+
throw new Error(`Expected DOM element to be an instance of Element, received ${typeof element}`);
|
|
4
|
+
return getPathTo(element);
|
|
5
|
+
}
|
|
6
|
+
function getPathTo(element) {
|
|
7
|
+
if (element.id !== "")
|
|
8
|
+
return `id("${element.id}")`;
|
|
9
|
+
if (!element.parentNode || element === document.documentElement)
|
|
10
|
+
return element.tagName;
|
|
11
|
+
let ix = 0;
|
|
12
|
+
const siblings = element.parentNode.childNodes;
|
|
13
|
+
for (let i = 0; i < siblings.length; i++) {
|
|
14
|
+
const sibling = siblings[i];
|
|
15
|
+
if (sibling === element)
|
|
16
|
+
return `${getPathTo(element.parentNode)}/${element.tagName}[${ix + 1}]`;
|
|
17
|
+
if (sibling.nodeType === 1 && sibling.tagName === element.tagName)
|
|
18
|
+
ix++;
|
|
19
|
+
}
|
|
20
|
+
return "invalid xpath";
|
|
21
|
+
}
|
|
22
|
+
const state = () => __vitest_worker__;
|
|
23
|
+
const runner = () => __vitest_browser_runner__;
|
|
24
|
+
const filepath = () => state().filepath || state().current?.file?.filepath || void 0;
|
|
25
|
+
const rpc = () => state().rpc;
|
|
26
|
+
const contextId = runner().contextId;
|
|
27
|
+
const channel = new BroadcastChannel(`vitest:${contextId}`);
|
|
28
|
+
function triggerCommand(command, ...args) {
|
|
29
|
+
return rpc().triggerCommand(contextId, command, filepath(), args);
|
|
30
|
+
}
|
|
31
|
+
const userEvent = {
|
|
32
|
+
click(element, options = {}) {
|
|
33
|
+
const xpath = convertElementToXPath(element);
|
|
34
|
+
return triggerCommand("__vitest_click", xpath, options);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const screenshotIds = {};
|
|
38
|
+
const page = {
|
|
39
|
+
get config() {
|
|
40
|
+
return runner().config;
|
|
41
|
+
},
|
|
42
|
+
viewport(width, height) {
|
|
43
|
+
const id = runner().iframeId;
|
|
44
|
+
channel.postMessage({ type: "viewport", width, height, id });
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
channel.addEventListener("message", function handler(e) {
|
|
47
|
+
if (e.data.type === "viewport:done" && e.data.id === id) {
|
|
48
|
+
channel.removeEventListener("message", handler);
|
|
49
|
+
resolve();
|
|
50
|
+
}
|
|
51
|
+
if (e.data.type === "viewport:fail" && e.data.id === id) {
|
|
52
|
+
channel.removeEventListener("message", handler);
|
|
53
|
+
reject(new Error(e.data.error));
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
async screenshot(options = {}) {
|
|
59
|
+
const currentTest = state().current;
|
|
60
|
+
if (!currentTest)
|
|
61
|
+
throw new Error("Cannot take a screenshot outside of a test.");
|
|
62
|
+
if (currentTest.concurrent) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
"Cannot take a screenshot in a concurrent test because concurrent tests run at the same time in the same iframe and affect each other's environment. Use a non-concurrent test to take a screenshot."
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
const repeatCount = currentTest.result?.repeatCount ?? 0;
|
|
68
|
+
const taskName = getTaskFullName(currentTest);
|
|
69
|
+
const number = screenshotIds[repeatCount]?.[taskName] ?? 1;
|
|
70
|
+
screenshotIds[repeatCount] ??= {};
|
|
71
|
+
screenshotIds[repeatCount][taskName] = number + 1;
|
|
72
|
+
const name = options.path || `${taskName.replace(/[^a-z0-9]/g, "-")}-${number}.png`;
|
|
73
|
+
return triggerCommand(
|
|
74
|
+
"__vitest_screenshot",
|
|
75
|
+
name,
|
|
76
|
+
{
|
|
77
|
+
...options,
|
|
78
|
+
element: options.element ? convertElementToXPath(options.element) : void 0
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
function getTaskFullName(task) {
|
|
84
|
+
return task.suite ? `${getTaskFullName(task.suite)} ${task.name}` : task.name;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export { page, userEvent };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { BrowserCommand, WorkspaceProject } from 'vitest/node';
|
|
2
|
+
export { BrowserCommand } from 'vitest/node';
|
|
3
|
+
import { Plugin } from 'vitest/config';
|
|
4
|
+
|
|
5
|
+
declare function defineBrowserCommand<T extends unknown[]>(fn: BrowserCommand<T>): BrowserCommand<T>;
|
|
3
6
|
|
|
4
7
|
declare const _default: (project: WorkspaceProject, base?: string) => Plugin[];
|
|
5
8
|
|
|
6
|
-
export { _default as default };
|
|
9
|
+
export { _default as default, defineBrowserCommand };
|