From cb9cece2f83f7161a907daf63fb86d497095caf2 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 9 Oct 2014 16:21:34 +1100 Subject: [PATCH] add browser testing #17 --- Makefile | 11 +++-- README.md | 4 ++ bin/generate-browser-test | 5 +++ package.json | 3 +- test/_helper.js | 90 +++++++++++++++++++++++++++++++++++++++ test/browser/index.html | 20 +++++++++ test/index.js | 72 ++----------------------------- 7 files changed, 132 insertions(+), 73 deletions(-) create mode 100755 bin/generate-browser-test create mode 100644 test/_helper.js create mode 100644 test/browser/index.html diff --git a/Makefile b/Makefile index ad9418da50..30bcb7304b 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index 06b88fd74b..dd4a2dddca 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bin/generate-browser-test b/bin/generate-browser-test new file mode 100755 index 0000000000..6a27d5b2cc --- /dev/null +++ b/bin/generate-browser-test @@ -0,0 +1,5 @@ +#!/usr/bin/env node + +var helper = require("../test/_helper"); + +console.log("(" + helper.run + ")(" + JSON.stringify(helper.getTests(), null, " ") + ", to5, proclaim)"); diff --git a/package.json b/package.json index 825f3ddf8d..624f28aa9b 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/_helper.js b/test/_helper.js new file mode 100644 index 0000000000..d6839fb77d --- /dev/null +++ b/test/_helper.js @@ -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; +}; diff --git a/test/browser/index.html b/test/browser/index.html new file mode 100644 index 0000000000..26164cb54d --- /dev/null +++ b/test/browser/index.html @@ -0,0 +1,20 @@ + + + + Mocha + + + +
+ + + + + + + + + diff --git a/test/index.js b/test/index.js index 49ae53adc1..7757bfa1c7 100644 --- a/test/index.js +++ b/test/index.js @@ -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);