Skip to content

Commit

Permalink
CLI: Use built-in reporters instead of via js-reporters package
Browse files Browse the repository at this point in the history
  • Loading branch information
Krinkle committed May 1, 2021
1 parent 3c99f8e commit 3a8cbba
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
10 changes: 4 additions & 6 deletions bin/qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

const program = require( "commander" );
const run = require( "../src/cli/run" );
const FindReporter = require( "../src/cli/find-reporter" );
const { displayAvailableReporters } = require( "../src/cli/find-reporter" );
const pkg = require( "../package.json" );

const findReporter = FindReporter.findReporter;
const displayAvailableReporters = FindReporter.displayAvailableReporters;

const description = `Runs tests using the QUnit framework.
Files should be a space-separated list of file/directory paths and/or glob
Expand Down Expand Up @@ -41,12 +38,13 @@ program
const opts = program.opts();

if ( opts.reporter === true ) {
displayAvailableReporters();
const requireQUnit = require( "../src/cli/require-qunit" );
displayAvailableReporters( requireQUnit().reporters );
}

const options = {
filter: opts.filter,
reporter: findReporter( opts.reporter ),
reporter: opts.reporter,
requires: opts.require,
seed: opts.seed
};
Expand Down
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
},
"dependencies": {
"commander": "7.1.0",
"js-reporters": "2.0.0",
"node-watch": "0.7.1",
"tiny-glob": "0.2.8"
},
Expand Down
13 changes: 4 additions & 9 deletions src/cli/find-reporter.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
"use strict";

const JSReporters = require( "js-reporters" );
const utils = require( "./utils" );
const pkg = require( "../../package.json" );

const hasOwn = Object.prototype.hasOwnProperty;
const builtin = {
console: JSReporters.ConsoleReporter,
tap: JSReporters.TapReporter
};

function findReporter( reporterName ) {
function findReporter( reporterName, builtin ) {
if ( !reporterName ) {
return JSReporters.TapReporter;
return builtin.tap;
}

// First, check if the reporter is one of the standard js-reporters ones
Expand All @@ -30,10 +25,10 @@ function findReporter( reporterName ) {
}

// If we didn't find a reporter, display the available reporters and exit
displayAvailableReporters( reporterName );
displayAvailableReporters( builtin, reporterName );
}

function displayAvailableReporters( inputReporterName ) {
function displayAvailableReporters( builtin, inputReporterName ) {
const message = [];

if ( inputReporterName ) {
Expand Down
3 changes: 2 additions & 1 deletion src/cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require( "path" );
const requireFromCWD = require( "./require-from-cwd" );
const requireQUnit = require( "./require-qunit" );
const utils = require( "./utils" );
const { findReporter } = require( "./find-reporter" );

const RESTART_DEBOUNCE_LENGTH = 200;

Expand Down Expand Up @@ -42,7 +43,7 @@ async function run( args, options ) {

options.requires.forEach( requireFromCWD );

options.reporter.init( QUnit );
findReporter( options.reporter, QUnit.reporters ).init( QUnit );

for ( let i = 0; i < files.length; i++ ) {
const filePath = path.resolve( process.cwd(), files[ i ] );
Expand Down
22 changes: 18 additions & 4 deletions test/cli/custom-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@ const expectedOutput = require( "./fixtures/expected/tap-outputs" );
const execute = require( "./helpers/execute" );

QUnit.module( "find-reporter", function() {
QUnit.test( "correctly finds js-reporters reporter by name", function( assert ) {
const reporter = findReporter( "tap" );
QUnit.test( "tap reporter from js-reporters is bundled", function( assert ) {
assert.strictEqual( QUnit.reporters.tap, JSReporters.TapReporter );
} );

QUnit.test( "find console reporter", function( assert ) {
const reporter = findReporter( "console", QUnit.reporters );
assert.strictEqual( reporter, JSReporters.ConsoleReporter );
} );

QUnit.test( "find tap reporter", function( assert ) {
const reporter = findReporter( "tap", QUnit.reporters );
assert.strictEqual( reporter, JSReporters.TapReporter );
} );

QUnit.test( "default to tap reporter", function( assert ) {
const reporter = findReporter( undefined, QUnit.reporters );
assert.strictEqual( reporter, JSReporters.TapReporter );
} );

QUnit.test( "correctly finds npm package reporter by name", function( assert ) {
const reporter = findReporter( "npm-reporter" );
QUnit.test( "find extra reporter package", function( assert ) {
const reporter = findReporter( "npm-reporter", QUnit.reporters );
assert.strictEqual( reporter, NPMReporter );
} );
} );
Expand Down

0 comments on commit 3a8cbba

Please sign in to comment.