Created
February 13, 2015 10:38
-
-
Save sapegin/420029a1c24138c15c4f to your computer and use it in GitHub Desktop.
Mocha + Require.js + Atomus (jsdom wrapper)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Author: Artem Sapegin http://sapegin.me, 2015 | |
(function() { | |
var config = { | |
paths: { | |
lodash: '../bower_components/lodash/lodash', | |
jquery: '../bower_components/jquery/dist/jquery', | |
backbone: '../bower_components/backbone/backbone', | |
react: '../bower_components/react/react', | |
backboneLocalStorage: '../bower_components/backbone.localStorage/backbone.localStorage', | |
reactBackbone: '../bower_components/react.backbone/react.backbone', | |
moment: '../bower_components/moment/moment' | |
}, | |
map: { | |
'*': { | |
underscore: 'lodash' | |
} | |
}, | |
deps: [ | |
// Mixins and plugins | |
'backboneLocalStorage', | |
'reactBackbone', | |
'util/react-extras', | |
// App | |
'main' | |
] | |
}; | |
if (typeof window !== 'undefined') { | |
// Set up Require.js in browser | |
require.config(config); | |
} | |
else { | |
// Expose config as a Node.js module | |
module.exports = config; | |
} | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Mocha environment setup | |
var path = require('path'); | |
var atomus = require('atomus'); | |
var ROOT = path.join(__dirname, '../'); | |
var SRC = path.join(ROOT, 'app'); | |
var APP_MAIN = 'main'; | |
/** | |
* Make Chai’s expect available for all tests. | |
*/ | |
global.expect = require('chai').expect; | |
/** | |
* Creates jsdom browser, loads module via Require.js and register it as a global function. | |
* Exposes jsdom’s browser as `global.browser`. | |
* | |
* @param {String} name Name of Require.js module: will be available as `global.module()`. | |
* @param {Function} done | |
*/ | |
global.requireModule = function(name, done) { | |
var requireJs = path.join(ROOT, 'bower_components/requirejs/require.js'); | |
var baseName = path.basename(name); | |
var requireConfig = require(path.join(SRC, 'config.js')); | |
requireConfig.baseUrl = SRC; | |
requireConfig.paths.jquery = path.join(ROOT, 'node_modules/atomus/lib/vendor/jquery-1.11.1.min.js'); | |
requireConfig.deps.splice(requireConfig.deps.indexOf(APP_MAIN)); // Do not require whole app | |
atomus() | |
.html( | |
'<!doctype html>' + | |
'<script src="' + requireJs + '"></script>' + | |
'<script>requirejs.config(' + JSON.stringify(requireConfig) + ')</script>' | |
) | |
.ready(function(errors, window) { | |
global.browser = this; | |
window.requirejs.onError = function(err) { | |
console.log('Require.js can not require', err.requireModules); | |
console.log(err.originalError.error); | |
throw err; | |
}; | |
window.requirejs([name], function(module) { | |
global[baseName] = module; | |
done(null, module); | |
}); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
describe 'models/project', -> | |
before (done) -> | |
requireModule 'models/project', done | |
it 'has default values', -> | |
model = new project() | |
expect(model.get('name')).to.equal('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment