-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
133 lines (108 loc) · 3.79 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
const util = require('util')
const chalk = require('chalk')
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,
} = {}) => {
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 captureMessage = (format, ...args) => {
const message = util.format(format, ...args)
if (silenceMessage && silenceMessage(message, methodName, { group: groups[groups.length - 1], groups })) {
return
}
// 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
let originalMethod = console[methodName]
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;
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