add browser testing #17

This commit is contained in:
Sebastian McKenzie
2014-10-09 16:21:34 +11:00
parent 47b1512f3b
commit cb9cece2f8
7 changed files with 132 additions and 73 deletions

90
test/_helper.js Normal file
View File

@@ -0,0 +1,90 @@
var transform = require("../lib/6to5/transform");
var fs = require("fs");
var _ = require("lodash");
var fixturesDir = __dirname + "/fixtures";
var humanise = function (val) {
return val.replace(/-/g, " ");
};
var readFile = function (filename) {
if (fs.existsSync(filename)) {
return fs.readFileSync(filename, "utf8");
} else {
return "";
}
};
exports.run = function (suites, transform, assert) {
_.each(suites, function (testSuite) {
suite(testSuite.title, function () {
_.each(testSuite.tests, function (task) {
test(task.title, function () {
var run = function () {
transform.test(task.actual, task.expect, task.options);
};
var throwMsg = task.options.throws;
if (throwMsg) {
// internal api doesn't have this option but it's best not to pollute
// the options object with useless options
delete task.options.throws;
assert.throws(run, new RegExp(throwMsg));
} else {
run();
}
});
});
});
});
};
exports.getTests = function () {
var suites = [];
_.each(fs.readdirSync(fixturesDir), function (suiteName) {
if (suiteName[0] === ".") return;
var suite = {
options: {},
tests: [],
title: humanise(suiteName),
filename: fixturesDir + "/" + suiteName
};
suites.push(suite);
var suiteOptsLoc = suite.filename + "/options.json";
if (fs.existsSync(suiteOptsLoc)) suite.options = require(suiteOptsLoc);
_.each(fs.readdirSync(suite.filename), function (taskName) {
if (taskName[0] === ".") return;
var taskDir = suite.filename + "/" + taskName;
if (fs.statSync(taskDir).isFile()) return;
var actualLoc = taskDir + "/actual.js";
var expectLoc = taskDir + "/expected.js";
var taskOptsLoc = taskDir + "/options.json";
var taskOpts = _.merge({ filename: actualLoc }, _.cloneDeep(suite.options));
if (fs.existsSync(taskOptsLoc)) _.merge(taskOpts, require(taskOptsLoc));
suite.tests.push({
title: humanise(taskName),
options: taskOpts,
actual: {
code: readFile(actualLoc),
filename: actualLoc,
},
expect: {
code: readFile(expectLoc),
filename: expectLoc
}
});
});
});
return suites;
};

20
test/browser/index.html Normal file
View File

@@ -0,0 +1,20 @@
<html>
<head>
<meta charset="utf-8">
<title>Mocha</title>
<link rel="stylesheet" href="../../node_modules/mocha/mocha.css" />
</head>
<body>
<div id="mocha"></div>
<script src="../../node_modules/proclaim/lib/proclaim.js"></script>
<script src="../../node_modules/lodash/lodash.js"></script>
<script src="../../node_modules/mocha/mocha.js"></script>
<script src="../../dist/6to5.js"></script>
<script>mocha.setup("tdd");</script>
<script src="../../dist/6to5-test.js"></script>
<script>
mocha.checkLeaks();
mocha.run();
</script>
</body>
</html>

View File

@@ -1,70 +1,4 @@
var transform = require("../lib/6to5/transform");
var assert = require("assert");
var fs = require("fs");
var _ = require("lodash");
var helper = require("./_helper");
var assert = require("assert");
var humanise = function (val) {
return val.replace(/-/g, " ");
};
var readFile = function (filename) {
if (fs.existsSync(filename)) {
return fs.readFileSync(filename, "utf8");
} else {
return "";
}
};
var fixturesDir = __dirname + "/fixtures";
_.each(fs.readdirSync(fixturesDir), function (suiteName) {
if (suiteName[0] === ".") return;
var suiteDir = fixturesDir + "/" + suiteName;
var suiteOptsLoc = suiteDir + "/options.json";
var suiteOpts = {};
if (fs.existsSync(suiteOptsLoc)) suiteOpts = require(suiteOptsLoc);
suite(humanise(suiteName), function () {
_.each(fs.readdirSync(suiteDir), function (taskName) {
if (taskName[0] === ".") return;
var taskDir = suiteDir + "/" + taskName;
if (fs.statSync(taskDir).isFile()) return;
var actualLoc = taskDir + "/actual.js";
var expectLoc = taskDir + "/expected.js";
var taskOptsLoc = taskDir + "/options.json";
var taskOpts = _.merge({ filename: actualLoc }, _.cloneDeep(suiteOpts));
if (fs.existsSync(taskOptsLoc)) _.merge(taskOpts, require(taskOptsLoc));
test(humanise(taskName), function () {
var actual = readFile(actualLoc);
var expect = readFile(expectLoc);
var test = function () {
transform.test({
filename: actualLoc,
code: actual
}, {
filename: expectLoc,
code: expect
}, taskOpts);
};
var throwMsg = taskOpts.throws;
if (throwMsg) {
// internal api doesn't have this option but it's best not to pollute
// the options object with useless options
delete taskOpts.throws;
assert.throws(test, new RegExp(throwMsg));
} else {
test();
}
});
});
});
});
helper.run(helper.getTests(), transform, assert);