-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
51 lines (46 loc) · 1.3 KB
/
index.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
/**
* Arrow functions don't have dynamic "this", so we need this
* helpers to provide them with a Mocha test context.
*
* Sync test/hook, or async one that returns a Promise now
* has the following signature:
*
* (ctx) => {...}
*
* Async test/hook that needs a callback:
*
* (ctx, cb) => {...}
*
* Sync test/hook that doesn't need the context is the same
* as before:
*
* () => {...}
*/
const beforeWithCtx = (fn) => before(withTestCtx(fn))
const afterWithCtx = (fn) => after(withTestCtx(fn))
const beforeEachWithCtx = (fn) => beforeEach(withTestCtx(fn))
const afterEachWithCtx = (fn) => afterEach(withTestCtx(fn))
const itWithCtx = (desc, fn) => it(desc, withTestCtx(fn))
itWithCtx.only = (desc, fn) => it.only(desc, withTestCtx(fn))
itWithCtx.skip = (desc, fn) => it.skip(desc, fn)
const withTestCtx = (fn) =>
{
if ('function' !== typeof fn) {
return fn
}
const decoratedFn = (fn.length == 2
? function(cb){ return fn(this, cb) }
: function (){ return fn(this) }
)
// this is needed for "document" and "html"
// reporters to output original test code
decoratedFn.toString = () => fn.toString()
return decoratedFn
}
export {
itWithCtx as it,
beforeWithCtx as before,
afterWithCtx as after,
beforeEachWithCtx as beforeEach,
afterEachWithCtx as afterEach
}