Skip to content

Instantly share code, notes, and snippets.

@ekryski
Last active July 25, 2018 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ekryski/4625734 to your computer and use it in GitHub Desktop.
Save ekryski/4625734 to your computer and use it in GitHub Desktop.
Testing code client side with Mocha + Chai + RequireJS (to keep it clean)
require.config({
shim: {
'underscore': {
exports: '_'
},
'less': {
exports: 'LESS'
}
'mocha': {
exports: 'mocha'
}
},
baseUrl: '../../js/',
paths: {
// Test Libs
// ------------------------------
'chai': '../test/resources/chai',
'mocha': '../test/resources/mocha',
// Tests
// ------------------------------
'test': '../test',
// Libs
// ------------------------------
'jquery': 'lib/jquery/jquery-1.8.2.min',
'underscore': 'lib/utils/lodash-0.9.1.min',
'less': 'lib/utils/less-1.3.1.min',
'bootstrap-affix': 'lib/bootstrap/bootstrap-affix',
'bootstrap-alert': 'lib/bootstrap/bootstrap-alert',
'bootstrap-button': 'lib/bootstrap/bootstrap-button',
'bootstrap-carousel': 'lib/bootstrap/bootstrap-carousel',
'bootstrap-collapse': 'lib/bootstrap/bootstrap-collapse',
'bootstrap-dropdown': 'lib/bootstrap/bootstrap-dropdown',
'bootstrap-modal': 'lib/bootstrap/bootstrap-modal',
'bootstrap-popover': 'lib/bootstrap/bootstrap-popover',
'bootstrap-scrollspy': 'lib/bootstrap/bootstrap-scrollspy',
'bootstrap-tab': 'lib/bootstrap/bootstrap-tab',
'bootstrap-tooltip': 'lib/bootstrap/bootstrap-tooltip',
'bootstrap-transition': 'lib/bootstrap/bootstrap-transition',
'bootstrap-typeahead': 'lib/bootstrap/bootstrap-typeahead',
'can': 'lib/canjs/can',
'text': 'lib/require/text-2.0.3'
// App
// ------------------------------
},
priority: [
'jquery',
'underscore'
]
});
mocha.setup({
ui: 'bdd',
ignoreLeaks: true
});
// Don't track
window.expect = chai.expect;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Foo Tests</title>
<link rel="stylesheet" href="./resources/mocha.css">
</head>
<body>
<div id="mocha"></div>
<script src="./resources/mocha.js" type="text/javascript"></script>
<script src="./resources/chai.js" type="text/javascript"></script>
<script src="./resources/require-2.1.1.min.js" type="text/javascript"></script>
<script src="./resources/config.js" type="text/javascript"></script>
<script type="text/javascript">
require(['test/foo.test'], function(){
mocha.run();
});
</script>
</body>
</html>
var expect = chai.expect;
describe("Foo", function () {
it("Should add correctly", function () {
expect(2+2).to.equal(4);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment