Skip to content

Commit

Permalink
test: add sinon.js for spies, deprecate current assertions
Browse files Browse the repository at this point in the history
- the goal is to get overall cleaner test.
- chai and sinon are standard libraries for testing
  assertions and spies. We should not use custom ones.
  • Loading branch information
nknapp committed Nov 15, 2019
1 parent 93e284e commit 93516a0
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 13 deletions.
120 changes: 119 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"mock-stdin": "^0.3.0",
"mustache": "^2.1.3",
"semver": "^5.0.1",
"sinon": "^7.5.0",
"typescript": "^3.4.3",
"underscore": "^1.5.1",
"webpack": "^1.12.6",
Expand Down
9 changes: 6 additions & 3 deletions spec/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@
"stop": true,
"ok": true,
"strictEqual": true,
"define": true
"define": true,
"expect": true,
"chai": true
},
"env": {
"mocha": true
},
"rules": {
// Disabling for tests, for now.
"no-path-concat": 0,
"no-path-concat": "off",

"no-var": 0
"no-var": "off",
"dot-notation": "off"
}
}
5 changes: 4 additions & 1 deletion spec/amd-runtime.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
document.documentElement.className = 'headless';
}
</script>
<script src="/node_modules/sinon/pkg/sinon.js"></script>
<script src="/node_modules/chai/chai.js"></script>
<script src="/node_modules/dirty-chai/lib/dirty-chai.js"></script>
<script src="/node_modules/mocha/mocha.js"></script>
<script>
window.expect = chai.expect;
mocha.setup('bdd');
</script>

<script src="/spec/env/json2.js"></script>
<script src="/spec/env/require.js"></script>

Expand Down
5 changes: 4 additions & 1 deletion spec/amd.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
document.documentElement.className = 'headless';
}
</script>
<script src="/node_modules/sinon/pkg/sinon.js"></script>
<script src="/node_modules/chai/chai.js"></script>
<script src="/node_modules/dirty-chai/lib/dirty-chai.js"></script>
<script src="/node_modules/mocha/mocha.js"></script>
<script>
window.expect = chai.expect;
mocha.setup('bdd');
</script>

<script src="/spec/env/json2.js"></script>
<script src="/spec/env/require.js"></script>

Expand Down
9 changes: 9 additions & 0 deletions spec/env/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ require('./common');
var fs = require('fs'),
vm = require('vm');

var chai = require('chai');
var dirtyChai = require('dirty-chai');


chai.use(dirtyChai);
global.expect = chai.expect;

global.sinon = require('sinon');

global.Handlebars = 'no-conflict';

var filename = 'dist/handlebars.js';
Expand Down
8 changes: 8 additions & 0 deletions spec/env/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,20 @@ global.compileWithPartials = function(string, hashOrArray, partials) {
};


/**
* @deprecated Use chai's expect-style API instead (`expect(actualValue).to.equal(expectedValue)`)
* @see https://www.chaijs.com/api/bdd/
*/
global.equals = global.equal = function equals(a, b, msg) {
if (a !== b) {
throw new AssertError("'" + a + "' should === '" + b + "'" + (msg ? ': ' + msg : ''), equals);
}
};

/**
* @deprecated Use chai's expect-style API instead (`expect(actualValue).to.equal(expectedValue)`)
* @see https://www.chaijs.com/api/bdd/#method_throw
*/
global.shouldThrow = function(callback, type, msg) {
var failed;
try {
Expand Down
3 changes: 3 additions & 0 deletions spec/env/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ require('./common');
var chai = require('chai');
var dirtyChai = require('dirty-chai');


chai.use(dirtyChai);
global.expect = chai.expect;

global.sinon = require('sinon');

global.Handlebars = require('../../lib');

global.CompilerContext = {
Expand Down
5 changes: 3 additions & 2 deletions spec/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
document.documentElement.className = 'headless';
}
</script>
<script src="/node_modules/mocha/mocha.js"></script>
<script src="/node_modules/sinon/pkg/sinon.js"></script>
<script src="/node_modules/chai/chai.js"></script>
<script src="/node_modules/dirty-chai/lib/dirty-chai.js"></script>
<script src="/node_modules/mocha/mocha.js"></script>
<script>
window.expect = chai.expect;
mocha.setup('bdd');
</script>
<script src="/dist/handlebars.js"></script>

<script src="/spec/env/json2.js"></script>
<script src="/spec/env/common.js"></script>
<script>
Expand Down
6 changes: 3 additions & 3 deletions spec/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ describe('security issues', function() {
});
it('should throw an exception when calling "{{blockHelperMissing "abc" .}}" ', function() {
var functionCalls = [];
shouldThrow(function() {
expect(function() {
var template = Handlebars.compile('{{blockHelperMissing "abc" .}}');
template({ fn: function() { functionCalls.push('called'); }});
}, Error);
equals(functionCalls.length, 0);
}).to.throw(Error);
expect(functionCalls.length).to.equal(0);
});
it('should throw an exception when calling "{{#blockHelperMissing .}}{{/blockHelperMissing}}"', function() {
shouldThrow(function() {
Expand Down
5 changes: 4 additions & 1 deletion spec/umd-runtime.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
document.documentElement.className = 'headless';
}
</script>
<script src="/node_modules/sinon/pkg/sinon.js"></script>
<script src="/node_modules/chai/chai.js"></script>
<script src="/node_modules/dirty-chai/lib/dirty-chai.js"></script>
<script src="/node_modules/mocha/mocha.js"></script>
<script>
window.expect = chai.expect;
mocha.setup('bdd');
</script>

<script src="/spec/env/json2.js"></script>
<script src="/spec/env/require.js"></script>

Expand Down
6 changes: 5 additions & 1 deletion spec/umd.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
document.documentElement.className = 'headless';
}
</script>

<script src="/node_modules/sinon/pkg/sinon.js"></script>
<script src="/node_modules/chai/chai.js"></script>
<script src="/node_modules/dirty-chai/lib/dirty-chai.js"></script>
<script src="/node_modules/mocha/mocha.js"></script>
<script>
window.expect = chai.expect;
mocha.setup('bdd');
</script>

<script src="/spec/env/json2.js"></script>
<script src="/spec/env/require.js"></script>

Expand Down

0 comments on commit 93516a0

Please sign in to comment.