@allwright.dev/vitest 0.0.24 → 0.0.26
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/index.d.ts +36 -4
- package/dist/index.js +123 -3
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type Browser, type LaunchOptions, type
|
|
2
|
-
import { expect } from "vitest";
|
|
1
|
+
import { Locator, Page, type Browser, type LaunchOptions, type WaitForSelectorOptions } from "@allwright.dev/core";
|
|
2
|
+
import { expect as vitestExpect } from "vitest";
|
|
3
3
|
export interface AllwrightVitestOptions {
|
|
4
4
|
launchOptions?: LaunchOptions;
|
|
5
5
|
serverAddr?: string;
|
|
@@ -8,10 +8,42 @@ export interface AllwrightVitestFixtures {
|
|
|
8
8
|
browser: Browser;
|
|
9
9
|
page: Page;
|
|
10
10
|
}
|
|
11
|
+
export interface RetryExpectationOptions {
|
|
12
|
+
timeoutMs?: number;
|
|
13
|
+
intervalMs?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface TextExpectationOptions extends RetryExpectationOptions {
|
|
16
|
+
command?: {
|
|
17
|
+
timeoutMs?: number;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export interface VisibleExpectationOptions extends RetryExpectationOptions {
|
|
21
|
+
command?: WaitForSelectorOptions;
|
|
22
|
+
}
|
|
23
|
+
export interface PageExpectMatchers {
|
|
24
|
+
toHaveText(selector: string, expected: string | RegExp, options?: TextExpectationOptions): Promise<void>;
|
|
25
|
+
toContainText(selector: string, expected: string | RegExp, options?: TextExpectationOptions): Promise<void>;
|
|
26
|
+
toHaveCount(selector: string, expected: number, options?: RetryExpectationOptions): Promise<void>;
|
|
27
|
+
toBeVisible(selector: string, options?: VisibleExpectationOptions): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
export interface LocatorExpectMatchers {
|
|
30
|
+
toHaveText(expected: string | RegExp, options?: TextExpectationOptions): Promise<void>;
|
|
31
|
+
toContainText(expected: string | RegExp, options?: TextExpectationOptions): Promise<void>;
|
|
32
|
+
toHaveCount(expected: number, options?: RetryExpectationOptions): Promise<void>;
|
|
33
|
+
toBeVisible(options?: VisibleExpectationOptions): Promise<void>;
|
|
34
|
+
}
|
|
11
35
|
export declare const test: import("vitest").TestAPI<{
|
|
12
36
|
browser: Browser;
|
|
13
37
|
page: Page;
|
|
14
38
|
allwright: AllwrightVitestOptions;
|
|
15
39
|
}>;
|
|
16
|
-
|
|
17
|
-
|
|
40
|
+
type VitestExpect = typeof vitestExpect;
|
|
41
|
+
type VitestMatcherReturn = ReturnType<VitestExpect>;
|
|
42
|
+
interface AllwrightExpect extends VitestExpect {
|
|
43
|
+
(actual: Page): PageExpectMatchers;
|
|
44
|
+
(actual: Locator): LocatorExpectMatchers;
|
|
45
|
+
<T>(actual: T): VitestMatcherReturn;
|
|
46
|
+
}
|
|
47
|
+
export declare const expect: AllwrightExpect;
|
|
48
|
+
export { vitestExpect };
|
|
49
|
+
export type { Browser, LaunchOptions, Locator, Page };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { chromium, setServerAddr, shutdown, } from "@allwright.dev/core";
|
|
2
|
-
import { expect, test as base } from "vitest";
|
|
1
|
+
import { chromium, setServerAddr, shutdown, Locator, Page, } from "@allwright.dev/core";
|
|
2
|
+
import { expect as vitestExpect, test as base } from "vitest";
|
|
3
3
|
export const test = base.extend({
|
|
4
4
|
allwright: async ({}, use) => {
|
|
5
5
|
await use({});
|
|
@@ -21,4 +21,124 @@ export const test = base.extend({
|
|
|
21
21
|
await use(browser.page());
|
|
22
22
|
},
|
|
23
23
|
});
|
|
24
|
-
|
|
24
|
+
const DEFAULT_EXPECT_TIMEOUT_MS = 5_000;
|
|
25
|
+
const DEFAULT_EXPECT_INTERVAL_MS = 100;
|
|
26
|
+
function isPage(value) {
|
|
27
|
+
return value instanceof Page;
|
|
28
|
+
}
|
|
29
|
+
function isLocator(value) {
|
|
30
|
+
return value instanceof Locator;
|
|
31
|
+
}
|
|
32
|
+
async function retryExpectation(callback, options = {}) {
|
|
33
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_EXPECT_TIMEOUT_MS;
|
|
34
|
+
const intervalMs = options.intervalMs ?? DEFAULT_EXPECT_INTERVAL_MS;
|
|
35
|
+
const deadline = Date.now() + timeoutMs;
|
|
36
|
+
let lastError;
|
|
37
|
+
while (Date.now() <= deadline) {
|
|
38
|
+
try {
|
|
39
|
+
await callback();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
lastError = error;
|
|
44
|
+
if (Date.now() + intervalMs > deadline) {
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
throw lastError instanceof Error ? lastError : new Error(String(lastError));
|
|
51
|
+
}
|
|
52
|
+
function createPageExpect(page) {
|
|
53
|
+
return {
|
|
54
|
+
async toHaveText(selector, expected, options = {}) {
|
|
55
|
+
await retryExpectation(async () => {
|
|
56
|
+
const result = await page.textContent(selector, options.command ?? {});
|
|
57
|
+
if (expected instanceof RegExp) {
|
|
58
|
+
vitestExpect(result.text).toMatch(expected);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
vitestExpect(result.text).toBe(expected);
|
|
62
|
+
}, options);
|
|
63
|
+
},
|
|
64
|
+
async toContainText(selector, expected, options = {}) {
|
|
65
|
+
await retryExpectation(async () => {
|
|
66
|
+
const result = await page.textContent(selector, options.command ?? {});
|
|
67
|
+
if (expected instanceof RegExp) {
|
|
68
|
+
vitestExpect(result.text).toMatch(expected);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
vitestExpect(result.text).toContain(expected);
|
|
72
|
+
}, options);
|
|
73
|
+
},
|
|
74
|
+
async toHaveCount(selector, expected, options = {}) {
|
|
75
|
+
await retryExpectation(async () => {
|
|
76
|
+
const result = await page.count(selector, {});
|
|
77
|
+
vitestExpect(result.count).toBe(expected);
|
|
78
|
+
}, options);
|
|
79
|
+
},
|
|
80
|
+
async toBeVisible(selector, options = {}) {
|
|
81
|
+
await retryExpectation(async () => {
|
|
82
|
+
const result = await page.waitForSelector(selector, {
|
|
83
|
+
visible: true,
|
|
84
|
+
...(options.command ?? {}),
|
|
85
|
+
});
|
|
86
|
+
vitestExpect(result.visible).toBe(true);
|
|
87
|
+
}, options);
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function createLocatorExpect(locator) {
|
|
92
|
+
return {
|
|
93
|
+
async toHaveText(expected, options = {}) {
|
|
94
|
+
await retryExpectation(async () => {
|
|
95
|
+
const result = await locator.textContent(options.command ?? {});
|
|
96
|
+
if (expected instanceof RegExp) {
|
|
97
|
+
vitestExpect(result.text).toMatch(expected);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
vitestExpect(result.text).toBe(expected);
|
|
101
|
+
}, options);
|
|
102
|
+
},
|
|
103
|
+
async toContainText(expected, options = {}) {
|
|
104
|
+
await retryExpectation(async () => {
|
|
105
|
+
const result = await locator.textContent(options.command ?? {});
|
|
106
|
+
if (expected instanceof RegExp) {
|
|
107
|
+
vitestExpect(result.text).toMatch(expected);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
vitestExpect(result.text).toContain(expected);
|
|
111
|
+
}, options);
|
|
112
|
+
},
|
|
113
|
+
async toHaveCount(expected, options = {}) {
|
|
114
|
+
await retryExpectation(async () => {
|
|
115
|
+
const result = await locator.count({});
|
|
116
|
+
vitestExpect(result.count).toBe(expected);
|
|
117
|
+
}, options);
|
|
118
|
+
},
|
|
119
|
+
async toBeVisible(options = {}) {
|
|
120
|
+
await retryExpectation(async () => {
|
|
121
|
+
const result = await locator.waitFor({
|
|
122
|
+
visible: true,
|
|
123
|
+
...(options.command ?? {}),
|
|
124
|
+
});
|
|
125
|
+
vitestExpect(result.visible).toBe(true);
|
|
126
|
+
}, options);
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
const expectImpl = ((actual) => {
|
|
131
|
+
if (isLocator(actual)) {
|
|
132
|
+
return createLocatorExpect(actual);
|
|
133
|
+
}
|
|
134
|
+
if (isPage(actual)) {
|
|
135
|
+
return createPageExpect(actual);
|
|
136
|
+
}
|
|
137
|
+
return vitestExpect(actual);
|
|
138
|
+
});
|
|
139
|
+
const vitestExpectDescriptors = Object.getOwnPropertyDescriptors(vitestExpect);
|
|
140
|
+
for (const [key, descriptor] of Object.entries(vitestExpectDescriptors)) {
|
|
141
|
+
Object.defineProperty(expectImpl, key, descriptor);
|
|
142
|
+
}
|
|
143
|
+
export const expect = expectImpl;
|
|
144
|
+
export { vitestExpect };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@allwright.dev/vitest",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.26",
|
|
4
4
|
"description": "Vitest fixtures for the allwright TypeScript client.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"prepublishOnly": "npm run build"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@allwright.dev/core": "0.0.
|
|
42
|
+
"@allwright.dev/core": "0.0.26"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"typescript": "^5.9.2",
|