Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Make tap and console reporters generally available #1607

Merged
merged 3 commits into from
May 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[test/cli/TapReporter.js]
trim_trailing_whitespace = false

[*.{yml,md}]
indent_style = space
indent_size = 2
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
13 changes: 7 additions & 6 deletions package-lock.json

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

2 changes: 1 addition & 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 Expand Up @@ -70,6 +69,7 @@
"grunt-eslint": "^23.0.0",
"grunt-git-authors": "^3.2.0",
"grunt-search": "^0.1.8",
"kleur": "4.1.4",
"npm-reporter": "file:./test/cli/fixtures/npm-reporter",
"nyc": "^15.1.0",
"proxyquire": "^1.8.0",
Expand Down
15 changes: 5 additions & 10 deletions src/cli/find-reporter.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
"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
// First, check if the reporter is one of the built-in ones
if ( hasOwn.call( builtin, reporterName ) ) {
return builtin[ reporterName ];
}
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
2 changes: 2 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Assert from "./assert";
import Logger from "./logger";
import Test, { test, pushFailure } from "./test";
import exportQUnit from "./export";
import reporters from "./reporters";

import config from "./core/config";
import { extend, objectType, is, now } from "./core/utilities";
Expand Down Expand Up @@ -42,6 +43,7 @@ extend( QUnit, {

dump,
equiv,
reporters,
is,
objectType,
on,
Expand Down
7 changes: 7 additions & 0 deletions src/reporters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ConsoleReporter from "./reporters/ConsoleReporter.js";
import TapReporter from "./reporters/TapReporter.js";

export default {
console: ConsoleReporter,
tap: TapReporter
};
36 changes: 36 additions & 0 deletions src/reporters/ConsoleReporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { console } from "../globals";

export default class ConsoleReporter {
constructor( runner, options = {} ) {

// Cache references to console methods to ensure we can report failures
// from tests tests that mock the console object itself.
// https://github.com/qunitjs/qunit/issues/1340
this.log = options.log || console.log.bind( console );

runner.on( "runStart", this.onRunStart.bind( this ) );
runner.on( "testStart", this.onTestStart.bind( this ) );
runner.on( "testEnd", this.onTestEnd.bind( this ) );
runner.on( "runEnd", this.onRunEnd.bind( this ) );
}

static init( runner, options ) {
return new ConsoleReporter( runner, options );
}

onRunStart( runStart ) {
this.log( "runStart", runStart );
}

onTestStart( test ) {
this.log( "testStart", test );
}

onTestEnd( test ) {
this.log( "testEnd", test );
}

onRunEnd( runEnd ) {
this.log( "runEnd", runEnd );
}
}
Loading