Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor to prevent the tear down check issue #14

Merged
merged 1 commit into from
Feb 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
102 changes: 32 additions & 70 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,32 @@ const init = ({
shouldFailOnLog = false,
errorMessage = defaultErrorMessage,
} = {}) => {
const patchConsoleMethod = (methodName, unexpectedConsoleCallStacks) => {
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 patchConsoleMethod = (methodName) => {
const unexpectedConsoleCallStacks = []

const newMethod = (format, ...args) => {
const message = util.format(format, ...args)
if (silenceMessage && silenceMessage(message, methodName)) {
Expand All @@ -31,90 +56,27 @@ const init = ({
}

let originalMethod = console[methodName]

beforeEach(() => {
console[methodName] = newMethod // eslint-disable-line no-console
unexpectedConsoleCallStacks.length = 0
})

afterEach(() => {
flushUnexpectedConsoleCalls(methodName, unexpectedConsoleCallStacks)
console[methodName] = originalMethod
})

return console[methodName]
}

const isSpy = (spy) => spy && spy._isMockFunction

const flushUnexpectedConsoleCalls = (mockMethod, methodName, unexpectedConsoleCallStacks) => {
// eslint-disable-next-line no-console
if (console[methodName] !== mockMethod && !isSpy(console[methodName])) {
throw new Error(`Test did not tear down console.${methodName} mock properly.

If you are trying to disable the "fail on console" mechanism, you should use beforeEach/afterEach
instead of beforeAll/afterAll
`)
}

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 unexpectedErrorCallStacks = []
const unexpectedWarnCallStacks = []
const unexpectedLogCallStacks = []

let errorMethod, warnMethod, logMethod

if (shouldFailOnError) {
errorMethod = patchConsoleMethod('error', unexpectedErrorCallStacks)
patchConsoleMethod('error')
}
if (shouldFailOnWarn) {
warnMethod = patchConsoleMethod('warn', unexpectedWarnCallStacks)
patchConsoleMethod('warn')
}
if (shouldFailOnLog) {
logMethod = patchConsoleMethod('log', unexpectedLogCallStacks)
patchConsoleMethod('log')
}

const flushAllUnexpectedConsoleCalls = () => {
if (shouldFailOnError) {
flushUnexpectedConsoleCalls(errorMethod, 'error', unexpectedErrorCallStacks)
}
if (shouldFailOnWarn) {
flushUnexpectedConsoleCalls(warnMethod, 'warn', unexpectedWarnCallStacks)
}
if (shouldFailOnLog) {
flushUnexpectedConsoleCalls(logMethod, 'log', unexpectedLogCallStacks)
}
unexpectedErrorCallStacks.length = 0
unexpectedWarnCallStacks.length = 0
unexpectedLogCallStacks.length = 0
}

const resetAllUnexpectedConsoleCalls = () => {
unexpectedErrorCallStacks.length = 0
unexpectedWarnCallStacks.length = 0
unexpectedLogCallStacks.length = 0
}

beforeEach(resetAllUnexpectedConsoleCalls)
afterEach(flushAllUnexpectedConsoleCalls)
}

module.exports = init