-
Notifications
You must be signed in to change notification settings - Fork 3
/
shoulda_test.js
98 lines (83 loc) · 2.33 KB
/
shoulda_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
96
97
98
import * as shoulda from "./shoulda.js";
const { assert, context, setup, should, teardown } = shoulda;
shoulda.reset();
const order = [];
context("fixture: evaluation order", () => {
setup(() => order.push("outer-setup"));
context("C1 sub context", () => {
setup(() => order.push("inner-setup"));
should("should-1", () => order.push("should-1"));
});
should("should-2", () => order.push("should-2"));
teardown(() => order.push("outer-teardown"));
});
context("evaluation order", () => {
should("execute the collection phase in the correct order", () => {
assert.equal([
"outer-setup",
"inner-setup",
"should-1",
"outer-teardown",
"outer-setup",
"should-2",
"outer-teardown",
], order);
});
});
context("state is passed to tests", () => {
setup((t) => t.state = 123);
should("have access to state", (t) => {
assert.equal(123, t.state);
});
teardown((t) => assert.equal(123, t.state));
});
context("assertions", () => {
should("succeed when the condition is true", () => {
assert.isTrue(true);
assert.isFalse(false);
assert.equal(1, 1);
assert.equal([1], [1]);
});
context("ensureCalled", () => {
should("ensureCalled should not fail if called", () => {
const f = shoulda.ensureCalled();
f();
});
});
context("throwsError", () => {
should("catch error and pass the test", () => {
assert.throwsError(() => {
throw new Error("This should be caught.");
}, "Error");
});
});
});
const StubFixture = { a: 1 };
context("stubs", () => {
should("stub out properties of objects", () => {
assert.equal(1, StubFixture.a);
shoulda.stub(StubFixture, "a", 2);
assert.equal(2, StubFixture.a);
});
});
if (!(await shoulda.run())) {
throw new Error("First group of tests failed.");
}
shoulda.reset();
const testsRan = [];
context("skipping tests", () => {
should.only("run A", () => testsRan.push("A"));
context.only("run this whole context", () => {
should("run B", () => testsRan.push("B"));
});
should("not run C", () => testsRan.push("C"));
should.only("run D", () => testsRan.push("D"));
});
await shoulda.run();
shoulda.reset();
context("skipping tests", () => {
should("have only run tests marked as only", () => {
assert.equal(["A", "B", "D"], testsRan);
});
});
await shoulda.run();