Skip to content

Instantly share code, notes, and snippets.

@etpinard
Last active August 18, 2016 15:25
Show Gist options
  • Save etpinard/9d492c9ec7acbf505fe9f59e3b3c0149 to your computer and use it in GitHub Desktop.
Save etpinard/9d492c9ec7acbf505fe9f59e3b3c0149 to your computer and use it in GitHub Desktop.
run tape tests on an html page
run tape tests on an html page
node_modules
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>simple test server</title>
</head>
<body>
<script type="text/javascript">console.log('logging from the index !!!')</script>
</body>
</html>
{
"name": "simple-test-server",
"version": "1.0.0",
"description": "run tape tests on an html page",
"scripts": {
"test": "node server.js"
},
"author": "Étienne Tétreault-Pinard",
"license": "MIT",
"dependencies": {
"browserify": "^13.1.0",
"cheerio": "^0.20.0",
"chrome-launch": "^1.1.4",
"ecstatic": "^2.1.0",
"tap-parser": "^2.0.0",
"tape": "^4.6.0",
"xhr": "^2.2.2"
}
}
var http = require('http');
var url = require('url');
var fs = require('fs');
var ecstatic = require('ecstatic');
var browserify = require('browserify');
var cheerio = require('cheerio');
var chrome = require('chrome-launch');
var tapParser = require('tap-parser');
var PORT = 8080;
var PATH_TO_INDEX = __dirname + '/index.html';
var PATH_TO_INDEX_STUB = __dirname + '/index.tmp.html';
var PATH_TO_TEST_FILE = __dirname + '/test.js';
var PATH_TO_TEST_BUNDLE = __dirname + '/test.tmp.js';
var URL = 'http://localhost:' + PORT + '/index.tmp.html' ;
var EXIT_CODE = 0;
// main
stubIndex()
.then(bundleTests)
.then(startServer)
.then(launch)
function stubIndex() {
return new Promise(function(resolve, reject) {
var html = fs.readFileSync(PATH_TO_INDEX, 'utf-8');
var $ = cheerio.load(html);
$('body').append('<script type="text/javascript" src="test.tmp.js"></script>');
fs.writeFile(PATH_TO_INDEX_STUB, $.html(), resolve);
});
}
function bundleTests() {
return new Promise(function(resolve, reject) {
var wsBundle = fs.createWriteStream(PATH_TO_TEST_BUNDLE);
browserify(PATH_TO_TEST_FILE, { debug: true })
.bundle()
.pipe(wsBundle);
wsBundle.on('close', resolve);
});
}
function startServer() {
return new Promise(function(resolve, reject) {
var server = http.createServer(ecstatic({ root: __dirname }));
server.on('request', handle);
server.listen(PORT, resolve);
});
}
function handle(req, res) {
var query = url.parse(req.url).query || '';
var parser = tapParser();
function is(query, root) {
return query.indexOf(root) !== -1;
}
if(is(query, 'data')) handleData(req, res);
if(is(query, 'done')) handleDone();
function handleData(req, res) {
req.pipe(parser);
req.pipe(process.stdout);
}
parser.on('assert', function(assert) {
if(EXIT_CODE === 0 && assert.ok === false) EXIT_CODE = 1;
})
function handleDone() {
removeBuildFiles();
process.exit(EXIT_CODE);
}
}
function launch() {
chrome(URL);
}
function removeBuildFiles() {
fs.unlinkSync(PATH_TO_INDEX_STUB);
fs.unlinkSync(PATH_TO_TEST_BUNDLE);
}
var test = require('tape');
var xhr = require('xhr');
var cnt = 0;
var noop = function() {};
var post = function(query, data) {
var opts = data ? { body: data } : {};
xhr.post('/?' + query + '&' + (cnt++), opts, noop);
};
var ws = test.createStream();
ws.on('data', function(data) {
post('data', data)
});
test.onFinish(function() {
post('done');
});
module.exports = test;
var test = require('./tape-wrapper');
test('timing test', function(t) {
t.plan(2);
t.equal(typeof Date.now, 'function');
var start = Date.now();
setTimeout(function () {
t.equal(Date.now() - start, 100);
}, 100);
});
test('webgl support', function(t) {
t.plan(1);
var gl, canvas;
try {
canvas = document.createElement('canvas');
gl = canvas.getContext('webgl');
}
catch(err) {
gl = null;
}
var hasSupport = !!gl;
t.equal(hasSupport, true);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment