Skip to content

Commit

Permalink
Add failing test: reverse multi bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
esamattis committed Mar 26, 2013
1 parent 3777c3a commit 214dc35
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
43 changes: 43 additions & 0 deletions test/reverse_multi_bundle.js
@@ -0,0 +1,43 @@
/**
* To be able to lazy load bundles with script loaders the loaded bundles
* must have access to modules exposed by previous bundles.
*
* In effect this is the same as adding the bundles in reverse order
**/

var browserify = require('../');
var vm = require('vm');
var test = require('tap').test;

test('reverse multi bundle', function (t) {
t.plan(4);

// Main app bundle has the main app code and the shared libarary code
var app = browserify([__dirname + '/reverse_multi_bundle/app.js'])
.external(__dirname + '/reverse_multi_bundle/lazy.js')
.require(__dirname + '/reverse_multi_bundle/shared.js');

// Lazily loaded bundle has only its own code even it uses code from the
// shared library.
var lazy = browserify()
.require(__dirname + '/reverse_multi_bundle/lazy.js')
.external(__dirname + '/reverse_multi_bundle/shared.js');


app.bundle(function (err, appSrc) {
if (err) throw err;
lazy.bundle(function(err, lazySrc) {
if (err) throw err;

var src = appSrc + lazySrc;
var c = {
setTimeout: setTimeout,
t: t
};
vm.runInNewContext(src, c);
});

});


});
22 changes: 22 additions & 0 deletions test/reverse_multi_bundle/app.js
@@ -0,0 +1,22 @@

t.equal(
require("./shared")(), 1,
"the main app bundle can already use the shared library"
);

t.throws(function() {
require("./lazy");
}, "lazy bundle is not executed yet so the lazy module cannot be required yet");

// Use setTimeout as script loader simulator as in real use case this would be
// a call to one. Now we just let the rest of the source code string we build
// to execute.
setTimeout(function() {
// After lazy bundle is executed we can require the lazy.js module
require("./lazy");
t.equal(
require("./shared")(),3,
"lazy module was able to use shared code"
);
}, 1);

4 changes: 4 additions & 0 deletions test/reverse_multi_bundle/lazy.js
@@ -0,0 +1,4 @@
t.equal(
require("./shared")(),2,
"lazy.js can use the shared library"
);
4 changes: 4 additions & 0 deletions test/reverse_multi_bundle/shared.js
@@ -0,0 +1,4 @@
var i = 0;
module.exports = function() {
return ++i;
};

0 comments on commit 214dc35

Please sign in to comment.