This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.js
97 lines (84 loc) · 2.55 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var Tests = (function () {
var env = {specs: {}, prefix: ''}
var assertions = 0
var failures = 0
function log (message) {
console.log('%c' + message, 'color: #444')
}
function fail (message) {
console.log('%c' + message, 'color: red')
}
function assert (truth, message) {
assertions = assertions + 1
if (!truth) {
failures = failures + 1
fail(' ' + message)
}
};
assert.equal = function (expected, observed) {
assert(observed === expected,
'Expected "' + observed + '" to be "' + expected + '".')
}
assert.ok = function (observed) {
assert(observed === true,
'Expected "' + observed + '" to be true."')
}
assert.exist = function (key, obj) {
if (typeof obj !== 'object') fail('expected an object, got a', typeof observed)
assert(obj[key] !== undefined,
'key' + key + 'does not exists')
}
assert.notEqual = function (unexpected, observed) {
assert(observed !== unexpected,
'Expected somethign else than "' + unexpected + '".')
}
assert.content = function (text, id) {
var elem = document.getElementById(id)
assert(elem.innerText === text,
id + ' should contain "' + text + '".')
}
assert.selector = function (selector) {
var elem = document.querySelector(selector)
assert(elem,
'Expected to find element matching "' + selector + '".')
}
async function run () {
var arr = Object.entries(this.specs)
var setup = this.setup
var teardown = this.teardown
var prefix = this.prefix
if (arr.length === 0) return
for (let suite of arr) {
var name = suite[0]
var fun = suite[1]
var env = {specs: {}, prefix: prefix + ' '}
log(prefix + name)
if (setup) setup()
// run the description block.
// It's either a test that will be run right away
// Or its a nested descripion that will seed the specs in env
await fun.bind(env)(describe.bind(env), assert)
// recurse
await run.bind(env)()
if (teardown) teardown()
};
return report()
};
function report () {
if (failures > 0) {
return console.log('%c' + assertions + ' assertions. ' + '%c' + failures + ' failures.', 'color: green', 'color: red')
} else return console.log('%c' + assertions + ' assertions. all ok !', 'color: green')
}
function describe (context, fun) {
this.specs[context] = fun
};
async function runTests () {
assertions = 0
failures = 0
await run.bind(env)()
};
return {
describe: describe.bind(env),
run: runTests
}
}())