-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
test.js
201 lines (161 loc) · 4.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* eslint-disable no-eval */
/* global describe, it, chai, ES6Promise */
var assert
var Runnable
var isNode = typeof require === 'function'
if (isNode) {
assert = require('chai').assert
Runnable = require('mocha').Runnable
} else {
assert = chai.assert
Runnable = window.Mocha.Runnable
ES6Promise.polyfill()
}
/**
* Thunkify a function for `process.nextTick`.
*
* @return {Function}
*/
function defer () {
return new Promise(function (resolve) {
setTimeout(resolve, 0)
})
}
describe('co-mocha', function () {
describe('synchronous', function () {
it('should pass', function (done) {
var test = new Runnable('synchronous', function () {})
test.run(done)
})
it('should fail', function (done) {
var test = new Runnable('synchronous', function () {
throw new Error('You had one job')
})
test.run(function (err) {
assert(err instanceof Error)
assert.equal(err.message, 'You had one job')
return done()
})
})
})
describe('promise', function () {
it('should pass', function (done) {
var test = new Runnable('promise', function () {
return defer()
})
test.run(done)
})
it('should fail', function (done) {
var test = new Runnable('promise', function () {
return new Promise(function (resolve, reject) {
return setTimeout(function () {
return reject(new Error('You promised me'))
}, 0)
})
})
test.run(function (err) {
assert(err instanceof Error)
assert.equal(err.message, 'You promised me')
return done()
})
})
})
describe('callback', function () {
it('should pass', function (done) {
var test = new Runnable('callback', function (done) {
return setTimeout(done, 0)
})
test.run(done)
})
it('should fail', function (done) {
var test = new Runnable('callback', function (done) {
return setTimeout(function () {
return done(new Error('You never called me back'))
}, 0)
})
test.run(function (err) {
assert(err instanceof Error)
assert.equal(err.message, 'You never called me back')
return done()
})
})
})
describe('generators', function () {
var TEST_SOURCE = [
'(function * () {',
' yield defer()',
'})'
].join('\n')
var TEST_ERROR_SOURCE = [
'(function * () {',
' yield defer()',
' throw new Error(\'This generation has failed\')',
'})'
].join('\n')
describe('es6', function () {
try {
eval('(function * () {})')
} catch (e) {
console.log('Generators are not supported natively, skipping...')
return
}
it('visual debugging', function * () {
yield Promise.resolve('This is purely for testing Mocha HTML output')
})
it('should pass', function (done) {
var test = new Runnable('es6', eval(TEST_SOURCE))
test.run(done)
})
it('should fail', function (done) {
var test = new Runnable('es6', eval(TEST_ERROR_SOURCE))
test.run(function (err) {
assert(err instanceof Error)
assert.equal(err.message, 'This generation has failed')
return done()
})
})
})
if (isNode) {
var traceur = require('traceur')
var regenerator = require('regenerator')
describe('regenerator', function () {
it('should pass', function (done) {
var test = new Runnable('regenerator', eval(regenerator.compile(TEST_SOURCE, {
includeRuntime: true
}).code))
test.run(done)
})
it('should fail', function (done) {
var test = new Runnable('regenerator', eval(
regenerator.compile(TEST_ERROR_SOURCE, {
includeRuntime: true
}).code
))
test.run(function (err) {
assert(err instanceof Error)
assert.equal(err.message, 'This generation has failed')
return done()
})
})
})
describe('traceur', function () {
it('should pass', function (done) {
var test = new Runnable(
'regenerator', eval(traceur.compile(TEST_SOURCE))
)
test.run(done)
})
it('should fail', function (done) {
var test = new Runnable(
'regenerator', eval(traceur.compile(TEST_ERROR_SOURCE))
)
test.run(function (err) {
assert(err instanceof Error)
assert.equal(err.message, 'This generation has failed')
return done()
})
})
})
}
})
})