This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
mocha.js
140 lines (115 loc) · 3.46 KB
/
mocha.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
/* eslint-env mocha */
/**
* @typedef {object} Skip
* @property {string} [name]
* @property {string} [reason]
*/
/**
* @param {any} o
* @returns {o is Skip}
*/
const isSkip = (o) => Object.prototype.toString.call(o) === '[object Object]' && (o.name || o.reason)
/**
* Get a "describe" function that is optionally 'skipped' or 'onlyed'
* If skip/only are boolean true, or an object with a reason property, then we
* want to skip/only the whole suite
*
* @param {object} [config]
* @param {boolean | Skip | (string | Skip)[]} [config.skip]
* @param {boolean} [config.only]
*/
export function getDescribe (config) {
if (config) {
if (config.skip === true) {
return describe.skip
}
if (config.only === true) {
return describe.only // eslint-disable-line
}
if (Array.isArray(config.skip)) {
const skipArr = config.skip
/**
* @param {string} name
* @param {*} impl
*/
const _describe = (name, impl) => {
const skip = skipArr.find(skip => {
if (typeof skip === 'string') {
return skip === name
}
return skip.name === name
})
if (skip) {
return describe.skip(`${name} (${typeof skip === 'string' ? '🤷' : skip.reason})`, impl)
}
describe(name, impl)
}
_describe.skip = describe.skip
_describe.only = describe.only // eslint-disable-line
return _describe
} else if (isSkip(config.skip)) {
const skip = config.skip
if (!skip.reason) {
return describe.skip
}
/**
* @param {string} name
* @param {*} impl
*/
const _describe = (name, impl) => {
describe.skip(`${name} (${skip.reason})`, impl)
}
_describe.skip = describe.skip
_describe.only = describe.only // eslint-disable-line
return _describe
}
}
return describe
}
/**
* Get an "it" function that is optionally 'skipped' or 'onlyed'
* If skip/only is an array, then we _might_ want to skip/only the specific
* test if one of the items in the array is the same as the test name or if one
* of the items in the array is an object with a name property that is the same
* as the test name.
*
* @param {object} [config]
* @param {boolean | Skip | (string | Skip)[]} [config.skip]
* @param {boolean} [config.only]
* @returns {Mocha.TestFunction}
*/
export function getIt (config) {
if (!config) return it
/**
* @param {string | Mocha.Func} name
* @param {Mocha.Func | Mocha.AsyncFunc} [impl]
*/
const _it = (name, impl) => {
if (typeof name !== 'string') {
throw new Error('Pass test name as first argument to it')
}
if (Array.isArray(config.skip)) {
const skip = config.skip
.map((s) => isSkip(s) ? s : { name: s, reason: '🤷' })
.find((s) => s.name === name)
if (skip) {
if (skip.reason) name = `${name} (${skip.reason})`
return it.skip(name, impl)
}
}
if (Array.isArray(config.only)) {
const only = config.only
.map((o) => isSkip(o) ? o : { name: o, reason: '🤷' })
.find((o) => o.name === name)
if (only) {
if (only.reason) name = `${name} (${only.reason})`
return it.only(name, impl) // eslint-disable-line no-only-tests/no-only-tests
}
}
return it(name, impl)
}
_it.retries = it.retries
_it.skip = it.skip
_it.only = it.only // eslint-disable-line no-only-tests/no-only-tests
return _it
}