Skip to content
Myr

custom-common.js

This file

Path

test/verbose/custom-common.js

49 lines · 2.5 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

History of this file

Take it

TweaksSindre Sorhusbefore adoption698a1b8

lines 1–2why, one step back
1import assert from 'node:assert/strict';
2import test from 'node:test';

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 3–9why, one step back
3import {setFixtureDirectory} from '../helpers/fixtures-directory.js';
4import {foobarString} from '../helpers/input.js';5import {nestedSubprocess} from '../helpers/nested.js';6import {QUOTE, getCommandLine, testTimestamp} from '../helpers/verbose.js';78setFixtureDirectory();9

TweaksSindre Sorhusbefore adoption698a1b8

line 10why, one step back
10const testCustomReturn = async (verboseOutput, expectedOutput) => {

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 11–15why, one step back
11 const {stderr} = await nestedSubprocess(
12 'empty.js',13 {optionsFixture: 'custom-return.js', optionsInput: {verboseOutput}},14 {stripFinalNewline: false},15 );

TweaksSindre Sorhusbefore adoption698a1b8

line 16why, one step back
16 assert.equal(stderr, expectedOutput);

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 17–18why, one step back
17};
18

TweaksSindre Sorhusbefore adoption698a1b8

lines 19–28why, one step back
19test('"verbose" returning a string prints it', () => testCustomReturn(`${foobarString}\n`, `${foobarString}\n`));
20test('"verbose" returning a string without a newline adds it', () => testCustomReturn(foobarString, `${foobarString}\n`));21test('"verbose" returning a string with multiple newlines keeps them', () => testCustomReturn(`${foobarString}\n\n`, `${foobarString}\n\n`));22test('"verbose" returning an empty string prints an empty line', () => testCustomReturn('', '\n'));23test('"verbose" returning undefined ignores it', () => testCustomReturn(undefined, ''));24test('"verbose" returning a number ignores it', () => testCustomReturn(0, ''));25test('"verbose" returning a bigint ignores it', () => testCustomReturn(0n, ''));26test('"verbose" returning a boolean ignores it', () => testCustomReturn(true, ''));27test('"verbose" returning an object ignores it', () => testCustomReturn({}, ''));28test('"verbose" returning an array ignores it', () => testCustomReturn([], ''));

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

line 29why, one step back
29

TweaksSindre Sorhusbefore adoption698a1b8

line 30why, one step back
30test('"verbose" receives verboseLine string as first argument', async () => {

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

line 31why, one step back
31 const {stderr} = await nestedSubprocess('noop.js', [foobarString], {optionsFixture: 'custom-uppercase.js'});

TweaksSindre Sorhusbefore adoption698a1b8

line 32why, one step back
32 assert.equal(getCommandLine(stderr), `${testTimestamp} [0] $ NOOP.js ${foobarString}`);

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 33–34why, one step back
33});
34

TweaksSindre Sorhusbefore adoption698a1b8

line 35why, one step back
35test('"verbose" can print as JSON', async () => {

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 36–37why, one step back
36 const {stderr} = await nestedSubprocess('noop.js', ['. .'], {optionsFixture: 'custom-json.js', type: 'duration', reject: false});
37 const {type, message, escapedCommand, commandId, timestamp, piped, result, options} = JSON.parse(stderr);

TweaksSindre Sorhusbefore adoption698a1b8

lines 38–48why, one step back
38 assert.equal(type, 'duration');
39 assert.ok(message.includes('done in'));40 assert.equal(escapedCommand, `noop.js ${QUOTE}. .${QUOTE}`);41 assert.equal(commandId, '0');42 assert.ok(Number.isInteger(new Date(timestamp).getTime()));43 assert.equal(piped, false);44 assert.equal(result.failed, false);45 assert.equal(result.exitCode, 0);46 assert.equal(result.stdout, '. .');47 assert.equal(result.stderr, '');48 assert.equal(options.reject, false);

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

line 49why, one step back
49});