html-browser-tester 0.0.12 → 0.0.13
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 +1 -1
- package/lib/browser-test.d.ts +35 -29
- package/lib/browser-test.js +9 -4
- package/lib/expect/assert.d.ts +2 -0
- package/lib/expect/assert.js +21 -0
- package/lib/expect/index.d.ts +1 -1
- package/lib/expect/index.js +6 -4
- package/lib/main.d.ts +5 -0
- package/lib/main.js +12 -4
- package/lib/spy.d.ts +8 -0
- package/lib/spy.js +18 -0
- package/package.json +1 -1
package/README.md
CHANGED
package/lib/browser-test.d.ts
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
import { Spy } from "./spy";
|
1
2
|
declare type Option = {
|
2
3
|
html: string;
|
3
4
|
width?: number;
|
@@ -17,42 +18,47 @@ export declare class BrowserTester {
|
|
17
18
|
private beforeEachCallbacks;
|
18
19
|
private afterEachCallbacks;
|
19
20
|
constructor({ html, width, height, }: Option);
|
21
|
+
spyOn<T extends string>(obj: Record<T, Function>, key: T): Spy<T>;
|
20
22
|
setBrowserSize(width: number, height: number): void;
|
21
23
|
beforeEach(callback: (window: Window, doc: Document) => Promise<void>): void;
|
22
24
|
afterEach(callback: (window: Window, doc: Document) => Promise<void>): void;
|
23
25
|
test(description: string, callback: (window: Window, doc: Document) => Promise<void>): void;
|
24
26
|
it(description: string, callback: (window: Window, doc: Document) => Promise<void>): void;
|
25
|
-
expect(
|
26
|
-
toBe: (value: unknown) => void;
|
27
|
-
toBeTruthy: (value: unknown) => void;
|
28
|
-
toBeFalsy: (value: unknown) => void;
|
29
|
-
toBeNull: (value: unknown) => void;
|
30
|
-
toBeUndefined: (value: unknown) => void;
|
31
|
-
toBeDefined: (value: unknown) => void;
|
32
|
-
toBeNaN: (value: unknown) => void;
|
33
|
-
toMatch: (value: unknown) => void;
|
34
|
-
toContain: (value: unknown) => void;
|
35
|
-
toBeLessThan: (value: unknown) => void;
|
36
|
-
toBeGreaterThan: (value: unknown) => void;
|
37
|
-
toBeLessThanOrEqual: (value: unknown) => void;
|
38
|
-
toBeGreaterThanOrEqual: (value: unknown) => void;
|
39
|
-
toBeInstanceOf: (value: unknown) => void;
|
27
|
+
expect(...args: unknown[]): {
|
28
|
+
toBe: (...value: unknown[]) => void;
|
29
|
+
toBeTruthy: (...value: unknown[]) => void;
|
30
|
+
toBeFalsy: (...value: unknown[]) => void;
|
31
|
+
toBeNull: (...value: unknown[]) => void;
|
32
|
+
toBeUndefined: (...value: unknown[]) => void;
|
33
|
+
toBeDefined: (...value: unknown[]) => void;
|
34
|
+
toBeNaN: (...value: unknown[]) => void;
|
35
|
+
toMatch: (...value: unknown[]) => void;
|
36
|
+
toContain: (...value: unknown[]) => void;
|
37
|
+
toBeLessThan: (...value: unknown[]) => void;
|
38
|
+
toBeGreaterThan: (...value: unknown[]) => void;
|
39
|
+
toBeLessThanOrEqual: (...value: unknown[]) => void;
|
40
|
+
toBeGreaterThanOrEqual: (...value: unknown[]) => void;
|
41
|
+
toBeInstanceOf: (...value: unknown[]) => void;
|
42
|
+
toHaveBeenCalled: (...value: unknown[]) => void;
|
43
|
+
toHaveBeenCalledWith: (...value: unknown[]) => void;
|
40
44
|
} & {
|
41
45
|
not: {
|
42
|
-
toBe: (value: unknown) => void;
|
43
|
-
toBeTruthy: (value: unknown) => void;
|
44
|
-
toBeFalsy: (value: unknown) => void;
|
45
|
-
toBeNull: (value: unknown) => void;
|
46
|
-
toBeUndefined: (value: unknown) => void;
|
47
|
-
toBeDefined: (value: unknown) => void;
|
48
|
-
toBeNaN: (value: unknown) => void;
|
49
|
-
toMatch: (value: unknown) => void;
|
50
|
-
toContain: (value: unknown) => void;
|
51
|
-
toBeLessThan: (value: unknown) => void;
|
52
|
-
toBeGreaterThan: (value: unknown) => void;
|
53
|
-
toBeLessThanOrEqual: (value: unknown) => void;
|
54
|
-
toBeGreaterThanOrEqual: (value: unknown) => void;
|
55
|
-
toBeInstanceOf: (value: unknown) => void;
|
46
|
+
toBe: (...value: unknown[]) => void;
|
47
|
+
toBeTruthy: (...value: unknown[]) => void;
|
48
|
+
toBeFalsy: (...value: unknown[]) => void;
|
49
|
+
toBeNull: (...value: unknown[]) => void;
|
50
|
+
toBeUndefined: (...value: unknown[]) => void;
|
51
|
+
toBeDefined: (...value: unknown[]) => void;
|
52
|
+
toBeNaN: (...value: unknown[]) => void;
|
53
|
+
toMatch: (...value: unknown[]) => void;
|
54
|
+
toContain: (...value: unknown[]) => void;
|
55
|
+
toBeLessThan: (...value: unknown[]) => void;
|
56
|
+
toBeGreaterThan: (...value: unknown[]) => void;
|
57
|
+
toBeLessThanOrEqual: (...value: unknown[]) => void;
|
58
|
+
toBeGreaterThanOrEqual: (...value: unknown[]) => void;
|
59
|
+
toBeInstanceOf: (...value: unknown[]) => void;
|
60
|
+
toHaveBeenCalled: (...value: unknown[]) => void;
|
61
|
+
toHaveBeenCalledWith: (...value: unknown[]) => void;
|
56
62
|
};
|
57
63
|
};
|
58
64
|
clearTests(): void;
|
package/lib/browser-test.js
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.BrowserTester = void 0;
|
4
4
|
const expect_1 = require("./expect");
|
5
|
+
const spy_1 = require("./spy");
|
5
6
|
class BrowserTester {
|
6
7
|
html = '';
|
7
8
|
width;
|
@@ -20,6 +21,9 @@ class BrowserTester {
|
|
20
21
|
this.height = height;
|
21
22
|
}
|
22
23
|
}
|
24
|
+
spyOn(obj, key) {
|
25
|
+
return new spy_1.Spy(obj, key);
|
26
|
+
}
|
23
27
|
setBrowserSize(width, height) {
|
24
28
|
if (width) {
|
25
29
|
this.iframe.width = `${width}px`;
|
@@ -46,15 +50,16 @@ class BrowserTester {
|
|
46
50
|
callback,
|
47
51
|
});
|
48
52
|
}
|
49
|
-
expect(
|
50
|
-
|
53
|
+
expect(...args) {
|
54
|
+
// @ts-ignore
|
55
|
+
return this.expects.expect(...args);
|
51
56
|
}
|
52
57
|
clearTests() {
|
53
58
|
this.tests = [];
|
54
59
|
}
|
55
60
|
evaluate(code) {
|
56
|
-
const func = new Function('test', 'it', 'expect', 'beforeEach', 'afterEach', 'setBrowserSize', code);
|
57
|
-
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));
|
61
|
+
const func = new Function('test', 'it', 'expect', 'beforeEach', 'afterEach', 'setBrowserSize', 'spyOn', code);
|
62
|
+
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.bind(this));
|
58
63
|
}
|
59
64
|
run() {
|
60
65
|
return new Promise((resolve) => {
|
package/lib/expect/assert.d.ts
CHANGED
@@ -13,4 +13,6 @@ export declare const assert: (expected: unknown) => {
|
|
13
13
|
toBeLessThanOrEqual: (result: unknown) => boolean;
|
14
14
|
toBeGreaterThanOrEqual: (result: unknown) => boolean;
|
15
15
|
toBeInstanceOf: (result: unknown) => boolean;
|
16
|
+
toHaveBeenCalled: () => boolean;
|
17
|
+
toHaveBeenCalledWith: (...args: any[]) => boolean;
|
16
18
|
};
|
package/lib/expect/assert.js
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.assert = void 0;
|
4
|
+
const spy_1 = require("../spy");
|
4
5
|
const assert = (expected) => ({
|
5
6
|
toBe: (resut) => {
|
6
7
|
return expected === resut;
|
@@ -70,5 +71,25 @@ const assert = (expected) => ({
|
|
70
71
|
}
|
71
72
|
return false;
|
72
73
|
},
|
74
|
+
toHaveBeenCalled: () => {
|
75
|
+
if (!(expected instanceof spy_1.Spy)) {
|
76
|
+
return false;
|
77
|
+
}
|
78
|
+
if (expected.called) {
|
79
|
+
return true;
|
80
|
+
}
|
81
|
+
return false;
|
82
|
+
},
|
83
|
+
toHaveBeenCalledWith: (...args) => {
|
84
|
+
if (!(expected instanceof spy_1.Spy)) {
|
85
|
+
return false;
|
86
|
+
}
|
87
|
+
if (!expected.calledArgs.length) {
|
88
|
+
return false;
|
89
|
+
}
|
90
|
+
return expected.calledArgs.some(arg => {
|
91
|
+
return arg.every((a, i) => a === args[i]);
|
92
|
+
});
|
93
|
+
}
|
73
94
|
});
|
74
95
|
exports.assert = assert;
|
package/lib/expect/index.d.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import { assert } from './assert';
|
2
2
|
declare type AssertKey = keyof ReturnType<typeof assert>;
|
3
3
|
declare type TruthyAssertObject = {
|
4
|
-
[T in AssertKey]: (value: unknown) => void;
|
4
|
+
[T in AssertKey]: (...value: unknown[]) => void;
|
5
5
|
};
|
6
6
|
declare type ReturnObject = TruthyAssertObject & {
|
7
7
|
not: TruthyAssertObject;
|
package/lib/expect/index.js
CHANGED
@@ -10,13 +10,15 @@ class Expect {
|
|
10
10
|
not: {},
|
11
11
|
};
|
12
12
|
Object.keys(assertedObject).forEach((key) => {
|
13
|
-
returnObject[key] = (result) => {
|
14
|
-
|
13
|
+
returnObject[key] = (...result) => {
|
14
|
+
// @ts-ignore
|
15
|
+
this.expects.push(assertedObject[key](...result));
|
15
16
|
};
|
16
17
|
});
|
17
18
|
Object.keys(assertedObject).forEach((key) => {
|
18
|
-
returnObject.not[key] = (result) => {
|
19
|
-
|
19
|
+
returnObject.not[key] = (...result) => {
|
20
|
+
// @ts-ignore
|
21
|
+
this.expects.push(!assertedObject[key](...result));
|
20
22
|
};
|
21
23
|
});
|
22
24
|
return returnObject;
|
package/lib/main.d.ts
CHANGED
package/lib/main.js
CHANGED
@@ -19,6 +19,11 @@ const html = `
|
|
19
19
|
<h1>Title1</h1>
|
20
20
|
<h2>Title2</h2>
|
21
21
|
</body>
|
22
|
+
<script>
|
23
|
+
function main() {
|
24
|
+
console.log('hello')
|
25
|
+
}
|
26
|
+
</script>
|
22
27
|
</html>
|
23
28
|
`;
|
24
29
|
const main = async () => {
|
@@ -37,10 +42,13 @@ const main = async () => {
|
|
37
42
|
const h2 = doc.querySelector('h2');
|
38
43
|
browserTest.expect(window.getComputedStyle(h2).color).toBe('rgb(255, 0, 0)');
|
39
44
|
});
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
45
|
+
browserTest.test('console.log to have been called with `hello`', async (window) => {
|
46
|
+
const spy = browserTest.spyOn(window.console, 'log');
|
47
|
+
// @ts-ignore
|
48
|
+
window.main();
|
49
|
+
browserTest.expect(spy).toHaveBeenCalled();
|
50
|
+
browserTest.expect(spy).toHaveBeenCalledWith('hello');
|
51
|
+
});
|
44
52
|
const results = await browserTest.run();
|
45
53
|
console.log(results);
|
46
54
|
};
|
package/lib/spy.d.ts
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
export interface SpyObject extends Record<string, any> {
|
2
|
+
(this: Record<string, unknown>, ...args: Array<any>): unknown;
|
3
|
+
}
|
4
|
+
export declare class Spy<T extends string> {
|
5
|
+
called: boolean;
|
6
|
+
calledArgs: Array<any>[];
|
7
|
+
constructor(obj: Record<T, Function>, methodName: T);
|
8
|
+
}
|
package/lib/spy.js
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.Spy = void 0;
|
4
|
+
class Spy {
|
5
|
+
called = false;
|
6
|
+
calledArgs = [];
|
7
|
+
constructor(obj, methodName) {
|
8
|
+
const savedFn = obj[methodName];
|
9
|
+
const spy = this;
|
10
|
+
obj[methodName] = function () {
|
11
|
+
const args = Array.from(arguments);
|
12
|
+
spy.called = true;
|
13
|
+
spy.calledArgs.push(args);
|
14
|
+
savedFn.apply(obj, args);
|
15
|
+
};
|
16
|
+
}
|
17
|
+
}
|
18
|
+
exports.Spy = Spy;
|