html-browser-tester 0.0.19 → 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.
@@ -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;
@@ -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
  }
@@ -60,8 +68,8 @@ class BrowserTester {
60
68
  this.tests = [];
61
69
  }
62
70
  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));
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/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.20",
6
6
  "scripts": {
7
7
  "dev": "vite",
8
8
  "preview": "vite preview",