Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
- add options to disable failure on warn/error
- rename `ignoreError` to `silenceMessage`
  • Loading branch information
Valentin Hervieu committed Jan 11, 2021
1 parent 57a40a9 commit 36f2690
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ In a file used in the `setupFilesAfterEnv` option of Jest, add this code:
import failOnConsole from 'jest-fail-on-console'

failOnConsole()

// or with options:
failOnConsole({
shouldFailOnWarn: false,
})
```

## But I have some expected console errors/warning
Expand All @@ -47,17 +52,32 @@ test('should log an error', () => {

You can pass an object with options to the function:

### ignoreError
### shouldFailOnWarn

Use this to make a test fail when a warning is logged.

- Type: `boolean`
- Default: `true`

### shouldFailOnError

Use this to make a test fail when an error is logged.

- Type: `boolean`
- Default: `true`

### silenceMessage

Signature: `(errorMessage: string) => boolean`
- Signature: `(message: string, methodName: 'warn' | 'error') => boolean`

This will be call for every error. If you return true, the test will not fail.
This function is called for every console warn/error.
If true is returned, the message will not show in the console and the test won't fail.

Example:

```ts
failOnConsole({
ignoreError: (errorMessage) => {
silenceMessage: (errorMessage) => {
if (/Not implemented: navigation/.test(errorMessage)) {
return true
}
Expand Down
11 changes: 10 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
export type InitOptions = {
ignoreError?: (errorMessage: string) => boolean
/**
* This function is called for every console warn/error.
* If true is returned, the message will not show in the console
* and the test won't fail.
*/
silenceMessage?: (message: string, methodName: 'warn' | 'error') => boolean
/** defaults to true */
shouldFailOnWarn?: boolean
/** defaults to true */
shouldFailOnError?: boolean
}
declare function init(options?: InitOptions): void

Expand Down
21 changes: 15 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const util = require('util')
const chalk = require('chalk')

const init = ({ ignoreError }) => {
const init = ({ silenceMessage, shouldFailOnWarn = true, shouldFailOnError = true }) => {
const patchConsoleMethod = (methodName, unexpectedConsoleCallStacks) => {
const newMethod = (format, ...args) => {
if (ignoreError && ignoreError(format)) {
if (silenceMessage && silenceMessage(format, methodName)) {
return
}

Expand Down Expand Up @@ -66,12 +66,21 @@ const init = ({ ignoreError }) => {
const unexpectedErrorCallStacks = []
const unexpectedWarnCallStacks = []

const errorMethod = patchConsoleMethod('error', unexpectedErrorCallStacks)
const warnMethod = patchConsoleMethod('warn', unexpectedWarnCallStacks)
let errorMethod, warnMethod
if (shouldFailOnError) {
errorMethod = patchConsoleMethod('error', unexpectedErrorCallStacks)
}
if (shouldFailOnWarn) {
warnMethod = patchConsoleMethod('warn', unexpectedWarnCallStacks)
}

const flushAllUnexpectedConsoleCalls = () => {
flushUnexpectedConsoleCalls(errorMethod, 'error', unexpectedErrorCallStacks)
flushUnexpectedConsoleCalls(warnMethod, 'warn', unexpectedWarnCallStacks)
if (shouldFailOnError) {
flushUnexpectedConsoleCalls(errorMethod, 'error', unexpectedErrorCallStacks)
}
if (shouldFailOnWarn) {
flushUnexpectedConsoleCalls(warnMethod, 'warn', unexpectedWarnCallStacks)
}
unexpectedErrorCallStacks.length = 0
unexpectedWarnCallStacks.length = 0
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-fail-on-console",
"version": "1.0.1",
"version": "2.0.0",
"description": "Utility to make jest tests fail when console.error() or console.warn() are used",
"main": "index.js",
"repository": "git@github.com:ricardo-ch/jest-fail-on-console.git",
Expand Down

0 comments on commit 36f2690

Please sign in to comment.