lotte
lotte copied to clipboard
Add support for 'require'
require() does not work in lotte tests. It would be awesome if we could require() our own modules though. In my tests, I usually startup my web server right in the test code, then inject dependencies in the web server and then I mock the desired behavior according to each test.
For example:
@base 'http://localhost:10000'
@title 'Foo bar'
{WebServer} = require '../webserver'
class EmptyApp
_searchResult: []
searchStuff: (terms) ->
return _searchResult;
app = new EmptyApp
new WebServer(10000, app).start =>
@open '/', ->
@describe 'Search test', ->
app._searchResult = ['foo', 'bar']
# do a search, submit the form, assert things, etc
@success()
I have been using a custom Lottefile function for a while now which does Sprocket-style requires. I haven't been able to make it work inside PhantomJS but happy to pull any patches :smile:
// [..]
on('compile', function(args, options, resume) {
'use strict';
args.code = args.code.replace(/^\s*\/\/=\s*require\s+(["<])([^">]+).$/mg, function(match, type, location) {
var directory;
// Absolute require from current working directory.
if (type === '<') {
directory = '[require("fs").workingDirectory]';
// Relative require from current executing file.
} else {
// ${FILE} is a special marker which is substituted with the absolute
// path to the current executing file. By splitting its path components
// and removing the last one, we retrieve only the directory.
directory = '${FILE:encode}.split(/[\\/\\\\]/).slice(0, -1)';
}
return 'phantom.injectJs(' + directory + '.concat(' + JSON.stringify((location.replace(/\.js$/, '') + '.js').split(/[\/\\]/)) + ').join(require("fs").separator));';
});
resume();
});
And to use:
//=require <scripts/fileA>
//=require <scripts/fileB>
this.open('..', function() {
'use strict';
// [..]