-
Notifications
You must be signed in to change notification settings - Fork 783
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
CLI: Introduce QUnit CLI #1115
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ node_js: | |
- "7" | ||
env: | ||
- NPM_SCRIPT=test | ||
- NPM_SCRIPT=test:cli | ||
matrix: | ||
fast_finish: true | ||
allow_failures: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"env": { | ||
"node": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"env": { | ||
"node": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} ) ); | ||
} ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thebin
property in this same file, here?There was a problem hiding this comment.
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 callqunit
as actual users will after installing it.There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.