Skip to content

Commit

Permalink
refactor(test): new cli setup
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jul 2, 2017
1 parent 23dde2e commit 00c1ddc
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 157 deletions.
24 changes: 0 additions & 24 deletions bin/test

This file was deleted.

3 changes: 2 additions & 1 deletion cmds/build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const build = require('../src/build')
const onError = require('../src/error-handler')

module.exports = {
command: 'build',
Expand All @@ -18,6 +19,6 @@ module.exports = {
}
},
handler (argv) {
build(argv)
build(argv).catch(onError)
}
}
30 changes: 30 additions & 0 deletions cmds/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

const test = require('../src/test')
const onError = require('../src/error-handler')

module.exports = {
command: 'test',
desc: 'Test your code in different environments',
builder: {
target: {
alias: 't',
describe: 'In which target environment to execute the tests',
type: 'array',
choices: ['node', 'browser', 'webworker'],
default: ['node', 'browser', 'webworker']
},
verbose: {
alias: 'v',
describe: 'Print verbose test output',
default: false
},
watch: {
describe: 'Watch files for changes and rerun tests (only available for browser based tests)',
default: false
}
},
handler (argv) {
test(argv).catch(onError)
}
}
3 changes: 2 additions & 1 deletion config/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ if (process.env.SAUCE_USERNAME &&

module.exports = function (config) {
config.set({
frameworks: ['mocha'],
basePath: process.cwd(),
preprocessors: {
'test/**/*.js': ['webpack', 'sourcemap']
Expand All @@ -87,7 +88,7 @@ module.exports = function (config) {
},
port: 9876,
colors: true,
logLevel: process.env.DEBUG ? config.LOG_DEBUG : config.LOG_INFO,
logLevel: config.LOG_WARN,
autoWatch: false,
browsers: browsers,
customLaunchers: launchers,
Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"aegir": "./cli.js",
"aegir-lint": "./cli.js lint",
"aegir-build": "./cli.js build",
"aegir-test": "bin/test",
"aegir-test": "./cli.js test",
"aegir-release": "bin/release",
"aegir-coverage": "bin/coverage",
"aegir-docs": "bin/docs"
Expand Down Expand Up @@ -42,6 +42,7 @@
"eslint": "^4.1.1",
"eslint-config-aegir": "^1.0.1",
"fs-extra": "^3.0.1",
"glob": "^7.1.2",
"gulp": "^3.9.1",
"gulp-bump": "^2.7.0",
"gulp-conventional-changelog": "^1.1.3",
Expand Down Expand Up @@ -69,10 +70,13 @@
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^2.0.3",
"listr": "^0.12.0",
"lodash": "^4.17.4",
"lodash.camelcase": "^4.3.0",
"lodash.includes": "^4.3.0",
"lodash.upperfirst": "^4.3.1",
"mocha": "^3.4.1",
"mocha": "^3.4.2",
"ora": "^1.3.0",
"p-map": "^1.1.1",
"path-exists": "^3.0.0",
"pretty-hrtime": "^1.0.3",
"pump": "^1.0.2",
Expand All @@ -86,7 +90,8 @@
"uglify-es": "^3.0.23",
"webpack": "^3.0.0",
"webpack-merge": "^4.1.0",
"yargs": "^8.0.2"
"yargs": "^8.0.2",
"zen-observable": "^0.5.2"
},
"repository": {
"type": "git",
Expand Down
15 changes: 15 additions & 0 deletions src/error-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const chalk = require('chalk')

function onError (err) {
if (!err) {
return
}

chalk.red(err.message)
chalk.gray(err.stack)
process.exit(1)
}

module.exports = onError
58 changes: 58 additions & 0 deletions src/test/browser-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict'

const path = require('path')
const timeout = require('../../config/custom').timeout
const user = require('../../config/user').customConfig

const CONFIG_FILE = path.join(__dirname, '..', '..', 'config', 'karma.conf.js')

let userFiles = []
if (user.karma && user.karma.files) {
userFiles = user.karma.files
}

const webworkerClient = {
mochaWebWorker: {
pattern: [
'test/browser.js',
'test/**/*.spec.js'
],
mocha: {
timeout: timeout
}
}
}

const defaultClient = {
mocha: {
timeout: timeout
}
}

function getConfig (isWebworker, ctx) {
return {
configFile: CONFIG_FILE,
singleRun: !ctx.watch,
watch: ctx.watch,
frameworks: isWebworker ? ['mocha-webworker'] : ['mocha'],
logLevel: ctx.verbose ? 'debug' : 'error',
client: isWebworker ? webworkerClient : defaultClient,
mochaOwnReporter: {
reporter: ctx.verbose ? 'spec' : 'progress'
},
files: [{
pattern: 'test/browser.js',
included: !isWebworker
}, {
pattern: 'test/**/*.spec.js',
included: !isWebworker
}, {
pattern: 'test/fixtures/**/*',
watched: false,
served: true,
included: false
}].concat(userFiles)
}
}

module.exports = getConfig
26 changes: 26 additions & 0 deletions src/test/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

const Server = require('karma').Server
const getConfig = require('./browser-config')

const IS_SAUCE = process.env.SAUCE_USERNAME && process.env.TRAVIS

function testBrowser (isWebworker) {
return (ctx) => new Promise((resolve, reject) => {
const config = getConfig(isWebworker, ctx)

const server = new Server(config, (exitCode) => {
if (exitCode > 0 && !IS_SAUCE) {
reject(new Error('Some tests are failing'))
}
resolve()
})

server.start()
})
}

module.exports = {
default: testBrowser(false),
webworker: testBrowser(true)
}
27 changes: 27 additions & 0 deletions src/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const Listr = require('listr')
const _ = require('lodash')

const node = require('./node')
const browser = require('./browser')

const TASKS = new Listr([{
title: 'Test Node.js',
task: node,
enabled: (ctx) => _.includes(ctx.target, 'node')
}, {
title: 'Test Browser',
task: browser.default,
enabled: (ctx) => _.includes(ctx.target, 'browser')
}, {
title: 'Test Webworker',
task: browser.webworker,
enabled: (ctx) => _.includes(ctx.target, 'webworker')
}])

function test (opts) {
return TASKS.run(opts)
}

module.exports = test
35 changes: 35 additions & 0 deletions src/test/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const Mocha = require('mocha')
const glob = require('glob')
const _ = require('lodash')

const CONFIG = require('../../config/custom').timeout

const FILES = [
'test/node.js',
'test/**/*.spec.js'
]

function testNode (ctx) {
const mocha = new Mocha({
ui: 'bdd',
reporter: ctx.verbose ? 'spec' : 'progress',
useColors: true,
timeout: CONFIG.timeout
})

const files = _.flatten(FILES.map((pattern) => glob.sync(pattern)))
files.forEach((file) => mocha.addFile(file))

return new Promise((resolve, reject) => {
mocha.run((failure) => {
if (failure) {
reject(new Error(`Failed ${failure} tests`))
}
resolve()
})
})
}

module.exports = testNode
7 changes: 7 additions & 0 deletions src/test/webworker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

function testWebworker () {
console.log('webworker')
}

module.exports = testWebworker
Loading

0 comments on commit 00c1ddc

Please sign in to comment.