This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.js
153 lines (115 loc) · 4.32 KB
/
index.spec.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* eslint-env jest */
const cases = require("./index");
describe("when creating test cases", () => {
let describeSpy;
let testSpy;
beforeEach(() => {
describeSpy = jest
.spyOn(global, "describe")
.mockImplementation((title, fn) => {
fn();
});
testSpy = jest.spyOn(global, "test").mockImplementation((name, fn) => {
// Callbacks passed to test should be provided a done function when they
// they have an arity.
if (fn.length) {
fn(() => {});
} else {
fn();
}
});
});
it("creates a describe entry", () => {
cases("my case", [], () => {});
expect(describeSpy).toHaveBeenCalledTimes(1);
expect(describeSpy.mock.calls[0][0]).toEqual("my case");
expect(typeof describeSpy.mock.calls[0][1]).toEqual("function");
});
it("creates a describe.only entry", () => {
describeSpy.only = jest.fn((title, fn) => fn());
cases.only("my case", [], () => {});
expect(describeSpy.only).toHaveBeenCalledTimes(1);
expect(describeSpy.only.mock.calls[0][0]).toEqual("my case");
expect(typeof describeSpy.only.mock.calls[0][1]).toEqual("function");
});
it("creates a describe.skip entry", () => {
describeSpy.skip = jest.fn((title, fn) => fn());
cases.skip("my case", [], () => {});
expect(describeSpy.skip).toHaveBeenCalledTimes(1);
expect(describeSpy.skip.mock.calls[0][0]).toEqual("my case");
expect(typeof describeSpy.skip.mock.calls[0][1]).toEqual("function");
});
it("create a test entry", () => {
cases("my case", [0], () => {});
expect(testSpy).toHaveBeenCalledTimes(1);
expect(testSpy.mock.calls[0][0]).toEqual("case 0");
expect(typeof testSpy.mock.calls[0][1]).toEqual("function");
});
it("creates named test entry", () => {
cases("my case", { "my test": [0] }, () => {});
expect(testSpy).toHaveBeenCalledTimes(1);
expect(testSpy.mock.calls[0][0]).toEqual("my test");
expect(typeof testSpy.mock.calls[0][1]).toEqual("function");
});
it("create a test entry without a describe entry", () => {
cases.inline([0], () => {});
expect(describeSpy).toHaveBeenCalledTimes(0);
expect(testSpy).toHaveBeenCalledTimes(1);
expect(testSpy.mock.calls[0][0]).toEqual("case 0");
expect(typeof testSpy.mock.calls[0][1]).toEqual("function");
});
it("create a test.skip entry without a describe entry", () => {
testSpy.skip = jest.fn((title, fn) => fn());
cases.inline.skip([0], () => {});
expect(describeSpy).toHaveBeenCalledTimes(0);
expect(testSpy.skip).toHaveBeenCalledTimes(1);
expect(testSpy.skip.mock.calls[0][0]).toEqual("case 0");
expect(typeof testSpy.skip.mock.calls[0][1]).toEqual("function");
});
it("create a test.only entry without a describe entry", () => {
testSpy.only = jest.fn((title, fn) => fn());
cases.inline.only([0], () => {});
expect(describeSpy).toHaveBeenCalledTimes(0);
expect(testSpy.only).toHaveBeenCalledTimes(1);
expect(testSpy.only.mock.calls[0][0]).toEqual("case 0");
expect(typeof testSpy.only.mock.calls[0][1]).toEqual("function");
});
it("calls test entries with parameters", () => {
const fn = jest.fn();
const params = [0, 1];
cases("my case", params, fn);
expect(fn).toHaveBeenCalledTimes(2);
expect(fn.mock.calls[0].length).toEqual(1);
expect(fn.mock.calls[1].length).toEqual(1);
expect(fn.mock.calls[0][0]).toEqual(params[0]);
expect(fn.mock.calls[1][0]).toEqual(params[1]);
});
it("calls async callback based test entries with params and done callback", () => {
const fn = jest.fn((params, done) => {});
const params = [0];
cases("my case", params, fn);
expect(fn.mock.calls[0].length).toEqual(2);
expect(fn.mock.calls[0][0]).toEqual(params[0]);
expect(typeof fn.mock.calls[0][1]).toEqual("function");
});
});
describe("when executing test cases", () => {
cases("synchronous", [0], input => {
expect(input).toEqual(input);
});
cases("async callback", [0], (input, done) => {
setTimeout(() => {
expect(input).toEqual(input);
done();
}, 50);
});
cases("async/await", [0], async input => {
function identity(_) {
return new Promise(resolve => {
setTimeout(() => resolve(_), 50);
});
}
const expected = await identity(input);
expect(expected).toEqual(input);
});
});