Skip to content

Commit

Permalink
feat(tests): Add more testing features
Browse files Browse the repository at this point in the history
Add ability to run without coverage, add ability to run without cache, have mastarm control babel

transformation instead of requiring projects to define a .babelrc file.
  • Loading branch information
evansiroky committed Sep 28, 2016
1 parent b47a0a9 commit b9ef80e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ Run the [Jest](http://facebook.github.io/jest/) test runner on your project. It

```shell
$ mastarm test

Usage: test [options]

Run tests using Jest

Options:

-h, --help output usage information
-u, --update-snapshots Force update of snapshots. USE WITH CAUTION.
--coverage Run Jest with coverage reporting
--no-cache Run Jest without cache

```

[npm-image]: https://img.shields.io/npm/v/mastarm.svg?maxAge=2592000&style=flat-square
Expand Down
7 changes: 5 additions & 2 deletions bin/mastarm
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,14 @@ commander
.command('test')
.description('Run tests using Jest')
.option('-u, --update-snapshots', 'Force update of snapshots. USE WITH CAUTION.')
.option('--coverage', 'Run Jest with coverage reporting')
.option('--no-cache', 'Run Jest without cache')
.action(function (options) {
const jest = require('jest')
const config = loadConfig(process.cwd(), commander.config, commander.env)
const generateTestConfig = require('../lib/test.js')
jest.run(generateTestConfig(config, options))
const testUtils = require('../lib/test')
testUtils.setupTestEnvironment(config)
jest.run(testUtils.generateTestConfig(options))
})

commander.parse(process.argv)
Expand Down
11 changes: 11 additions & 0 deletions lib/jestPreprocessor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const babel = require('babel-core')
const jestPreset = require('babel-preset-jest')

module.exports = {
process: function (src) {
const transformCfg = {
presets: ['es2015', 'react', 'stage-0', jestPreset]
}
return babel.transform(src, transformCfg).code
}
}
20 changes: 14 additions & 6 deletions lib/test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
const path = require('path')

module.exports = function generateTestConfig (config, options) {
if (config.messages) {
process.env.MESSAGES = JSON.stringify(config.messages)
}
module.exports.generateTestConfig = (options) => {
const jestConfig = {
collectCoverage: true,
collectCoverage: options.coverage,
collectCoverageFrom: ['lib/**/*.js'],
coverageDirectory: 'coverage'
coverageDirectory: 'coverage',
scriptPreprocessor: path.resolve(__dirname, 'jestPreprocessor.js')
}
const jestArguments = []
if (options.updateSnapshots) {
jestArguments.push('-u')
}
if (options.cache === false) {
jestArguments.push('--no-cache')
}
jestArguments.push('--config', JSON.stringify(jestConfig))
return jestArguments
}

module.exports.setupTestEnvironment = (config) => {
if (config.messages) {
process.env.MESSAGES = JSON.stringify(config.messages)
}
}

0 comments on commit b9ef80e

Please sign in to comment.