Skip to content

Instantly share code, notes, and snippets.

@plmrry
Last active September 30, 2016 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plmrry/5179ef132c3f39368ff092e5b1ad76ec to your computer and use it in GitHub Desktop.
Save plmrry/5179ef132c3f39368ff092e5b1ad76ec to your computer and use it in GitHub Desktop.
Sequential Tests with Tape and Bash
node_modules

Sequential Tests with Tape and Bash

A demo of using a bash script to run tape tests in sequence.

Scenario

  • Each test script can be run in isolation, and passes:
node one-test.js
node two-test.js
  • When running all tests via tape globbing, second test fails:
npm run tape '*-test.js'
# or
npm run bad-test
  • Using bash, all tests run and pass:
bash run-tests.sh
# or
npm test

The Problem

  • Code being tested calls a global function repeatedly, and does not stop.
  • tape allows for running multiple tests via globbing, e.g.: tape '*-test.js'. However, to accomplish this tape will require each script into the same environment, meaning they will share global.

The Solution

  • A bash script that runs each test independently, in sequence.
const tape = require('tape');
global.callback = () => {};
global.callTheFunc = () => {
global.callback();
}
tape.onFinish(() => {
process.exit(0);
})
tape('test one', test => {
let count = 0;
global.callback = () => {
console.log('called in test one');
count++;
}
setInterval(() => {
global.callTheFunc();
}, 100);
setTimeout(() => {
// test.fail();
test.equal(count, 10, "Called ten times");
test.end();
}, 1050);
});
{
"name": "run-tests",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"bad-test": "tape '*-test.js'",
"test": "bash run-tests.sh",
"tape": "tape",
"gistup": "gistup"
},
"author": "",
"license": "ISC",
"devDependencies": {
"tape": "^4.6.1"
}
}
#! /bin/bash
for file in ./*-test.js
do
node "$file"
if [ $? -ne 0 ]
then
exit 1
fi
done
const tape = require('tape');
tape.onFinish(() => {
process.exit(0);
})
tape('test two', test => {
let count = 0;
global.callback = () => {
console.log('called in test two');
count++;
}
setTimeout(() => {
test.equal(count, 0, "Called zero times");
test.end();
}, 1000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment