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

CLI: Introduce QUnit CLI #1115

Merged
merged 2 commits into from
Mar 24, 2017
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ node_js:
- "7"
env:
- NPM_SCRIPT=test
- NPM_SCRIPT=test:cli
matrix:
fast_finish: true
allow_failures:
Expand Down
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ grunt.initConfig( {
},
all: [
"*.js",
"bin/**/*.js",
"reporter/**/*.js",
"runner/**/*.js",
"src/**/*.js",
Expand Down
5 changes: 5 additions & 0 deletions bin/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"env": {
"node": true
}
}
19 changes: 19 additions & 0 deletions bin/qunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#! /usr/bin/env node

"use strict";

const program = require( "commander" );
const run = require( "./run" );
const utils = require( "./utils" );
const pkg = require( "../package.json" );

program._name = "qunit";
program
.version( pkg.version )
.usage( "[options] [files]" )
.parse( process.argv );

const args = program.args;
const files = utils.getFilesFromArgs( args );

run( files );
41 changes: 41 additions & 0 deletions bin/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use strict";

const path = require( "path" );
const TapReporter = require( "js-reporters" ).TapReporter;

// During development, QUnit is built into "dist/", but when published to npm
// we move it to "qunit/". This IIFE handles both cases.
const QUnit = ( function requireQUnit() {
try {
return require( "../qunit/qunit" );
} catch ( e ) {
if ( e.code === "MODULE_NOT_FOUND" ) {
return require( "../dist/qunit" );
}

throw e;
}
}() );

module.exports = function run( files ) {

// TODO: Enable mode where QUnit is not auto-injected, but other setup is
// still done automatically.
global.QUnit = QUnit;

// TODO: Reporter should be customizable
TapReporter.init( QUnit );

for ( let i = 0; i < files.length; i++ ) {
const filePath = path.resolve( process.cwd(), files[ i ] );
require( filePath );
}

QUnit.start();

QUnit.on( "runEnd", function setExitCode( data ) {
if ( data.testCounts.failed ) {
process.exitCode = 1;
}
} );
};
43 changes: 43 additions & 0 deletions bin/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";

const walkSync = require( "walk-sync" );
const existsStat = require( "exists-stat" );

function getFilesFromArgs( args ) {
let globs = args.slice();

// Default to files in the test directory
if ( !globs.length ) {
globs.push( "test/**/*.js" );
}

// For each of the potential globs, we check if it is a directory path and
// update it so that it matches the JS files in that directory.
globs = globs.map( glob => {
const stat = existsStat( glob );

if ( stat && stat.isDirectory() ) {
return `${glob}/**/*.js`;
} else {
return glob;
}
} );

const files = walkSync( process.cwd(), { globs } );

if ( !files.length ) {
error( "No files were found matching: " + args.join( ", " ) );
}

return files;
}

function error( message ) {
console.error( message );
process.exit( 1 );
}

module.exports = {
error,
getFilesFromArgs
};
17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,32 @@
"url": "https://github.com/qunitjs/qunit/issues"
},
"license": "MIT",
"bin": {
"qunit": "bin/qunit"
},
"files": [
"bin/",
"qunit/qunit.js",
"qunit/qunit.css",
"LICENSE.txt"
],
"dependencies": {},
"dependencies": {
"commander": "2.9.0",
"exists-stat": "1.0.0",
"js-reporters": "1.2.0",
"walk-sync": "0.3.1"
},
"devDependencies": {
"async": "2.1.4",
"babel-plugin-external-helpers": "6.18.0",
"babel-preset-es2015": "6.18.0",
"browserstack-runner": "0.4.4",
"co": "4.6.0",
"commitplease": "2.7.6",
"eslint-config-jquery": "1.0.0",
"eslint-plugin-html": "1.7.0",
"eslint-plugin-qunit": "2.3.0",
"execa": "0.6.1",
"fs-extra": "1.0.0",
"grunt": "1.0.1",
"grunt-cli": "1.2.0",
Expand All @@ -58,13 +69,15 @@
"browserstack": "grunt build && sh build/run-browserstack.sh",
"build": "grunt build",
"test": "grunt",
"coverage": "grunt coverage"
"coverage": "grunt coverage",
"test:cli": "grunt build && npm link && qunit test/cli/*.js"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a noob on npm features, this will call qunit set in the bin property in this same file, here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, by calling npm link we can call qunit as actual users will after installing it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trentmwillis You shouldn't need npm link: npm will prepend ./node_modules/.bin to PATH when this npm script is run.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It failed on a previous run. We can mess around with it after this lands, but for now I don't feel it is worth digging into.

},
"commitplease": {
"components": [
"All",
"Assert",
"Build",
"CLI",
"CSS",
"Core",
"Dump",
Expand Down
5 changes: 5 additions & 0 deletions test/cli/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"env": {
"node": true
}
}
9 changes: 9 additions & 0 deletions test/cli/fixtures/double.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
QUnit.module( "Double", function() {
QUnit.test( "has a test", function( assert ) {
assert.ok( true );
} );

QUnit.test( "has another test", function( assert ) {
assert.ok( true );
} );
} );
65 changes: 65 additions & 0 deletions test/cli/fixtures/expected/tap-outputs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Expected outputs from the TapReporter for the commands run in CLI tests
module.exports = {
"qunit":
`TAP version 13
ok 1 First > 1
ok 2 Second > 1
1..2
# pass 2
# skip 0
# todo 0
# fail 0`,

"qunit 'glob/**/*-test.js'":
`TAP version 13
ok 1 A-Test > derp
ok 2 Nested-Test > herp
1..2
# pass 2
# skip 0
# todo 0
# fail 0`,

"qunit single.js":
`TAP version 13
ok 1 Single > has a test
1..1
# pass 1
# skip 0
# todo 0
# fail 0`,

"qunit single.js double.js":
`TAP version 13
ok 1 Double > has a test
ok 2 Double > has another test
ok 3 Single > has a test
1..3
# pass 3
# skip 0
# todo 0
# fail 0`,

"qunit test":
`TAP version 13
ok 1 First > 1
ok 2 Second > 1
1..2
# pass 2
# skip 0
# todo 0
# fail 0`,

"qunit test single.js 'glob/**/*-test.js'":
`TAP version 13
ok 1 A-Test > derp
ok 2 Nested-Test > herp
ok 3 Single > has a test
ok 4 First > 1
ok 5 Second > 1
1..5
# pass 5
# skip 0
# todo 0
# fail 0`
};
9 changes: 9 additions & 0 deletions test/cli/fixtures/fail/failure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
QUnit.module( "Failure", function() {
QUnit.test( "bad", function( assert ) {
assert.ok( false );
} );

QUnit.test( "bad again", function( assert ) {
assert.ok( false );
} );
} );
5 changes: 5 additions & 0 deletions test/cli/fixtures/glob/a-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
QUnit.module( "A-Test", function() {
QUnit.test( "derp", function( assert ) {
assert.ok( true );
} );
} );
5 changes: 5 additions & 0 deletions test/cli/fixtures/glob/not-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
QUnit.module( "Not-A", function() {
QUnit.test( "1", function( assert ) {
assert.ok( true );
} );
} );
5 changes: 5 additions & 0 deletions test/cli/fixtures/glob/sub/nested-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
QUnit.module( "Nested-Test", function() {
QUnit.test( "herp", function( assert ) {
assert.ok( true );
} );
} );
5 changes: 5 additions & 0 deletions test/cli/fixtures/single.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
QUnit.module( "Single", function() {
QUnit.test( "has a test", function( assert ) {
assert.ok( true );
} );
} );
5 changes: 5 additions & 0 deletions test/cli/fixtures/test/first.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
QUnit.module( "First", function() {
QUnit.test( "1", function( assert ) {
assert.ok( true );
} );
} );
5 changes: 5 additions & 0 deletions test/cli/fixtures/test/nested/second.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
QUnit.module( "Second", function() {
QUnit.test( "1", function( assert ) {
assert.ok( true );
} );
} );
91 changes: 91 additions & 0 deletions test/cli/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"use strict";

const path = require( "path" );
const exec = require( "execa" ).shell;
const co = require( "co" );

const expectedOutput = require( "./fixtures/expected/tap-outputs" );

// Executes the provided command from within the fixtures directory
function execute( command ) {
const cwd = process.cwd();
process.chdir( path.join( __dirname, "fixtures" ) );

const execution = exec( command );

process.chdir( cwd );

return execution;
}

QUnit.module( "CLI Main", function() {
QUnit.test( "defaults to running tests in 'test' directory", co.wrap( function* ( assert ) {
const command = "qunit";
const execution = yield execute( command );

assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} ) );

QUnit.test( "errors if no test files are found to run", co.wrap( function* ( assert ) {
try {
yield execute( "qunit does-not-exist.js" );
} catch ( e ) {
assert.equal( e.stderr.indexOf( "No files were found matching" ), 0 );
}
} ) );

QUnit.test( "accepts globs for test files to run", co.wrap( function* ( assert ) {
const command = "qunit 'glob/**/*-test.js'";
const execution = yield execute( command );

assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} ) );

QUnit.test( "runs a single JS file", co.wrap( function* ( assert ) {
const command = "qunit single.js";
const execution = yield execute( command );

assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} ) );

QUnit.test( "runs multiple JS files", co.wrap( function* ( assert ) {
const command = "qunit single.js double.js";
const execution = yield execute( command );

assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} ) );

QUnit.test( "runs all JS files in a directory matching an arg", co.wrap( function* ( assert ) {
const command = "qunit test";
const execution = yield execute( command );

assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} ) );

QUnit.test( "runs multiple types of file paths", co.wrap( function* ( assert ) {
const command = "qunit test single.js 'glob/**/*-test.js'";
const execution = yield execute( command );

assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} ) );

QUnit.test( "exit code is 1 when failing tests are present", co.wrap( function* ( assert ) {
try {
yield execute( "qunit fail/*.js" );
} catch ( e ) {
assert.equal( e.code, 1 );
}
} ) );
} );
Loading