html-browser-tester 0.0.19 → 0.0.21

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.
@@ -18,11 +18,17 @@ export declare class BrowserTester {
18
18
  private iframe;
19
19
  private tests;
20
20
  private expects;
21
+ private setups;
22
+ private beforeAllCallbacks;
23
+ private afterAllCallbacks;
21
24
  private beforeEachCallbacks;
22
25
  private afterEachCallbacks;
23
26
  constructor({ html, url, width, height, }: Option);
24
27
  spyOn<T extends string>(obj: Record<T, Function>, key: T): Spy<T>;
25
28
  setBrowserSize(width: number, height: number): void;
29
+ setup(callback: (window: Window, doc: Document) => Promise<void>): void;
30
+ beforeAll(callback: (window: Window, doc: Document) => Promise<void>): void;
31
+ afterAll(callback: (window: Window, doc: Document) => Promise<void>): void;
26
32
  beforeEach(callback: (window: Window, doc: Document) => Promise<void>): void;
27
33
  afterEach(callback: (window: Window, doc: Document) => Promise<void>): void;
28
34
  test(description: string, callback: (window: Window, doc: Document, iframe: HTMLIFrameElement) => Promise<void>): void;
@@ -11,6 +11,9 @@ class BrowserTester {
11
11
  iframe;
12
12
  tests = [];
13
13
  expects = new expect_1.Expect();
14
+ setups = [];
15
+ beforeAllCallbacks = [];
16
+ afterAllCallbacks = [];
14
17
  beforeEachCallbacks = [];
15
18
  afterEachCallbacks = [];
16
19
  constructor({ html = '', url = '', width, height, }) {
@@ -34,6 +37,15 @@ class BrowserTester {
34
37
  this.iframe.height = `${height}px`;
35
38
  }
36
39
  }
40
+ setup(callback) {
41
+ this.setups.push(callback);
42
+ }
43
+ beforeAll(callback) {
44
+ this.beforeAllCallbacks.push(callback);
45
+ }
46
+ afterAll(callback) {
47
+ this.afterAllCallbacks.push(callback);
48
+ }
37
49
  beforeEach(callback) {
38
50
  this.beforeEachCallbacks.push(callback);
39
51
  }
@@ -60,8 +72,8 @@ class BrowserTester {
60
72
  this.tests = [];
61
73
  }
62
74
  evaluate(code, args = {}) {
63
- const func = new Function('test', 'it', 'expect', 'beforeEach', 'afterEach', 'setBrowserSize', 'spyOn', ...Object.keys(args), 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, ...Object.values(args));
75
+ const func = new Function('test', 'it', 'expect', 'beforeEach', 'afterEach', 'beforeAll', 'afterAll', 'setup', 'setBrowserSize', 'spyOn', ...Object.keys(args), code);
76
+ 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.setup.bind(this), this.setBrowserSize.bind(this), this.spyOn, ...Object.values(args));
65
77
  }
66
78
  run() {
67
79
  return new Promise((resolve) => {
@@ -83,6 +95,12 @@ class BrowserTester {
83
95
  const iframeCallback = async () => {
84
96
  iframe.removeEventListener('load', iframeCallback);
85
97
  const results = [];
98
+ for (const s of this.setups) {
99
+ await s(iframe.contentWindow, iframe.contentDocument);
100
+ }
101
+ for (const b of this.beforeAllCallbacks) {
102
+ await b(iframe.contentWindow, iframe.contentDocument);
103
+ }
86
104
  for (const t of this.tests) {
87
105
  for (const b of this.beforeEachCallbacks) {
88
106
  await b(iframe.contentWindow, iframe.contentDocument);
@@ -107,6 +125,9 @@ class BrowserTester {
107
125
  await a(iframe.contentWindow, iframe.contentDocument);
108
126
  }
109
127
  }
128
+ for (const a of this.afterAllCallbacks) {
129
+ await a(iframe.contentWindow, iframe.contentDocument);
130
+ }
110
131
  iframe.removeEventListener('load', iframeCallback);
111
132
  URL.revokeObjectURL(url);
112
133
  iframe.remove();
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "html-browser-tester",
3
3
  "main": "./lib/browser-test.js",
4
4
  "types": "./lib/browser-test.d.ts",
5
- "version": "0.0.19",
5
+ "version": "0.0.21",
6
6
  "scripts": {
7
7
  "dev": "vite",
8
8
  "preview": "vite preview",