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-console-group-reporter
Advanced tools
A Jest reporter that groups console messages, allows filtering, and provides flexible display configuration options.
A Jest reporter that groups console messages, allows filtering, and provides flexible display configuration options.
Install jest-console-group-reporter
using your favorite package manager:
# npm
npm install jest-console-group-reporter -D
# yarn
yarn add jest-console-group-reporter -D
# pnpm
pnpm add jest-console-group-reporter -D
Minimum requirements
To use jest-console-group-reporter with the default configuration, simply add it to your Jest configuration:
// Add the reporter to your jest config
module.exports = {
// ...
reporters: ["jest-console-group-reporter"],
};
If you would like to use GitHub Actions grouping, see the example here
You can filter specific console messages by providing a string, regular expression, or predicate function in the filters
option:
// Add as many as you like!
const filters = ["error", /^react/];
module.exports = {
// ...
reporters: [["jest-console-group-reporter", { filters }]],
};
You can create custom groups by specifying them in the groups
option:
// Add as many as you like!
const groups = [
{
name: "React console messages",
match: /react/,
},
];
module.exports = {
// ...
reporters: [["jest-console-group-reporter", { groups }]],
};
You can create dynamic group names by specifying them in the groups
option:
const groups = [
{
name: ({ type }) => `React console.${type}`,
match: /react/,
},
];
module.exports = {
// ...
reporters: [["jest-console-group-reporter", { groups }]],
};
Default configuration:
const defaultOptions: Options = {
consoleLevels: ["error", "warn", "info", "debug", "log"],
filters: [],
groups: [],
onlyFailingTestSuites: false,
afterEachTest: {
enable: true,
reportType: "summary",
filePaths: false,
},
afterAllTests: {
reportType: "detailed",
enable: true,
filePaths: true,
},
useGitHubActions: false,
};
Here are the available configuration options:
An array of regular expressions, strings, or functions to filter out console messages.
interface ConsoleMessage {
type: string;
message: string;
origin: string | undefined;
}
type Filters = Array<string | RegExp | ((consoleMessage: ConsoleMessage) => boolean)>;
When using a predicate function, the return type must be a boolean
.
Note: The origin is not available for all types of console message. So you will need to test it before using it.
interface ConsoleMessage {
origin: string;
type: string | undefined;
message: string;
}
const filters = [
({ message }: ConsoleMessage) => message === "react",
({ origin }: ConsoleMessage) => origin && origin.match(/node_modules/),
({ type }: ConsoleMessage) => type === "error",
];
module.exports = {
// ...
reporters: [["jest-console-group-reporter", { filters }]],
};
An array of custom groups, where each group has a name
and match
property. Messages matching the match
criteria will be grouped under the specified name
.
Note: The origin is not available for all types of console message. So you will need to test it before using it.
interface ConsoleMessage {
type: string;
message: string;
origin: string;
}
type Matcher = string | RegExp | (({ type, message, origin }: ConsoleMessage) => boolean);
type Groups = Array<{
match: Matcher;
name: string | (({ type, message, origin }: ConsoleMessage) => string);
}>;
interface ConsoleMessage {
origin: string;
type: string | undefined;
message: string;
}
const groups = [
{
name: "React warnings",
match: ({ message, type }: ConsoleMessage) => message.match(/react/) && type === "warn",
},
{
name: "Error from some module",
match: ({ origin }: ConsoleMessage) => origin && origin.match(/some_module/),
},
];
module.exports = {
// ...
reporters: [["jest-console-group-reporter", { groups }]],
};
An array of console message types to capture (e.g., 'log', 'warn', 'error').
type ConsoleLevels = string[];
const consoleLevels = ["error", "warn"];
module.exports = {
// ...
reporters: [["jest-console-group-reporter", { consoleLevels }]],
};
Configuration for displaying console messages after each test.
interface DisplayOptions {
enable: boolean;
filePaths: boolean;
reportType: "summary" | "detailed";
}
enable
(boolean): Enable or disable displaying messages after each test.filePaths
(boolean): Include file paths in the report.reportType
("summary" | "detailed"): Choose between "summary" and "detailed" report typesconst afterEachTest = {
enable: false;
}
module.exports = {
// ...
reporters: [["jest-console-group-reporter", { afterEachTest }]],
};
Configuration for displaying console messages after all tests have run.
interface DisplayOptions {
enable: boolean;
filePaths: boolean;
reportType: "summary" | "detailed";
}
enable
(boolean): Enable or disable displaying messages after all tests.filePaths
(boolean): Include file paths in the report.reportType
("summary" | "detailed"): Choose between "summary" and "detailed" report types.const afterAllTest = {
filePaths: false;
}
module.exports = {
// ...
reporters: [["jest-console-group-reporter", { afterAllTest }]],
};
Enable GitHub Actions specific behavior. This will wrap each console message in a dropdown. You will only want to enable this property when running in github actions.
type UseGithubActions = boolean;
/**
* @see https://www.npmjs.com/package/is-ci
*/
const isCI = require("is-ci");
module.exports = {
// ...
reporters: [["jest-console-group-reporter", { useGithubActions: isCI }]],
};
const useGithubActions = process.env.IS_CI;
module.exports = {
// ...
reporters: [["jest-console-group-reporter", { useGithubActions }]],
};
This reporter shows console messages for all tests by default. Use the onlyFailingTestSuites
option to see messages only for failing test suites. Due to Jest's limitations, messages can't be filtered for individual failing tests, only for the entire failing suites.
module.exports = {
// ...
reporters: [["jest-console-group-reporter", { onlyFailingTestSuites: true }]],
};
The jest-console-group-reporter internally uses the summary reporter provided by Jest to display the overall test summary. You do not need to explicitly pass the summary reporter to the reporter options, as it is automatically integrated by default.
When running Jest for a single test file (e.g. jest src/someTest.ts), the reporter is not activated. This seems to be a bug with Jest and not this reporter :)
FAQs
A Jest reporter that groups console messages, allows filtering, and provides flexible display configuration options.
We found that jest-console-group-reporter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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.