Socket
Socket
Sign inDemoInstall

console-testing-library

Package Overview
Dependencies
3
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.3.1 to 0.4.0

13

dist/pure.js

@@ -20,2 +20,4 @@ "use strict";

var _stripAnsi = _interopRequireDefault(require("strip-ansi"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -33,3 +35,4 @@

function createConsole({
isSilent: defaultIsSilent = true
isSilent: defaultIsSilent = true,
stripAnsi: defaultStripAnsi = false
} = {}) {

@@ -47,2 +50,3 @@ let logs = [];

let isSilent = defaultIsSilent;
let stripAnsi = defaultStripAnsi;
let targetConsole = undefined;

@@ -53,9 +57,10 @@ const writable = new _stream.Writable({

.slice(0, -1);
logs.push([currentLevel, message]);
const normalizedMessage = stripAnsi ? (0, _stripAnsi.default)(message) : message;
logs.push([currentLevel, normalizedMessage]);
if (currentLevel && currentLevel in levels) {
levels[currentLevel] = [levels[currentLevel], message].filter(Boolean).join('\n');
levels[currentLevel] = [levels[currentLevel], normalizedMessage].filter(Boolean).join('\n');
}
records[currentMethod] = [records[currentMethod], message].filter(Boolean).join('\n');
records[currentMethod] = [records[currentMethod], normalizedMessage].filter(Boolean).join('\n');
callback();

@@ -62,0 +67,0 @@ }

@@ -5,2 +5,3 @@ export const originalConsole = Console;

isSilent?: boolean;
stripAnsi?: boolean;
};

@@ -7,0 +8,0 @@

{
"name": "console-testing-library",
"version": "0.3.1",
"version": "0.4.0",
"description": "Testing console the right way",

@@ -29,3 +29,4 @@ "type": "module",

"jest-snapshot": "^24.9.0",
"pretty-format": "^24.9.0"
"pretty-format": "^24.9.0",
"strip-ansi": "^6.0.0"
},

@@ -36,2 +37,3 @@ "devDependencies": {

"@babel/preset-env": "^7.7.4",
"ansi-styles": "^4.3.0",
"canopic": "^0.2.1"

@@ -38,0 +40,0 @@ },

@@ -195,2 +195,27 @@ # `console-testing-library`

## Optionally stripping out Ansi characters
When working with the console, it's not unusual to make the output a bit prettier by
relying on libraries such as [chalk](https://www.npmjs.com/package/chalk). Those libraries rely on
[ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) for styling strings in the terminal.
Although those ANSI codes make the console output nicer, they may make the test data a bit harder to parse and work with for the mere human being (e.g. `Hello [31mWorld[39m!`).
The `createConsole()` function accepts a `stripAnsi` option (defaulting to `false`) to dynamically remove the control characters from what's being logged.
```js
import { createConsole, getLog, mockConsole } from 'console-testing-library';
const chalk = require('chalk'); // or any other similar library
const strippingConsole = createConsole({ stripAnsi: true });
const restore = mockConsole(strippingConsole);
console.log('Hello %s!', chalk.red('World'));
// Yeah! Plain text without any funky ANSI codes.
expect(getLog().log).toEqual('Hello World!');
restore();
```
## Custom matchers

@@ -197,0 +222,0 @@

@@ -6,2 +6,3 @@ import { Console } from 'console';

import prettyFormat from 'pretty-format';
import ansiStripper from 'strip-ansi';

@@ -28,3 +29,6 @@ const INSPECT_SYMBOL = util.inspect.custom;

export function createConsole({ isSilent: defaultIsSilent = true } = {}) {
export function createConsole({
isSilent: defaultIsSilent = true,
stripAnsi: defaultStripAnsi = false,
} = {}) {
let logs = [];

@@ -41,2 +45,3 @@ let records = {};

let isSilent = defaultIsSilent;
let stripAnsi = defaultStripAnsi;
let targetConsole = undefined;

@@ -50,9 +55,10 @@

.slice(0, -1);
logs.push([currentLevel, message]);
const normalizedMessage = stripAnsi ? ansiStripper(message) : message;
logs.push([currentLevel, normalizedMessage]);
if (currentLevel && currentLevel in levels) {
levels[currentLevel] = [levels[currentLevel], message]
levels[currentLevel] = [levels[currentLevel], normalizedMessage]
.filter(Boolean)
.join('\n');
}
records[currentMethod] = [records[currentMethod], message]
records[currentMethod] = [records[currentMethod], normalizedMessage]
.filter(Boolean)

@@ -59,0 +65,0 @@ .join('\n');

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc