From c86d856aa0e6eb0489e5e738bf601ceb42c13b41 Mon Sep 17 00:00:00 2001 From: Mateusz Derks Date: Sun, 2 Jul 2017 11:08:14 +0200 Subject: [PATCH] Allow to access browserSync instance from a plugin --- lib/index.js | 5 +++++ test/client-new/index.js | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 3b6b39b..d8b04aa 100644 --- a/lib/index.js +++ b/lib/index.js @@ -29,6 +29,11 @@ exports.init = function (options) { var browserSync = new BrowserSync(options); + // Expose browserSync instance to allow runtime extensions + BS.use = function (cb) { + cb(browserSync); + }; + // Always init on page load ghostMode.init(browserSync); codeSync.init(browserSync); diff --git a/test/client-new/index.js b/test/client-new/index.js index a5c4b0c..f96d7e9 100644 --- a/test/client-new/index.js +++ b/test/client-new/index.js @@ -5,6 +5,7 @@ describe("Init method", function(){ var ghostMode = window.__bs_ghost_mode__; var bs = window.__bs_stub__; var codeSync = window.__bs_code_sync__; + var BrowserSync = window.__bs; var notifySpy; var notifyFlashSpy; var ghostStub; @@ -16,17 +17,31 @@ describe("Init method", function(){ ghostStub = sinon.stub(ghostMode, "init"); codeSyncStub = sinon.stub(codeSync, "init"); }); + beforeEach(function () { + window.___browserSync___ = {}; + }); + afterEach(function (){ + delete window.___browserSync___; + }); after(function () { notifySpy.restore(); notifyFlashSpy.restore(); ghostStub.restore(); codeSyncStub.restore(); }); - it("should initilize", function(){ + it("should initialize", function(){ index.init(bs.options); sinon.assert.called(notifySpy); sinon.assert.called(notifyFlashSpy); sinon.assert.called(ghostStub); sinon.assert.called(codeSyncStub); }); + it("should expose browserSync instance", function(){ + var spy = sinon.spy(); + var BS = window.___browserSync___; + index.init(bs.options); + assert.equal(typeof BS.use === "function", true); + BS.use(spy); + sinon.assert.calledWith(spy, sinon.match.instanceOf(BrowserSync)); + }); }); \ No newline at end of file