-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
165 lines (136 loc) · 4.51 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
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
const util = require('util')
const chalk = {
red: (str) => `\u001B[31m${str}\u001B[39m`,
gray: (str) => `\u001B[90m${str}\u001B[39m`,
white: (str) => `\u001B[37m${str}\u001B[39m`,
bold: (str) => `\u001B[1m${str}\u001B[22m`,
}
const defaultErrorMessage = (methodName, bold) =>
`Expected test not to call ${bold(`console.${methodName}()`)}.\n\n` +
`If the ${methodName} is expected, test for it explicitly by mocking it out using ${bold(
'jest.spyOn'
)}(console, '${methodName}').mockImplementation() and test that the warning occurs.`
const init = ({
errorMessage = defaultErrorMessage,
shouldFailOnAssert = false,
shouldFailOnDebug = false,
shouldFailOnError = true,
shouldFailOnInfo = false,
shouldFailOnLog = false,
shouldFailOnWarn = true,
skipTest,
silenceMessage,
allowMessage,
shouldPrintMessage = false,
} = {}) => {
const flushUnexpectedConsoleCalls = (methodName, unexpectedConsoleCallStacks) => {
if (unexpectedConsoleCallStacks.length > 0) {
const messages = unexpectedConsoleCallStacks.map(([stack, message]) => {
const stackLines = stack.split('\n')
return (
`${chalk.red(message)}\n` +
`${stackLines
.map((line, index) => {
if (index === stackLines.length - 1) {
return chalk.white(line)
}
return chalk.gray(line)
})
.join('\n')}`
)
})
const message = errorMessage(methodName, chalk.bold)
throw new Error(`${message}\n\n${messages.join('\n\n')}`)
}
}
const groups = []
const patchConsoleMethod = (methodName) => {
const unexpectedConsoleCallStacks = []
const originalMethod = console[methodName]
const captureMessage = (format, ...args) => {
const message = util.format(format, ...args)
const context = { group: groups[groups.length - 1], groups }
if (
typeof silenceMessage === 'function' &&
silenceMessage(message, methodName, context)
) {
return
}
if (
typeof allowMessage === 'function' &&
allowMessage(message, methodName, context)
) {
originalMethod(format, ...args)
return
}
if (shouldPrintMessage) {
originalMethod(format, ...args)
}
// Capture the call stack now so we can warn about it later.
// The call stack has helpful information for the test author.
// Don't throw yet though b'c it might be accidentally caught and suppressed.
const { stack } = new Error()
if (stack) {
unexpectedConsoleCallStacks.push([
stack.substr(stack.indexOf('\n') + 1),
[...groups, message].join('\n'),
])
}
}
const newAssertMethod = (assertion, format, ...args) => {
if (assertion) {
return
}
captureMessage(format, ...args)
}
const newGroupMethod = (label) => {
groups.push(label || '')
}
const newGroupEndMethod = () => {
groups.pop()
}
const methods = {
assert: newAssertMethod,
group: newGroupMethod,
groupCollapsed: newGroupMethod,
groupEnd: newGroupEndMethod,
}
const newMethod = methods[methodName] || captureMessage
const canSkipTest = () => {
const currentTestState = expect.getState()
const testName = currentTestState.currentTestName
const testPath = currentTestState.testPath
if (skipTest && skipTest({ testName, testPath })) return true
return false
}
let shouldSkipTest
beforeAll(() => {
flushUnexpectedConsoleCalls(methodName, unexpectedConsoleCallStacks)
})
console[methodName] = newMethod
beforeEach(() => {
shouldSkipTest = canSkipTest()
if (shouldSkipTest) return
console[methodName] = newMethod // eslint-disable-line no-console
unexpectedConsoleCallStacks.length = 0
})
afterEach(() => {
if (shouldSkipTest) return
flushUnexpectedConsoleCalls(methodName, unexpectedConsoleCallStacks)
console[methodName] = originalMethod
})
}
beforeEach(() => {
groups.length = 0
})
if (shouldFailOnAssert) patchConsoleMethod('assert')
if (shouldFailOnDebug) patchConsoleMethod('debug')
if (shouldFailOnError) patchConsoleMethod('error')
if (shouldFailOnInfo) patchConsoleMethod('info')
if (shouldFailOnLog) patchConsoleMethod('log')
if (shouldFailOnWarn) patchConsoleMethod('warn')
patchConsoleMethod('group')
patchConsoleMethod('groupCollapsed')
patchConsoleMethod('groupEnd')
}
module.exports = init