Skip to content
This repository was archived by the owner on Mar 26, 2020. It is now read-only.

Add support for 'require' #8

Open
ccidral opened this issue Apr 25, 2013 · 1 comment
Open

Add support for 'require' #8

ccidral opened this issue Apr 25, 2013 · 1 comment

Comments

@ccidral
Copy link

ccidral commented Apr 25, 2013

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()
@StanAngeloff
Copy link
Owner

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 😄

// [..]

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';
  // [..]

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants