Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

test-console

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

test-console - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0

1

Jakefile.js

@@ -43,2 +43,3 @@ // Copyright (c) 2014 Titanium I.T. LLC. All rights reserved. For license, see "README" or "LICENSE" file.

return {
esversion: 8,
bitwise: true,

@@ -45,0 +46,0 @@ curly: false,

10

package.json
{
"name": "test-console",
"version": "1.1.0",
"version": "2.0.0",
"description": "A simple and pragmatic library for testing Node.js console output.",

@@ -28,8 +28,8 @@ "main": "src/index.js",

"devDependencies": {
"chai": "^3.0.0",
"jake": "^8.0.12",
"chai": "^4.3.4",
"jake": "^10.8.2",
"jshint": "^2.8.0",
"mocha": "^2.2.5",
"simplebuild-jshint": "^0.3.0"
"mocha": "^9.0.0",
"simplebuild-jshint": "^1.3.0"
}
}

@@ -11,5 +11,5 @@ # test-console

```javascript
var stdout = require("test-console").stdout;
const stdout = require("test-console").stdout;
var output = stdout.inspectSync(function() {
const output = stdout.inspectSync(() => {
console.log("foo");

@@ -29,4 +29,4 @@ });

```javascript
var stdout = require("test-console").stdout;
var stderr = require("test-console").stderr;
const stdout = require("test-console").stdout;
const stderr = require("test-console").stderr;
```

@@ -39,6 +39,8 @@

* `stdout.inspectSync`: Just like `inspect()`, but automatically restores the console when done.
* `stdout.inspectAsync`: Just like `inspectSync()`, but works with async functions.
* `stdout.ignore`: Prevent writes to `stdout` from appearing on the console.
* `stdout.ignoreSync`: Just like `ignore()`, but automatically restores the console when done.
* `stdout.ignoreAsync`: Just like `ignoreSync()`, but works with async functions.
All functions accept an optional options object as the first argument, where isTTY is the only available option. isTTY, if defined, will override the `stdout` field of the same name.
All functions accept an optional options object as the first argument, where `isTTY` is the only available option. `isTTY`, if defined, will override the `stdout` field of the same name.

@@ -53,7 +55,8 @@ The same API is also available on `stderr`.

* `options`: object [optional]
* `isTTY`: If not undefined, this value will be used to temporarily overwrite `stdout.isTTY`
* `isTTY`: If not undefined, this value will be used to temporarily overwrite `stdout.isTTY`.
* `inspect`: Returned as an object with two properties:
* `inspect`: Returned as an `EventEmitter` with two properties and one event:
* `inspect.output`: An array containing one string for each call to `stdout.write()`. This array updates every time another call to `stdout.write()` is made.
* `inspect.restore()`: Call this function to restore stdout.write to its normal behavior.
* `inspect.restore()`: Call this function to restore `stdout.write()` to its normal behavior.
* `data` event: This event fires every time `stdout.write()` is called.

@@ -63,3 +66,3 @@ Example of using `inspect()` to test a synchronous function:

```javascript
var inspect = stdout.inspect();
const inspect = stdout.inspect();
functionUnderTest();

@@ -70,7 +73,16 @@ inspect.restore();

Example of using `inspect()` to test an asynchronous function:
Example of using `inspect()` to test an asynchronous function with await:
```javascript
var inspect = stdout.inspect();
functionUnderTest(function() {
const inspect = stdout.inspect();
await functionUnderTestAsync();
inspect.restore();
assert.deepEqual(inspect.output, [ "foo\n" ]);
```
Example of using `inspect()` to test an asynchronous function with a callback:
```javascript
const inspect = stdout.inspect();
functionUnderTest(() => {
inspect.restore();

@@ -81,5 +93,18 @@ assert.deepEqual(inspect.output, [ "foo\n" ]);

Example of using `inspect()` to listen for an event:
```javascript
const inspect = stdout.inspect();
let output = "";
inspect.on("data", (chunk) => {
output += chunk;
});
functionUnderTestAsync();
inspect.restore();
assert.equal(output, "foo\n");
```
### `output = stdout.inspectSync(options, fn)`
Or: `output = stdout.inspectSync(fn)`
### `output = stdout.inspectSync(fn)`

@@ -89,7 +114,7 @@ Just like `inspect()`, but automatically restores the console when done.

* `options`: object [optional]
* `isTTY`: If not undefined, this value will be used to temporarily overwrite `stdout.isTTY`
* `isTTY`: If not undefined, this value will be used to temporarily overwrite `stdout.isTTY`.
* `fn(output)`: The function to run while inspecting stdout. After the function returns, stdout.write is automatically restored. Note that `output` is passed into this function in addition to being returned from `inspectSync()`.
* `output`: Passed into `fn` and also returned as an array containing one string for each call to `stdout.write()`. This array updates every time another call to `stdout.write()` is made.
* `output`: Passed into `fn` and also returned as an array. Contains one string for each call to `stdout.write()`. This array updates every time another call to `stdout.write()` is made.

@@ -99,3 +124,3 @@ Example of using `inspectSync()` to test a synchronous function:

```javascript
var output = stdout.inspectSync(function() {
const output = stdout.inspectSync(() => {
functionUnderTest();

@@ -109,3 +134,3 @@ });

```javascript
stdout.inspectSync(function(output) {
stdout.inspectSync((output) => {
functionUnderTest();

@@ -119,2 +144,35 @@ assert.deepEqual(output, [ "foo\n" ]);

### `output = await stdout.inspectAsync(options, fn)`
### `output = await stdout.inspectAsync(fn)`
Just like `inspectSync()`, but works with asynchronous functions.
* `options`: object [optional]
* `isTTY`: If not undefined, this value will be used to temporarily overwrite `stdout.isTTY`.
* `fnAsync(output)`: The function to run while inspecting stdout. After the function returns, stdout.write is automatically restored. Note that `output` is passed into this function in addition to being returned from `inspectSync()`.
* `output`: Passed into `fnAsync` and also returned as an array containing one string for each call to `stdout.write()`. This array updates every time another call to `stdout.write()` is made.
Example of using `inspectAsync()` to test an asynchronous function:
```javascript
const output = await stdout.inspectAsync(async () => {
await functionUnderTestAsync();
});
assert.deepEqual(output, [ "foo\n" ]);
```
Example of using `inspectAsync() to incrementally test several asynchronous functions:
```javascript
await stdout.inspectAsync(async (output) => {
await functionUnderTestAsync();
assert.deepEqual(output, [ "foo\n" ]);
await anotherFunctionUnderTestAsync();
assert.deepEqual(output, [ "foo\n", "bar\n" ]);
});
```
### `restore = stdout.ignore(options)`

@@ -132,3 +190,3 @@

```javascript
var restore = stdout.ignore();
const restore = stdout.ignore();
functionUnderTest();

@@ -138,7 +196,7 @@ restore();

Example of using `ignore()` to prevent an asynchronous function from writing to the console:
Example of using `ignore()` to prevent an asynchronous function with a callback from writing to the console:
```javascript
var restore = stdout.ignore();
functionUnderTest(function() {
const restore = stdout.ignore();
functionUnderTest(() => {
restore();

@@ -151,9 +209,9 @@ });

```javascript
var restoreStdout;
let restoreStdout;
beforeEach(function() {
beforeEach(() => {
restoreStdout = stdout.ignore();
});
afterEach(function() {
afterEach(() => {
restoreStdout();

@@ -167,3 +225,3 @@ });

### `ignoreSync(options, fn)`
Or: `ignoreSync(fn)`
### `ignoreSync(fn)`

@@ -180,3 +238,3 @@ Just like `ignore()`, but automatically restores the console when done.

```javascript
stdout.ignoreSync(function() {
stdout.ignoreSync(() => {
functionUnderTest();

@@ -187,4 +245,25 @@ });

### `await ignoreAsync(options, fnAsync)`
### `await ignoreAsync(fn)`
Just like `ignoreSync()`, but works with async/await.
* `options`: object [optional]
* `isTTY`: If not undefined, this value will be used to temporarily overwrite `stdout.isTTY`
* `fnAsync()`: The function to run while ignoring stdout. After the function returns, stdout.write is automatically restored.
Example of using `ignoreSync()` to prevent an asynchronous function from writing to the console:
```javascript
await stdout.ignoreAsync(async () => {
await functionUnderTestAsync();
});
```
## Version History
__2.0.0:__ Add events to inspect(). Add inspectAsync(), ignoreAsync(). **BREAKING CHANGE:** Requires Node 7.6.0 or higher (due to async/await support)
__1.1.0:__ Add ability to override stdout.isTTY (and stderr.isTTY).

@@ -205,3 +284,5 @@

Inspect `data` event added by Tim Toohey.
### Release Process

@@ -208,0 +289,0 @@

// Copyright (c) 2014-2015 Titanium I.T. LLC. All rights reserved. For license, see "README" or "LICENSE" file.
"use strict";
var assert = require("chai").assert;
var stdout = require("./index.js").stdout;
var stderr = require("./index.js").stderr;
const assert = require("chai").assert;
const stdout = require("./index.js").stdout;
const stderr = require("./index.js").stderr;

@@ -11,5 +11,5 @@ describe("'synchronous' inspect", function() {

it("calls passed-in function synchronously", function() {
var fnCalled = false;
var inspectReturned = false;
stdout.inspectSync(function(output) {
let fnCalled = false;
let inspectReturned = false;
stdout.inspectSync((output) => {
fnCalled = true;

@@ -22,8 +22,8 @@ assert.isFalse(inspectReturned, "function should be called before inspect call returns");

it("fails nicely when user forgets to pass in a function", function() {
var errMsg = "inspectSync() requires a function parameter. Did you mean to call inspect()?";
assert.throws(function() {
it("fails nicely when user forgets to pass in a function", () => {
const errMsg = "inspectSync() requires a function parameter. Did you mean to call inspect()?";
assert.throws(() => {
stdout.inspectSync();
}, errMsg);
assert.throws(function() {
assert.throws(() => {
stdout.inspectSync({});

@@ -34,3 +34,3 @@ }, errMsg);

it("provides writes to passed-in function", function() {
stdout.inspectSync(function(output) {
stdout.inspectSync((output) => {
assert.deepEqual(output, [], "nothing written");

@@ -48,3 +48,3 @@

it("supports console.log", function() {
stdout.inspectSync(function(output) {
stdout.inspectSync((output) => {
console.log("quux");

@@ -57,4 +57,4 @@ assert.deepEqual(output, ["quux\n"], "console.log()");

// This is a bit weird. We're going to assume inspectSync() works and use it to test inspectSync(). Inception!
stdout.inspectSync(function(output) {
stdout.inspectSync(function() {
stdout.inspectSync((output) => {
stdout.inspectSync(() => {
console.log("foo");

@@ -67,4 +67,4 @@ });

it("mocks isTTY value", function() {
var originalIsTTY = process.stdout.isTTY;
stdout.inspectSync({ isTTY: !originalIsTTY }, function() {
const originalIsTTY = process.stdout.isTTY;
stdout.inspectSync({ isTTY: !originalIsTTY }, () => {
assert.equal(process.stdout.isTTY, !originalIsTTY, 'isTTY should be changed');

@@ -75,9 +75,9 @@ });

it("uses existing isTTY value by default", function() {
it("uses existing isTTY value by default", () => {
// Testing for various argument lists
var originalIsTTY = process.stdout.isTTY;
stdout.inspectSync(function() {
const originalIsTTY = process.stdout.isTTY;
stdout.inspectSync(() => {
assert.equal(process.stdout.isTTY, originalIsTTY, 'isTTY should not be changed');
});
stdout.inspectSync({}, function() {
stdout.inspectSync({}, () => {
assert.equal(process.stdout.isTTY, originalIsTTY, 'isTTY should not be changed');

@@ -87,4 +87,4 @@ });

// testing for both original values of isTTY for failure modes that don't occur for both isTTY=false and isTTY=true
stdout.inspectSync({ isTTY: true }, function() {
stdout.inspectSync(function() {
stdout.inspectSync({ isTTY: true }, () => {
stdout.inspectSync(() => {
assert.equal(process.stdout.isTTY, true, 'isTTY should still be true if original value was true');

@@ -94,4 +94,4 @@ });

stdout.inspectSync({ isTTY: false }, function() {
stdout.inspectSync(function() {
stdout.inspectSync({ isTTY: false }, () => {
stdout.inspectSync(() => {
assert.equal(process.stdout.isTTY, false, 'isTTY should still be false if original value was false');

@@ -104,5 +104,5 @@ });

// More inception!
stdout.inspectSync(function(output) {
var originalIsTTY = process.stdout.isTTY;
stdout.inspectSync({ isTTY: !originalIsTTY }, function() {
stdout.inspectSync((output) => {
const originalIsTTY = process.stdout.isTTY;
stdout.inspectSync({ isTTY: !originalIsTTY }, () => {
// this space intentionally left blank

@@ -118,7 +118,7 @@ });

// inception!
stdout.inspectSync(function(output) {
var originalIsTTY = process.stdout.isTTY;
var exceptionPropagated = false;
stdout.inspectSync((output) => {
const originalIsTTY = process.stdout.isTTY;
let exceptionPropagated = false;
try {
stdout.inspectSync({ isTTY: !process.stdout.isTTY }, function() {
stdout.inspectSync({ isTTY: !process.stdout.isTTY }, () => {
throw new Error("intentional exception");

@@ -138,3 +138,3 @@ });

it("also returns output", function() {
var output = stdout.inspectSync(function() {
const output = stdout.inspectSync(() => {
console.log("foo");

@@ -149,9 +149,77 @@ });

it("awaits passed-in function", async function() {
let fnCalled = false;
let inspectReturned = false;
await stdout.inspectAsync(async function(output) {
await tickAsync();
fnCalled = true;
assert.isFalse(inspectReturned, "function should be called before inspect call returns");
});
inspectReturned = true;
assert.isTrue(fnCalled, "function should have been called");
});
it("fails nicely when user forgets to pass in a function", async function() {
const errMsg = "inspectAsync() requires a function parameter. Did you mean to call inspect()?";
await assertThrowsAsync(async () => {
await stdout.inspectAsync();
}, errMsg);
await assertThrowsAsync(async () => {
await stdout.inspectAsync({});
}, errMsg);
});
it("provides writes to passed-in function", async function() {
await stdout.inspectAsync(async (output) => {
process.stdout.write("foo");
assert.deepEqual(output, ["foo"], "one call to stdout.write()");
});
});
it("restores old behavior when done", async function() {
await stdout.inspectAsync(async (output) => {
await stdout.inspectAsync(async () => {
// this space intentionally left blank
});
console.log("foo");
assert.deepEqual(output, ["foo\n"], "console should be restored");
});
});
it("restores old behavior even when an exception occurs", async function() {
await stdout.inspectAsync(async (output) => {
let exceptionPropagated = false;
try {
await stdout.inspectAsync(async () => {
throw new Error("intentional exception");
});
}
catch(err) {
exceptionPropagated = true;
}
assert.isTrue(exceptionPropagated, "exception should be propagated");
console.log("foo");
assert.deepEqual(output, ["foo\n"], "console should be restored");
});
});
it("also returns output", async function() {
const output = await stdout.inspectAsync(async () => {
console.log("foo");
});
assert.deepEqual(output, ["foo\n"], "returned output");
});
});
describe("neutral inspect", function() {
it("fails nicely when user confuses it for inspectSync and passes in a function", function() {
var errMsg = "inspect() doesn't take a function parameter. Did you mean to call inspectSync()?";
assert.throws(function() {
stdout.inspect(function() {});
const errMsg = "inspect() doesn't take a function parameter. Did you mean to call inspectSync()?";
assert.throws(() => {
stdout.inspect(() => {});
}, errMsg);
assert.throws(function() {
stdout.inspect({}, function() {});
assert.throws(() => {
stdout.inspect({}, () => {});
}, errMsg);

@@ -161,3 +229,3 @@ });

it("is like synchronous version, except you have to restore it manually", function() {
var inspect = stdout.inspect();
const inspect = stdout.inspect();
console.log("foo");

@@ -168,6 +236,17 @@ assert.deepEqual(inspect.output, ["foo\n"], "output");

it("emits 'data' event when data written", function() {
const inspect = stdout.inspect();
const data = [];
inspect.on("data", (string) => {
data.push(string);
});
console.log("foo");
inspect.restore();
assert.deepEqual(data, ["foo\n"], "chunk should be emitted");
});
it("prevents output to console until restored", function() {
// inception!
stdout.inspectSync(function(output) {
var inspect = stdout.inspect();
stdout.inspectSync((output) => {
const inspect = stdout.inspect();

@@ -189,3 +268,3 @@ console.log("foo");

it("fails nicely when user forgets to pass in a function", function() {
assert.throws(function() {
assert.throws(() => {
stdout.ignoreSync();

@@ -196,7 +275,11 @@ }, "ignoreSync() requires a function parameter. Did you mean to call ignore()?");

it("simply disables output to console", function() {
let fnRan = false;
// We'll use inspect() to make sure ignore() works. Inception! (Okay, that joke's getting old. Too bad! Mwahaha!)
stdout.inspectSync(function(output) {
stdout.ignoreSync(function() {
stdout.inspectSync((output) => {
stdout.ignoreSync(() => {
console.log("foo");
fnRan = true;
});
assert.equal(fnRan, true, "should have ran function");
assert.deepEqual(output, [], "console should be ignored");

@@ -209,3 +292,3 @@ console.log("bar");

it("doesn't provide any parameters", function() {
stdout.ignoreSync(function() {
stdout.ignoreSync(() => {
assert.equal(arguments.length, 0, "# of arguments");

@@ -220,5 +303,38 @@ });

it("fails nicely when user forgets to pass in a function", async function() {
await assertThrowsAsync(async () => {
await stdout.ignoreAsync();
}, "ignoreAsync() requires a function parameter. Did you mean to call ignore()?");
});
it("simply disables output to console, and works with async function", async function() {
let fnRan = false;
await stdout.inspectAsync(async (output) => {
await stdout.ignoreAsync(async () => {
await tickAsync();
console.log("foo");
fnRan = true;
});
assert.equal(fnRan, true, "should have ran or awaited function");
assert.deepEqual(output, [], "console should be ignored");
console.log("bar");
assert.deepEqual(output, ["bar\n"], "console should be restored");
});
});
it("doesn't provide any parameters", async function() {
await stdout.ignoreAsync(async () => {
assert.equal(arguments.length, 0, "# of arguments");
});
});
});
describe("neutral ignore", function() {
it("fails nicely when user confuses it for ignoreSync and passes in a function", function() {
assert.throws(function() {
stdout.ignore(function() {});
assert.throws(() => {
stdout.ignore(() => {});
}, "ignore() doesn't take a function parameter. Did you mean to call ignoreSync()?");

@@ -229,4 +345,4 @@ });

// inception!
stdout.inspectSync(function(output) {
var restore = stdout.ignore();
stdout.inspectSync((output) => {
const restore = stdout.ignore();

@@ -255,3 +371,3 @@ console.log("foo");

it("actually works", function() {
var inspect = stderr.inspect();
const inspect = stderr.inspect();
process.stderr.write("foo");

@@ -262,2 +378,28 @@ assert.deepEqual(inspect.output, ["foo"], "output");

});
});
async function tickAsync() {
await new Promise((resolve) => {
setImmediate(resolve);
});
}
async function assertThrowsAsync(fnAsync, expectedRegexOrExactString, message) {
message = message ? `${message}: ` : "";
try {
await fnAsync();
}
catch (err) {
if (expectedRegexOrExactString === undefined) return;
if (typeof expectedRegexOrExactString === "string") {
assert.equal(err.message, expectedRegexOrExactString, message);
}
else {
assert.matches(err.message, expectedRegexOrExactString, message);
}
return;
}
assert.fail(`${message}Expected exception: ${expectedRegexOrExactString}`);
}
// Copyright (c) 2014-2015 Titanium I.T. LLC. All rights reserved. For license, see "README" or "LICENSE" file.
"use strict";
const EventEmitter = require("events");
exports.stdout = new TestStream(process.stdout);
exports.stderr = new TestStream(process.stderr);
class TestStream {
function TestStream(stream) {
this._stream = stream;
}
constructor(stream) {
this._stream = stream;
}
TestStream.prototype.inspect = function(options) {
expectNoFunction(arguments, "inspect", "inspectSync");
inspect(options) {
expectNoFunction(arguments, "inspect", "inspectSync");
var isTTY;
if (options && options.isTTY !== undefined) {
isTTY = options.isTTY;
}
let isTTY;
if (options && options.isTTY !== undefined) {
isTTY = options.isTTY;
}
// This code inspired by http://userinexperience.com/?p=714
var output = [];
var stream = this._stream;
// This code inspired by http://userinexperience.com/?p=714
const output = [];
const stream = this._stream;
const res = new EventEmitter();
var originalWrite = stream.write;
stream.write = function(string) {
output.push(string);
};
const originalWrite = stream.write;
stream.write = (string) => {
output.push(string);
res.emit("data", string);
};
var originalIsTTY = stream.isTTY;
if (isTTY !== undefined) {
stream.isTTY = isTTY;
}
const originalIsTTY = stream.isTTY;
if (isTTY !== undefined) {
stream.isTTY = isTTY;
}
return {
output: output,
restore: function() {
res.output = output;
res.restore = () => {
stream.write = originalWrite;
stream.isTTY = originalIsTTY;
};
return res;
}
inspectSync(options, fn) {
expectFunction(arguments, "inspectSync", "inspect");
[ options, fn ] = normalizeArgs(options, fn);
const inspect = this.inspect(options);
try {
fn(inspect.output);
}
};
};
finally {
inspect.restore();
}
return inspect.output;
}
TestStream.prototype.inspectSync = function(options, fn) {
expectFunction(arguments, "inspectSync", "inspect");
async inspectAsync(options, fnAsync) {
expectFunction(arguments, "inspectAsync", "inspect");
[ options, fnAsync ] = normalizeArgs(options, fnAsync);
if (arguments.length === 1) {
fn = options;
options = {};
const inspect = this.inspect(options);
try {
await fnAsync(inspect.output);
}
finally {
inspect.restore();
}
return inspect.output;
}
var inspect = this.inspect(options);
try {
ignore(options) {
expectNoFunction(arguments, "ignore", "ignoreSync");
fn(inspect.output);
return this.inspect(options).restore;
}
finally {
inspect.restore();
}
return inspect.output;
};
TestStream.prototype.ignore = function(options) {
expectNoFunction(arguments, "ignore", "ignoreSync");
ignoreSync(options, fn) {
expectFunction(arguments, "ignoreSync", "ignore");
[ options, fn ] = normalizeArgs(options, fn);
return this.inspect(options).restore;
};
this.inspectSync(options, () => {
fn();
});
}
TestStream.prototype.ignoreSync = function(options, fn) {
expectFunction(arguments, "ignoreSync", "ignore");
async ignoreAsync(options, fnAsync) {
expectFunction(arguments, "ignoreAsync", "ignore");
[ options, fnAsync ] = normalizeArgs(options, fnAsync);
if (arguments.length === 1) {
fn = options;
options = {};
await this.inspectAsync(options, async () => {
await fnAsync();
});
}
this.inspectSync(options, function() {
fn();
});
};
}
exports.stdout = new TestStream(process.stdout);
exports.stderr = new TestStream(process.stderr);
function expectNoFunction(args, calledFunction, functionToCallInstead) {

@@ -92,2 +113,10 @@ if (args.length && typeof args[0] === 'function' || args.length > 1) {

}
}
}
function normalizeArgs(options, fn) {
if (fn === undefined) {
fn = options;
options = {};
}
return [ options, fn ];
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc