Skip to content

Instantly share code, notes, and snippets.

@shawninder
Created April 15, 2016 15:07
Show Gist options
  • Save shawninder/44c44a66ca302e3f3f5229174bdfa78e to your computer and use it in GitHub Desktop.
Save shawninder/44c44a66ca302e3f3f5229174bdfa78e to your computer and use it in GitHub Desktop.
'use strict'
var Stream = require('stream')
var test = require('tape')
var browserify = require('browserify')
/**
* This plugin waits until browserify is done going through the require tree,
* then adds "SUCCESS!" to the array passed in as `results` in the options object
*/
var myPlugin = function (brow, options) {
brow.on('bundle', function (bundler) {
bundler.on('end', function () {
// Mock some async work
setTimeout(function () {
options.results.push('SUCCESS!')
}, 1000)
})
})
}
test('plugin', function (t) {
console.log('test')
t.plan(1)
var brow = browserify()
var js = "console.log('hello world')"
var fileStub = new Stream.Readable()
fileStub.push(js, 'utf8')
fileStub.push(null)
var pluginResults = []
brow.plugin(myPlugin, {
results: pluginResults
})
brow.add(fileStub)
brow.bundle(function (err) {
if (err) {
t.fail('browserify errored: ' + err)
} else {
// setTimeout(function () {
t.equal(pluginResults.length, 1, "plugin is done doing it's thing")
// }, 1100)
}
})
})
@shawninder
Copy link
Author

If you run this, you'll observe that the test fails. Now notice the setTimeout I left commented out. If you uncomment those 2 lines, the test passes.

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