This repository has been archived by the owner on Oct 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
95 lines (90 loc) · 2.66 KB
/
test.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
const glob = require("glob");
const rollup = require("rollup");
const tape = require("tape");
const { JSDOM, VirtualConsole } = require("jsdom");
const sinon = require("sinon");
const virtualConsole = new VirtualConsole();
virtualConsole.sendTo(console, { omitJSDOMErrors: true });
virtualConsole.on("jsdomError", e => {
if (e.type !== "not implemented") {
console.error(e);
}
});
const { document, window, HTMLFormElement, Event, FormData } = new JSDOM(
`<!DOCTYPE html><body></body>`,
{ virtualConsole }
).window;
global.document = document;
global.Event = window.Event;
global.FormData = FormData;
glob("src/**/*.test.js", {}, (err, files) => {
if (err) {
console.log(err);
process.exit(1);
}
files.forEach(file => {
rollup
.rollup({ input: __dirname + "/" + file })
.then(b =>
b.generate({
format: "cjs"
})
)
.then(({ code }) => {
tape(file, t => {
global.test = function(name, code) {
t.test(name, t => {
global.jest = {
fn: () => {
const s = sinon.spy();
s.mockReset = s.reset;
return s;
}
};
global.expect = obj => ({
toEqual: exp => t.deepEqual(obj, exp),
toMatchSnapshot: () => {
const snapFile =
__dirname + "/" + file.replace(/\.js$/, ".snap.json");
let shot;
try {
shot = require(snapFile);
} catch (e) {
shot = {};
}
if (name in shot) {
t.deepEqual(obj, shot[name]);
} else {
shot[name] = obj;
require("fs").writeFileSync(snapFile, JSON.stringify(shot));
}
},
toHaveBeenCalledWith: (...args) =>
t.ok(obj.calledWith(...args)),
toHaveBeenCalled: () => t.ok(obj.called()),
toBe: val => t.equal(obj, val),
not: {
toHaveBeenCalled: () => t.ok(obj.notCalled)
}
});
code();
t.end();
});
};
eval(code);
});
})
.catch(e => {
if (e.code === "PARSE_ERROR") {
console.log("Error: " + e.message);
console.log(e.frame);
console.log(
" at " + e.loc.file + ":" + e.loc.line + ":" + e.loc.column
);
} else {
console.log(e.stack);
}
process.exit(1);
});
});
});