Skip to content

Commit 01e0f70

Browse files
Refactored tests to use Bower
1 parent 1312f5d commit 01e0f70

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+22980
-145
lines changed

.bowerrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"directory": "www/bower_components"
3+
}

bower.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
{
22
"name": "ono",
33
"description": "Throw better errors.",
4-
"keywords": [],
4+
"keywords": [
5+
"throw",
6+
"error",
7+
"errors",
8+
"exception",
9+
"printf",
10+
"format",
11+
"wrap",
12+
"inner"
13+
],
514
"authors": [
615
{
716
"name": "James Messinger",
8-
"homepage": "https://github.com/BigstickCarpet"
17+
"homepage": "http://jamesmessinger.com"
918
}
1019
],
1120
"license": "MIT",
@@ -21,5 +30,11 @@
2130
"/lib",
2231
"/.*"
2332
],
24-
"dependencies": {}
33+
"dependencies": {},
34+
"devDependencies": {
35+
"chai": "*",
36+
"mocha": "*",
37+
"sinon-js": "*",
38+
"useragent-parser": "*"
39+
}
2540
}

tests/_config.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* This script configures Mocha, Chai, Sinon, etc.
3+
* It also exposes everything as globals, to allow tests to run in Node and in browsers.
4+
*
5+
* Why not use Browserify instead of globals?
6+
* - To make sure Ono works properly when Node and CommonJS are not available
7+
* - Some of our devDependencies have separate packages packages for Node vs. Browser (e.g. Mocha, Sinon)
8+
* - This reduces redundant boilerplate code in the .spec files
9+
*/
10+
(function() {
11+
'use strict';
12+
13+
if (typeof(window) === 'object') {
14+
// Configure Mocha
15+
mocha.setup('bdd');
16+
mocha.fullTrace();
17+
mocha.checkLeaks();
18+
mocha.globals([]);
19+
20+
// Expose Browser globals
21+
window.global = window;
22+
window.expect = chai.expect;
23+
window.userAgent = userAgentParser.parse(navigator.userAgent);
24+
window.userAgent.isNode = false;
25+
window.userAgent.isBrowser = true;
26+
}
27+
else {
28+
// Expose Node globals
29+
global.ono = require('../');
30+
global.expect = require('chai').expect;
31+
global.sinon = require('sinon');
32+
33+
global.userAgent = {
34+
isNode: true,
35+
isBrowser: false
36+
}
37+
}
38+
39+
// Set global settings for all tests
40+
beforeEach(function() {
41+
this.currentTest.timeout(2000);
42+
this.currentTest.slow(100);
43+
});
44+
45+
})();

tests/_utils.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
(function() {
2+
'use strict';
3+
4+
/**
5+
* Utility methods for use in tests
6+
*/
7+
global.utils = {
8+
forEachMethod: forEachMethod,
9+
matchesJSON: matchesJSON
10+
};
11+
12+
/**
13+
* Invokes the given function for each {@link Ono} method
14+
*
15+
* @param {function} fn - The function that's invoked for each method
16+
*/
17+
function forEachMethod(fn) {
18+
var names = ['', 'error', 'eval', 'range', 'reference', 'syntax', 'type', 'uri'];
19+
var types = ['Error', 'Error', 'EvalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError'];
20+
21+
for (var i = 0; i < names.length; i++) {
22+
var name, method;
23+
24+
if (i === 0) {
25+
name = 'ono()';
26+
method = ono;
27+
}
28+
else {
29+
name = 'ono.' + names[i] + '()';
30+
method = ono[names[i]];
31+
}
32+
33+
var type = types[i];
34+
fn(name, method, global[type], type);
35+
}
36+
}
37+
38+
/**
39+
* Asserts that a JSON-serialized Error has the expected properties & values.
40+
*
41+
* @param {object} expected - The expected properties & values
42+
* @returns {function}
43+
*/
44+
function matchesJSON(expected) {
45+
return function(json) {
46+
if (userAgent.isFirefox) {
47+
expect(json.fileName).to.be.a('string').and.not.empty;
48+
expect(json.lineNumber).to.be.a('number').above(0);
49+
expect(json.columnNumber).to.be.a('number').above(0);
50+
expected.fileName = json.fileName;
51+
expected.lineNumber = json.lineNumber;
52+
expected.columnNumber = json.columnNumber;
53+
}
54+
55+
if (userAgent.isIE && 'description' in json) {
56+
expect(json.description).to.be.a('string');
57+
expected.description = json.description;
58+
}
59+
60+
if (!('stack' in json)) {
61+
// Some browsers don't support the "stack" property
62+
delete expected.stack;
63+
}
64+
65+
expect(json).to.deep.equal(expected);
66+
return true;
67+
}
68+
}
69+
})();
70+

tests/formatter.spec.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
'use strict';
2-
3-
var helper = require('./helper'),
4-
expect = require('chai').expect,
5-
ono = require('../');
6-
71
describe('ono.formatter', function() {
8-
var originalFormatter = ono.formatter;
2+
'use strict';
3+
4+
var originalFormatter;
95

106
before(function() {
7+
originalFormatter = ono.formatter;
8+
119
// A simple formatter that replaces $0, $1, $2, etc. with the corresponding param
1210
ono.formatter = function(message) {
1311
var params = Array.prototype.slice.call(arguments, 1);
@@ -30,7 +28,7 @@ describe('ono.formatter', function() {
3028
expect(err.message).to.equal('4 must be greater than 10');
3129

3230
var json = JSON.parse(JSON.stringify(err));
33-
expect(json).to.satisfy(helper.matchesJSON({
31+
expect(json).to.satisfy(utils.matchesJSON({
3432
name: err.name,
3533
message: err.message,
3634
stack: err.stack
@@ -47,7 +45,7 @@ describe('ono.formatter', function() {
4745
expect(err.message).to.equal('4 must be greater than 10');
4846

4947
var json = JSON.parse(JSON.stringify(err));
50-
expect(json).to.satisfy(helper.matchesJSON({
48+
expect(json).to.satisfy(utils.matchesJSON({
5149
name: err.name,
5250
message: err.message,
5351
stack: err.stack

tests/helper.js

Lines changed: 0 additions & 99 deletions
This file was deleted.

tests/index.html

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
<!DOCTYPE html>
22
<html>
33
<head lang="en">
4-
<meta charset="UTF-8">
5-
<link rel="stylesheet" href="../node_modules/mocha/mocha.css"/>
4+
<meta charset='utf-8'>
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
67

78
<title>Ono Tests</title>
9+
10+
<link rel="stylesheet" href="../www/bower_components/mocha/mocha.css"/>
811
</head>
912
<body>
1013
<div id="mocha"></div>
1114

12-
<!-- Unit Testing Frameworks -->
13-
<script src="../node_modules/mocha/mocha.js"></script>
14-
<script src="../node_modules/chai/chai.js"></script>
15-
<script src="../node_modules/sinon/pkg/sinon.js"></script>
16-
<script>mocha.setup('bdd')</script>
15+
<!-- Third-Party Libraries -->
16+
<script src="../www/bower_components/mocha/mocha.js"></script>
17+
<script src="../www/bower_components/chai/chai.js"></script>
18+
<script src="../www/bower_components/sinon-js/sinon.js"></script>
19+
<script src="../www/bower_components/useragent-parser/src/useragent-parser.js"></script>
1720

1821
<!-- Ono -->
1922
<script src="../dist/ono.js"></script>
2023

2124
<!-- Unit Tests -->
22-
<script src="helper.js"></script>
25+
<script src="_config.js"></script>
26+
<script src="_utils.js"></script>
2327
<script src="ono.spec.js"></script>
2428
<script src="formatter.spec.js"></script>
2529

2630
<script>
27-
expect = chai.expect;
28-
mocha.checkLeaks();
29-
mocha.globals([]);
3031
mocha.run();
3132
</script>
3233
</body>

0 commit comments

Comments
 (0)