-
Notifications
You must be signed in to change notification settings - Fork 291
/
async-callback_spec.js
174 lines (142 loc) · 4.66 KB
/
async-callback_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
describe('async-callback', function() {
var env;
beforeEach(function() {
env = new jasmine.Env();
});
describe('it', function() {
it("should time out if callback is not called", function() {
env.describe("it", function() {
env.it("doesn't wait", function(done) {
expect(1+2).toEqual(3);
});
});
env.currentRunner().execute();
waitsFor(function() {
return env.currentRunner().results().totalCount > 0;
}, 6000);
runs(function() {
expect(env.currentRunner().results().failedCount).toEqual(1);
expect(firstResult(env.currentRunner()).message).toMatch(/timeout/);;
});
});
it("should accept timeout for individual spec", function() {
env.describe("it", function() {
env.it("doesn't wait", function(done) {
expect(1+2).toEqual(3);
}, 250);
});
env.currentRunner().execute();
waitsFor(function() {
return env.currentRunner().results().totalCount > 0;
}, 500);
runs(function() {
expect(env.currentRunner().results().failedCount).toEqual(1);
expect(firstResult(env.currentRunner()).message).toMatch(/timeout/);;
});
});
it("should fail if callback is passed error", function() {
env.describe("it", function() {
env.it("doesn't wait", function(done) {
process.nextTick(function() {
done("Failed asynchronously");
});
});
});
env.currentRunner().execute();
waitsFor(function() {
return env.currentRunner().results().totalCount > 0;
});
runs(function() {
expect(env.currentRunner().results().failedCount).toEqual(1);
expect(firstResult(env.currentRunner()).message).toEqual("Failed asynchronously");
});
});
it("should finish after callback is called", function() {
env.describe("it", function() {
env.it("waits", function(done) {
process.nextTick(function() {
env.currentSpec.expect(1+2).toEqual(3);
done();
});
});
});
env.currentRunner().execute();
waitsFor(function() {
return env.currentRunner().results().totalCount > 0;
}, 2000);
runs(function() {
expect(env.currentRunner().results().passedCount).toEqual(1);
});
});
it('should run in the context of the current spec', function(){
var actualContext;
var jasmineSpecContext;
env.describe("it", function() {
env.it("register context", function(done) {
actualContext = this;
jasmineSpecContext = env.currentSpec;
env.expect(this).toBe(jasmineSpecContext);
done();
});
});
env.currentRunner().execute();
waitsFor(function() {
return env.currentRunner().results().totalCount > 0;
}, 'tested jasmine env runner to run the test', 100);
runs(function() {
expect(actualContext).not.toBe(global);
expect(actualContext).toBe(jasmineSpecContext);
});
});
});
describe("beforeEach", function() {
it("should wait for callback", function() {
env.describe("beforeEach", function() {
var waited = false;
env.beforeEach(function(done) {
process.nextTick(function() {
waited = true;
done();
});
});
env.it("waited", function() {
env.currentSpec.expect(waited).toBeTruthy();
});
});
env.currentRunner().execute();
waitsFor(function() {
return env.currentRunner().results().totalCount > 0;
});
runs(function() {
expect(env.currentRunner().results().passedCount).toEqual(1);
});
});
});
describe("afterEach", function() {
it("should be passed async callback", function() {
var completed = false;
env.describe("afterEach", function() {
env.afterEach(function(done) {
process.nextTick(function() {
done('Failed in afterEach');
completed = true;
});
});
env.it("should pass", function() {
this.expect(1+2).toEqual(3);
});
});
env.currentRunner().execute();
waitsFor(function() {
return completed === true;
});
runs(function() {
expect(env.currentRunner().results().passedCount).toEqual(1);
expect(env.currentRunner().results().failedCount).toEqual(1);
});
});
});
});
function firstResult(runner) {
return runner.results().getItems()[0].getItems()[0].getItems()[0];
}