html-browser-tester 0.0.6 → 0.0.8

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/README.md CHANGED
@@ -57,7 +57,39 @@ const main = async () => {
57
57
  const results = await browserTester.run()
58
58
 
59
59
  console.log(results)
60
+ /*
61
+ [
62
+ { description: 'h1,h2 textContent should have right textContent', result: true },
63
+ { description: 'title should have right textContent', result: true },
64
+ { description: 'h2 should have red text', result: true }
65
+ ]
66
+ */
60
67
  }
61
68
 
62
69
  main()
63
- ```
70
+ ```
71
+
72
+ ### Evaluate
73
+
74
+ You can also evaluate template literals to run tests
75
+
76
+ ```js
77
+ browserTest.evaluate(`
78
+ test('h1,h2 textContent should have right textContent', async (_, doc) => {
79
+ const h1 = doc.querySelector('h1')
80
+ const h2 = doc.querySelector('h2')
81
+ expect(h1?.textContent).toBe('Title1')
82
+ expect(h2?.textContent).toBe('Title2')
83
+ })
84
+
85
+ test('title should have right textContent', async (_, doc) => {
86
+ const title = doc.querySelector('title')
87
+ expect(title?.textContent).toBe('Hello')
88
+ })
89
+
90
+ test('h2 should have red text', async (window, doc) => {
91
+ const h2 = doc.querySelector('h2')
92
+ expect(window.getComputedStyle(h2).color).toBe('rgb(255, 0, 0)')
93
+ })
94
+ `)
95
+ ```
@@ -21,6 +21,7 @@ export declare class BrowserTester {
21
21
  beforeEach(callback: (window: Window, doc: Document) => Promise<void>): void;
22
22
  afterEach(callback: (window: Window, doc: Document) => Promise<void>): void;
23
23
  test(description: string, callback: (window: Window, doc: Document) => Promise<void>): void;
24
+ it(description: string, callback: (window: Window, doc: Document) => Promise<void>): void;
24
25
  expect(value: unknown): {
25
26
  toBe: (value: unknown) => void;
26
27
  toBeTruthy: (value: unknown) => void;
@@ -53,6 +54,7 @@ export declare class BrowserTester {
53
54
  };
54
55
  };
55
56
  clearTests(): void;
57
+ evaluate(code: string): void;
56
58
  run(): Promise<Result[]>;
57
59
  }
58
60
  export {};
@@ -40,12 +40,22 @@ class BrowserTester {
40
40
  callback,
41
41
  });
42
42
  }
43
+ it(description, callback) {
44
+ this.tests.push({
45
+ description,
46
+ callback,
47
+ });
48
+ }
43
49
  expect(value) {
44
50
  return this.expects.expect(value);
45
51
  }
46
52
  clearTests() {
47
53
  this.tests = [];
48
54
  }
55
+ evaluate(code) {
56
+ const func = new Function('test', 'expect', 'beforeEach', 'afterEach', code);
57
+ func(this.test.bind(this), this.expect.bind(this), this.beforeEach.bind(this), this.afterEach.bind(this));
58
+ }
49
59
  run() {
50
60
  return new Promise((resolve) => {
51
61
  const blob = new Blob([this.html], { type: "text/html" });
@@ -66,12 +76,20 @@ class BrowserTester {
66
76
  for (const b of this.beforeEachCallbacks) {
67
77
  await b(iframe.contentWindow, iframe.contentDocument);
68
78
  }
69
- await t.callback(iframe.contentWindow, iframe.contentDocument);
70
79
  const { description } = t;
71
- results.push({
72
- description,
73
- result: this.expects.isAllPassed()
74
- });
80
+ try {
81
+ await t.callback(iframe.contentWindow, iframe.contentDocument);
82
+ results.push({
83
+ description,
84
+ result: this.expects.isAllPassed()
85
+ });
86
+ }
87
+ catch (e) {
88
+ results.push({
89
+ description,
90
+ result: false,
91
+ });
92
+ }
75
93
  this.expects.clean();
76
94
  for (const a of this.afterEachCallbacks) {
77
95
  await a(iframe.contentWindow, iframe.contentDocument);
package/lib/main.js CHANGED
@@ -37,6 +37,10 @@ const main = async () => {
37
37
  const h2 = doc.querySelector('h2');
38
38
  browserTest.expect(window.getComputedStyle(h2).color).toBe('rgb(255, 0, 0)');
39
39
  });
40
+ // browserTest.test('can insert text to h5', async (window, doc) => {
41
+ // const h5 = doc.querySelector('h5') as HTMLHeadingElement
42
+ // h5.textContent = 'aaaa'
43
+ // })
40
44
  const results = await browserTest.run();
41
45
  console.log(results);
42
46
  };
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.6",
5
+ "version": "0.0.8",
6
6
  "scripts": {
7
7
  "dev": "vite",
8
8
  "preview": "vite preview",