Skip to content

Commit

Permalink
Merge pull request #363 from danjm/tests-option-tap-spec
Browse files Browse the repository at this point in the history
Add tap-spec option to npm test commands for mocha style reporting
  • Loading branch information
holgerd77 authored Oct 15, 2018
2 parents 6c0e1f1 + 76618dd commit e9b60a1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,23 @@ test docs), provided by the index of the array element in the test ``transaction
Run a state test from a specified source file not under the ``tests`` directory:
`node ./tests/tester -s --customStateTest='{path_to_file}'`

#### Running tests with a reporter/formatter

`npm run formatTest -t [npm script name OR node command]` will pipe to `tap-spec` by default.

To pipe the results of the API tests through `tap-spec`:

`npm run formatTest -- -t testAPI`

To pipe the results of tests run with a node command through `tap-spec`:

`npm run formatTest -- -t "./tests/tester -b --dir='bcBlockGasLimitTest'"`

The `-with` flag allows the specification of a formatter of your choosing:

`npm install -g tap-mocha-reporter`
`npm run formatTest -- -t testAPI -with 'tap-mocha-reporter json'`

#### Skipping Tests

There are three types of skip lists (``BROKEN``, ``PERMANENT`` and ``SLOW``) which
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"test": "node ./tests/tester -a",
"lint": "standard",
"prepublishOnly": "npm run lint && npm run build:dist && npm run testBuildIntegrity",
"build:dist": "babel lib/ -d dist/"
"build:dist": "babel lib/ -d dist/",
"formatTest": "node ./scripts/formatTest"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -60,6 +61,7 @@
"minimist": "^1.1.1",
"nyc": "^12.0.2",
"standard": "^10.0.0",
"tap-spec": "^5.0.0",
"tape": "4.6.3"
},
"author": "mjbecze <[email protected]>",
Expand Down
37 changes: 37 additions & 0 deletions scripts/formatTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { spawn } = require('child_process')
const fs = require('fs')
const path = require('path')

const testScript = process.argv.find((arg, i, array) => array[i - 1] === '-t')
const formatter = process.argv.find((arg, i, array) => array[i - 1] === '-with')

runTestsWithFormatter(testScript, formatter)

function runTestsWithFormatter (testScript, formatter = './node_modules/.bin/tap-spec') {
if (!testScript) {
console.log('No test script specified!')
return
}

const packageJson = fs.readFileSync(path.dirname(__dirname) + '/package.json', 'utf8')
const parsedPackageJson = JSON.parse(packageJson)
const npmTestScriptNames = Object.keys(parsedPackageJson.scripts)

const commandToRun = npmTestScriptNames.find(name => name === testScript)
? `npm run ${testScript}`
: `node ${testScript}`

const child = spawn('sh', ['-c', `${commandToRun} | ${formatter}`])

child.stdout.on('data', (data) => {
process.stdout.write(data)
})

child.stderr.on('data', (data) => {
process.stdout.write(data)
})

child.on('exit', (data) => {
process.stdout.write('Done')
})
}

0 comments on commit e9b60a1

Please sign in to comment.