Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
jest-fail-on-console
Advanced tools
Utility to make jest tests fail when console.error() or console.warn() are used
The jest-fail-on-console npm package is designed to make your Jest tests fail if any console methods (like console.log, console.error, etc.) are called during the test run. This helps ensure that your tests are clean and free of unexpected console output, which can be indicative of underlying issues or forgotten debug statements.
Fail on console.log
This feature makes the test fail if console.log is called during the test execution. It helps in identifying and removing unnecessary or forgotten console.log statements.
require('jest-fail-on-console');
// In your test file
it('should fail if console.log is called', () => {
console.log('This will cause the test to fail');
expect(true).toBe(true);
});
Fail on console.error
This feature makes the test fail if console.error is called during the test execution. It helps in catching errors that are logged to the console, ensuring they are properly handled.
require('jest-fail-on-console');
// In your test file
it('should fail if console.error is called', () => {
console.error('This will cause the test to fail');
expect(true).toBe(true);
});
Fail on console.warn
This feature makes the test fail if console.warn is called during the test execution. It helps in identifying and addressing warnings that are logged to the console.
require('jest-fail-on-console');
// In your test file
it('should fail if console.warn is called', () => {
console.warn('This will cause the test to fail');
expect(true).toBe(true);
});
jest-console is a package that provides utilities for testing console output in Jest. Unlike jest-fail-on-console, which fails tests on any console method call, jest-console allows you to assert specific console outputs, giving you more control over what is considered a test failure.
jest-mock-console is a package that allows you to mock console methods in your Jest tests. This package is useful for testing how your code handles console output without necessarily failing the test. It provides more flexibility compared to jest-fail-on-console, which strictly fails tests on console method calls.
Utility to make jest tests fail when console.error()
, console.warn()
, etc. are used
Jest doesn't fail the tests when there is a console.error
. In large codebase, we can end up with the test output overloaded by a lot of errors, warnings, etc..
To prevent this, we want to fail each test that is logging to the console. We also want to conserve a clear output of the original error.
This is what this utility is doing.
yarn add -D jest-fail-on-console
or
npm install -D jest-fail-on-console
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,
})
If a console.error()
is expected, then you should assert for it:
test('should log an error', () => {
jest.spyOn(console, 'error').mockImplementation()
// do your logic
expect(console.error).toHaveBeenCalledWith('your error message')
})
You can pass an object with options to the function:
Use this if you want to override the default error message of this library.
// signature
type errorMessage = (
methodName: 'assert' | 'debug' | 'error' | 'info' | 'log' | 'warn',
bold: (string: string) => string
) => string
Use this to make a test fail when a console.assert()
is logged.
boolean
false
Use this to make a test fail when a console.debug()
is logged.
boolean
false
Use this to make a test fail when a console.error()
is logged.
boolean
true
Use this to make a test fail when a console.info()
is logged.
boolean
false
Use this to make a test fail when a console.log()
is logged.
boolean
false
Use this to make a test fail when a console.warn()
is logged.
boolean
true
// signature
type allowMessage = (
message: string,
methodName: 'assert' | 'debug' | 'error' | 'info' | 'log' | 'warn',
context: { group: string; groups: string[] }
) => boolean
This function is called for every console method supported by this utility.
If true
is returned, the message will show in the console and the test won't fail.
Example:
failOnConsole({
allowMessage: (errorMessage) => {
if (/An expected error/.test(errorMessage)) {
return true
}
return false
},
})
// signature
type silenceMessage = (
message: string,
methodName: 'assert' | 'debug' | 'error' | 'info' | 'log' | 'warn',
context: { group: string; groups: string[] }
) => boolean
This function is called for every console method supported by this utility.
If true
is returned, the message will not show in the console and the test won't fail.
Example:
failOnConsole({
silenceMessage: (errorMessage) => {
if (/Not implemented: navigation/.test(errorMessage)) {
return true
}
return false
},
})
Use this if you want to ignore checks introduced by this library for specific tests determined by
the return of the callback function. Return false
if you do not want to skip console checks for
the specific test and return true
if you would like to skip it.
const ignoreList = [/.*components\/SomeComponent.test.tsx/]
const ignoreNameList = ['some component some test name']
failOnConsole({
skipTest: ({ testPath, testName }) => {
for (const pathExp of ignoreList) {
const result = pathExp.test(testPath)
if (result) return true
}
if (ignoreNameList.includes(testName)) {
return true
}
return false
},
})
Use this to print the message immediately when called not awaiting the test to finish. This is useful to show the message if there are other or earlier test failures which will result in the fail on console error to be hidden by jest.
boolean
false
This project is maintained by Valentin Hervieu.
This project was originally part of @ricardo-ch organisation because I (Valentin) was working at Ricardo. After leaving this company, they gracefully accepted to transfer the project to me. ❤️
Most of the logic is taken from React's setupTests file.
FAQs
Utility to make jest tests fail when console.error() or console.warn() are used
The npm package jest-fail-on-console receives a total of 305,018 weekly downloads. As such, jest-fail-on-console popularity was classified as popular.
We found that jest-fail-on-console demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.