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.
vitest-fail-on-console
Advanced tools
Utility to make vitest tests fail when console.error() or console.warn() are used
Utility to make vitest tests fail when console.error()
, console.warn()
, etc. are used
Vitest 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.
jest-fail-on-console
?There is several changes between jest-fail-on-console
and vitest-fail-on-console
:
This repository has been developed and publish to keep and maintain a Vitest version
of the original idea behind jest-fail-on-console
credited at the bottom of this file.
yarn add -D vitest-fail-on-console
or
npm install -D vitest-fail-on-console
In a file used in the setupFiles
option of Vitest's config file (vitest.config.ts
), add this code:
// tests/setup.ts
import failOnConsole from 'vitest-fail-on-console'
failOnConsole()
// or with options:
failOnConsole({
shouldFailOnWarn: false,
})
// vitest.config.ts
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
environment: "node",
setupFiles: ["tests/setup.ts"],
},
});
If a console.error()
is expected, then you should assert for it:
test('should log an error', () => {
vi.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 silenceMessage = (
message: string,
methodName: 'assert' | 'debug' | 'error' | 'info' | 'log' | 'warn'
) => 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 make sure test fails even when the console warning is called after the test finished executing. This is useful for debugging flaky tests. It's recommended to turn it off for CI (or set it to a low value) as it will slow down test execution
number
0
failOnConsole({
afterEachDelay: !process.env.CI ? 1000 : 0,
})
Most of the logic is taken from :
0.7.1 (2024-09-02)
FAQs
Utility to make vitest tests fail when console.error() or console.warn() are used
The npm package vitest-fail-on-console receives a total of 45,048 weekly downloads. As such, vitest-fail-on-console popularity was classified as popular.
We found that vitest-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.