html-browser-tester 0.0.18 → 0.0.20
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/lib/browser-test.d.ts +5 -1
- package/lib/browser-test.js +17 -3
- package/lib/main.js +8 -0
- package/package.json +1 -1
package/lib/browser-test.d.ts
CHANGED
@@ -18,11 +18,15 @@ export declare class BrowserTester {
|
|
18
18
|
private iframe;
|
19
19
|
private tests;
|
20
20
|
private expects;
|
21
|
+
private beforeAllCallbacks;
|
22
|
+
private afterAllCallbacks;
|
21
23
|
private beforeEachCallbacks;
|
22
24
|
private afterEachCallbacks;
|
23
25
|
constructor({ html, url, width, height, }: Option);
|
24
26
|
spyOn<T extends string>(obj: Record<T, Function>, key: T): Spy<T>;
|
25
27
|
setBrowserSize(width: number, height: number): void;
|
28
|
+
beforeAll(callback: (window: Window, doc: Document) => Promise<void>): void;
|
29
|
+
afterAll(callback: (window: Window, doc: Document) => Promise<void>): void;
|
26
30
|
beforeEach(callback: (window: Window, doc: Document) => Promise<void>): void;
|
27
31
|
afterEach(callback: (window: Window, doc: Document) => Promise<void>): void;
|
28
32
|
test(description: string, callback: (window: Window, doc: Document, iframe: HTMLIFrameElement) => Promise<void>): void;
|
@@ -67,7 +71,7 @@ export declare class BrowserTester {
|
|
67
71
|
};
|
68
72
|
};
|
69
73
|
clearTests(): void;
|
70
|
-
evaluate(code: string): void;
|
74
|
+
evaluate(code: string, args?: Record<string, unknown>): void;
|
71
75
|
run(): Promise<Result[]>;
|
72
76
|
}
|
73
77
|
export {};
|
package/lib/browser-test.js
CHANGED
@@ -11,6 +11,8 @@ class BrowserTester {
|
|
11
11
|
iframe;
|
12
12
|
tests = [];
|
13
13
|
expects = new expect_1.Expect();
|
14
|
+
beforeAllCallbacks = [];
|
15
|
+
afterAllCallbacks = [];
|
14
16
|
beforeEachCallbacks = [];
|
15
17
|
afterEachCallbacks = [];
|
16
18
|
constructor({ html = '', url = '', width, height, }) {
|
@@ -34,6 +36,12 @@ class BrowserTester {
|
|
34
36
|
this.iframe.height = `${height}px`;
|
35
37
|
}
|
36
38
|
}
|
39
|
+
beforeAll(callback) {
|
40
|
+
this.beforeAllCallbacks.push(callback);
|
41
|
+
}
|
42
|
+
afterAll(callback) {
|
43
|
+
this.afterAllCallbacks.push(callback);
|
44
|
+
}
|
37
45
|
beforeEach(callback) {
|
38
46
|
this.beforeEachCallbacks.push(callback);
|
39
47
|
}
|
@@ -59,9 +67,9 @@ class BrowserTester {
|
|
59
67
|
clearTests() {
|
60
68
|
this.tests = [];
|
61
69
|
}
|
62
|
-
evaluate(code) {
|
63
|
-
const func = new Function('test', 'it', 'expect', 'beforeEach', 'afterEach', 'setBrowserSize', 'spyOn', code);
|
64
|
-
func(this.test.bind(this), this.it.bind(this), this.expect.bind(this), this.beforeEach.bind(this), this.afterEach.bind(this), this.setBrowserSize.bind(this), this.spyOn);
|
70
|
+
evaluate(code, args = {}) {
|
71
|
+
const func = new Function('test', 'it', 'expect', 'beforeEach', 'afterEach', 'beforeAll', 'afterAll', 'setBrowserSize', 'spyOn', ...Object.keys(args), code);
|
72
|
+
func(this.test.bind(this), this.it.bind(this), this.expect.bind(this), this.beforeEach.bind(this), this.afterEach.bind(this), this.beforeAll.bind(this), this.afterAll.bind(this), this.setBrowserSize.bind(this), this.spyOn, ...Object.values(args));
|
65
73
|
}
|
66
74
|
run() {
|
67
75
|
return new Promise((resolve) => {
|
@@ -83,6 +91,9 @@ class BrowserTester {
|
|
83
91
|
const iframeCallback = async () => {
|
84
92
|
iframe.removeEventListener('load', iframeCallback);
|
85
93
|
const results = [];
|
94
|
+
for (const b of this.beforeAllCallbacks) {
|
95
|
+
await b(iframe.contentWindow, iframe.contentDocument);
|
96
|
+
}
|
86
97
|
for (const t of this.tests) {
|
87
98
|
for (const b of this.beforeEachCallbacks) {
|
88
99
|
await b(iframe.contentWindow, iframe.contentDocument);
|
@@ -107,6 +118,9 @@ class BrowserTester {
|
|
107
118
|
await a(iframe.contentWindow, iframe.contentDocument);
|
108
119
|
}
|
109
120
|
}
|
121
|
+
for (const a of this.afterAllCallbacks) {
|
122
|
+
await a(iframe.contentWindow, iframe.contentDocument);
|
123
|
+
}
|
110
124
|
iframe.removeEventListener('load', iframeCallback);
|
111
125
|
URL.revokeObjectURL(url);
|
112
126
|
iframe.remove();
|
package/lib/main.js
CHANGED
@@ -49,6 +49,14 @@ const main = async () => {
|
|
49
49
|
browserTest.expect(spy).toHaveBeenCalled();
|
50
50
|
browserTest.expect(spy).toHaveBeenCalledWith('hello');
|
51
51
|
});
|
52
|
+
await browserTest.evaluate(`
|
53
|
+
test('h1,h2 textContent should have right textContent2', async (_, doc) => {
|
54
|
+
const h1 = doc.querySelector('h1')
|
55
|
+
const h2 = doc.querySelector('h2')
|
56
|
+
expect(h1?.textContent).toBe('Title1')
|
57
|
+
expect(h2?.textContent).toBe('Title2')
|
58
|
+
})
|
59
|
+
`);
|
52
60
|
const results = await browserTest.run();
|
53
61
|
console.log(results);
|
54
62
|
};
|