Skip to content

Instantly share code, notes, and snippets.

@FND
Created September 27, 2012 21:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FND/3796438 to your computer and use it in GitHub Desktop.
Save FND/3796438 to your computer and use it in GitHub Desktop.
template for JavaScript components
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sample Test Suite</title>
<link rel="stylesheet" href="../lib/qunit.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="../lib/qunit.js"></script>
<script src="../lib/blanket.js"></script>
<script src="../src/sample.js" data-cover></script>
<script src="tests.js"></script>
</body>
</html>
.PHONY: release test lint dependencies
jquery_version = 1.8
qunit_version = 1.10.0
download = \
curl --output $(1) --time-cond $(1) --remote-time $(2)
# TODO: optional checksum verification
release: lint test
scp # TODO
test:
phantomjs lib/phantomjs-qunit-runner.js test/index.html
lint:
jslint-reporter `find {src,test} -type f -name "*.js"`
dependencies:
mkdir -p lib
$(call download, "lib/jquery.js", \
"http://ajax.googleapis.com/ajax/libs/jquery/$(jquery_version)/jquery.min.js")
$(call download, "lib/qunit.js", \
"http://code.jquery.com/qunit/qunit-$(qunit_version).js")
$(call download, "lib/qunit.css", \
"http://code.jquery.com/qunit/qunit-$(qunit_version).css")
$(call download, "lib/blanket.js", \
"https://raw.github.com/alex-seville/blanket/master/dist/qunit/blanket.min.js")
$(call download, "lib/phantomjs-qunit-runner.js", \
"https://raw.github.com/jquery/qunit/master/addons/phantomjs/runner.js")
/*jslint vars: true, white: true */
var SAMPLE = (function() {
"use strict";
var foo = "lorem";
var bar = "ipsum";
return {
foo: foo,
bar: bar
};
}());
/*jslint vars: true, white: true */
/*global module, test, strictEqual */
/*global SAMPLE */
(function() {
"use strict";
module("Hello World");
test("lipsum", function() {
strictEqual(SAMPLE.foo, "lorem");
strictEqual(SAMPLE.bar, "ipsum");
strictEqual(SAMPLE.baz, "...");
});
}());
@FND
Copy link
Author

FND commented Oct 2, 2012

caveats:

  • assumes jslint-reporter[1][2] - there might be better packaged alternatives ("there are many like it, but this one is mine")
  • assumes phantomjs is present
  • curl only works for simple libraries - perhaps NPM/Bower/Jam/... should be used to retrieve depenencies (and store them in ./lib)
  • no minification yet
  • packaging for frameworks should happen via scripts, e.g. ./dist/{rails,flask,...}
  • common functionality should eventually be captured in a script (→ iqroca {lint,upload})

[1] https://github.com/FND/jslint-reporter
[2] https://github.com/FND/homedir/blob/master/bin/jslint-reporter

@FND
Copy link
Author

FND commented Jul 31, 2013

since I cannot push due to gists no longer supporting directories:

diff --git a/Makefile b/Makefile

--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,10 @@ download = \
    curl --output $(1) --time-cond $(1) --remote-time $(2)

 test:
-   phantomjs lib/phantomjs-qunit-runner.js test/index.html
+   @set -o pipefail && \
+           phantomjs lib/phantomjs-qunit-runner.js test/index.html | \
+           sed -e "s/[^0 ][^0 ]* failed/\x1b[31m&\x1b[0m/" \
+                   -e  "s/Failed assertion: expected: \(.*\), but was: \(.*\)/\n    assertion failed\n    \x1b[31;1mexpected: \1\x1b[0m\n    \x1b[32;1mactual  : \2\x1b[0m/"

 lint:
    jslint-reporter `find {scripts,test} -type f -name "*.js"`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment