Skip to content

Commit

Permalink
Add support for custom error messages (closes ValentinH#9, closes Val…
Browse files Browse the repository at this point in the history
  • Loading branch information
JCB-K committed Jan 24, 2022
1 parent 2137fcf commit 268cef7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ declare namespace init {
shouldFailOnError?: boolean
/** defaults to false */
shouldFailOnLog?: boolean
/**
* This function lets you define a custom error message. The methodName is the method
* that caused the error, bold is a function that lets you bold subsets of your message.
* example: (methodName, bold) => `console.${methodName} is not ${bold('allowed')}`
*/
errorMessage?: (methodName: string, bold: (string) => string) => string
}
}

Expand Down
35 changes: 20 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
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.`

console.log(defaultErrorMessage('error', chalk.bold))

const init = ({
silenceMessage,
shouldFailOnWarn = true,
shouldFailOnError = true,
shouldFailOnLog = false,
errorMessage = defaultErrorMessage,
} = {}) => {
const patchConsoleMethod = (methodName, unexpectedConsoleCallStacks) => {
const newMethod = (format, ...args) => {
Expand All @@ -23,16 +32,16 @@ const init = ({
}
}

let spy
beforeEach(() => {
spy = jest.spyOn(console, methodName).mockImplementation(newMethod)
})
let spy
beforeEach(() => {
spy = jest.spyOn(console, methodName).mockImplementation(newMethod)
})

afterEach(() => {
spy.mockRestore()
})
afterEach(() => {
spy.mockRestore()
})

return console[methodName]
return console[methodName]
}

const isSpy = (spy) => spy && spy._isMockFunction
Expand All @@ -41,7 +50,7 @@ const init = ({
// 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
`)
Expand All @@ -63,11 +72,7 @@ const init = ({
)
})

const message =
`Expected test not to call ${chalk.bold(`console.${methodName}()`)}.\n\n` +
`If the ${methodName} is expected, test for it explicitly by mocking it out using ${chalk.bold(
'jest.spyOn'
)}(console, '${methodName}') and test that the warning occurs.`
const message = errorMessage(methodName, chalk.bold)

throw new Error(`${message}\n\n${messages.join('\n\n')}`)
}
Expand Down Expand Up @@ -114,4 +119,4 @@ const init = ({
afterEach(flushAllUnexpectedConsoleCalls)
}

module.exports = init
// module.exports = init

0 comments on commit 268cef7

Please sign in to comment.