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

View File

@ -3,12 +3,13 @@ MOCHA_CMD = node_modules/mocha/bin/_mocha
export NODE_ENV = test
.PHONY: clean test test-cov test-travis publish bench build
.PHONY: clean test test-cov test-travis test-browser publish bench build
clean:
rm -rf coverage templates.json test/tmp dist
test:
rm -rf test/tmp
$(MOCHA_CMD)
rm -rf test/tmp
@ -24,9 +25,13 @@ test-travis:
node $(ISTANBUL_CMD) $(MOCHA_CMD) --report lcovonly -- --reporter spec
if test -n "$$CODECLIMATE_REPO_TOKEN"; then codeclimate < coverage/lcov.info; fi
test-browser:
make build
node bin/generate-browser-test >dist/6to5-test.js
open test/browser/index.html
build:
rm -rf dist
mkdir dist
mkdir -p dist
node bin/cache-templates

View File

@ -172,6 +172,10 @@ global `to5`.
to5("class Test {}").code;
```
To test 6to5 in your browser run:
$ make test-browser
## Modules
6to5 modules compile straight to CommonJS, because of this various liberties are

5
bin/generate-browser-test Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env node
var helper = require("../test/_helper");
console.log("(" + helper.run + ")(" + JSON.stringify(helper.getTests(), null, " ") + ", to5, proclaim)");

View File

@ -59,6 +59,7 @@
"es6now": "0.8.11",
"jstransform": "^6.3.2",
"uglify-js": "^2.4.15",
"browserify": "^6.0.3"
"browserify": "^6.0.3",
"proclaim": "^2.0.0"
}
}

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);