-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test] Enable failing unexpected console warn|error in browser tests (#…
- Loading branch information
Showing
3 changed files
with
111 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const formatUtil = require('format-util'); | ||
|
||
function createUnexpectedConsoleMessagesHooks(Mocha, methodName, expectedMatcher) { | ||
const mochaHooks = { | ||
beforeAll: [], | ||
afterAll: [], | ||
beforeEach: [], | ||
afterEach: [], | ||
}; | ||
const unexpectedCalls = []; | ||
const stackTraceFilter = Mocha.utils.stackTraceFilter(); | ||
|
||
function logUnexpectedConsoleCalls(format, ...args) { | ||
const message = formatUtil(format, ...args); | ||
// Safe stack so that test dev can track where the unexpected console message was created. | ||
const { stack } = new Error(); | ||
|
||
unexpectedCalls.push([ | ||
// first line includes the (empty) error message | ||
// i.e. Remove the `Error:` line | ||
// second line is this frame | ||
stackTraceFilter(stack.split('\n').slice(2).join('\n')), | ||
message, | ||
]); | ||
} | ||
|
||
mochaHooks.beforeAll.push(function registerConsoleStub() { | ||
// eslint-disable-next-line no-console | ||
console[methodName] = logUnexpectedConsoleCalls; | ||
}); | ||
|
||
mochaHooks.afterEach.push(function flushUnexpectedCalls() { | ||
const hadUnexpectedCalls = unexpectedCalls.length > 0; | ||
const formattedCalls = unexpectedCalls.map(([stack, message]) => `${message}\n${stack}`); | ||
unexpectedCalls.length = 0; | ||
|
||
// eslint-disable-next-line no-console | ||
if (console[methodName] !== logUnexpectedConsoleCalls) { | ||
throw new Error(`Did not tear down spy or stub of console.${methodName} in your test.`); | ||
} | ||
if (hadUnexpectedCalls) { | ||
// In karma `file` is `null`. | ||
// We still have the stacktrace though | ||
const location = this.currentTest.file ?? '(unknown file)'; | ||
const testPath = `"${this.currentTest.parent | ||
.titlePath() | ||
.concat(this.currentTest.title) | ||
.join('" -> "')}"`; | ||
const message = | ||
`Expected test not to call console.${methodName}()\n\n` + | ||
'If the warning is expected, test for it explicitly by ' + | ||
`using the ${expectedMatcher}() matcher.`; | ||
|
||
const error = new Error( | ||
`${location}: ${message}\n\n${formattedCalls.join('\n\n')}\n\n` + | ||
`in ${testPath} (${location})`, | ||
); | ||
// The stack of `flushUnexpectedCalls` is irrelevant. | ||
// It includes no clue where the test was triggered | ||
error.stack = ''; | ||
throw error; | ||
} | ||
}); | ||
|
||
return mochaHooks; | ||
} | ||
|
||
function createMochaHooks(Mocha) { | ||
const warnHooks = createUnexpectedConsoleMessagesHooks(Mocha, 'warn', 'toWarnDev'); | ||
const errorHooks = createUnexpectedConsoleMessagesHooks(Mocha, 'error', 'toErrorDev'); | ||
|
||
return { | ||
beforeAll: [...warnHooks.beforeAll, ...errorHooks.beforeAll], | ||
afterAll: [...warnHooks.afterAll, ...errorHooks.afterAll], | ||
beforeEach: [...warnHooks.beforeEach, ...errorHooks.beforeEach], | ||
afterEach: [...warnHooks.afterEach, ...errorHooks.afterEach], | ||
}; | ||
} | ||
|
||
module.exports = { createMochaHooks }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters