Skip to content

Instantly share code, notes, and snippets.

@toddself
Last active December 18, 2015 18:29
Show Gist options
  • Save toddself/5825886 to your computer and use it in GitHub Desktop.
Save toddself/5825886 to your computer and use it in GitHub Desktop.
/*
* barnstormer
* https://github.com/condenast/barnstormer
*
* Copyright (c) 2013 Condé Nast. All rights reserved.
*/
var sync;
if (typeof define !== 'function') {
var define = require('amdefine')(module);
sync = require('../../lib/mongo_sync');
}
define(function (require) {
'use strict';
var Backbone = require('backbone');
if(typeof sync !== 'undefined'){
Backbone.sync = sync;
}
var NewsletterStory = Backbone.Model.extend({
url: '/parse',
idAttribute: '_id',
defaults: {
hed: '',
dek: '',
trunc_hed: '',
img: '',
url: '',
section: '',
expand: false,
name: ''
}
});
return NewsletterStory;
});
/* global requirejs */
requirejs.config({
baseUrl: '../',
hbs: {
disableI18n: true,
helperPathCallback: function (name) {
'use strict';
return 'views/helpers/' + name;
}
},
paths: {
'jquery': 'components/jquery/jquery',
'underscore': 'components/underscore/underscore',
'backbone': 'components/backbone/backbone',
'handlebars': 'components/handlebars/handlebars',
'hbs': 'components/require-handlebars-plugin/hbs',
'i18nprecompile': 'components/require-handlebars-plugin/hbs/i18nprecompile',
'json2': 'components/require-handlebars-plugin/hbs/json2',
'moment': 'components/moment/moment',
'bootstrap': 'components/bootstrap/js/bootstrap'
},
shim: {
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
'handlebars': {
exports: 'Handlebars'
},
'underscore': {
exports: '_'
},
'bootstrap': {
deps: ['jquery'],
exports: 'jQuery.fn.modal'
}
}
});
requirejs.config({
baseUrl: window.PHANTOMJS ? '../' : '/../',
});
@toddself
Copy link
Author

So grunt mocha provides a window object, so if you block code from running on the front end with typeof window === 'undefined' that doesn't work in your test suite. For some reason wrapping your code in:

if(typeof window === 'undefined'){ 
    Backbone.sync = require('../../lib/mongo_sync'); 
}

Would cause a script error with require when trying to import it. (Seriously.)

This is the only pattern I've gotten working yet to make code run only in a particular environment and still have the test suite work...

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