Skip to content

Commit

Permalink
Tests: Add CLI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Trent Willis committed Mar 22, 2017
1 parent 1847361 commit e0201fc
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 10 deletions.
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
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@
"dependencies": {
"commander": "2.9.0",
"exists-stat": "1.0.0",
"js-reporters": "1.1.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 @@ -67,7 +69,8 @@
"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"
},
"commitplease": {
"components": [
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

0 comments on commit e0201fc

Please sign in to comment.