'use strict';

var Worker  = require('tiny-worker');
var utility = require('../utility');
var glob    = require('glob');

describe('in worker', function() {
  before(function(done) {
    // Will match both `highlight.pack.js` and `highlight.min.js`
    var hljsPath = glob.sync(utility.buildPath('..', 'build', 'highlight.*.js'));

    this.worker = new Worker(function () {
      self.onmessage = function (event) {
        if (event.data.action === 'importScript') {
          importScripts(event.data.script);
          postMessage(1);
        } else {
          var result = self.hljs.highlightAuto(event.data);
          postMessage(result.value);
        }
      };
    });

    this.worker.onmessage = function () {
      done();
    };

    this.worker.postMessage({
      action: 'importScript',
      script: hljsPath[0]
    });
  });

  it('should works', function(done) {
    this.worker.onmessage = function (event) {
      var actual = event.data;
      actual.should.equal(
        '<span class="hljs-variable"><span class="hljs-keyword">var</span> say</span> = <span class="hljs-string">"Hello"</span>;' +
        '<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span> </span>{}'
      );
      done();
    };

    this.worker.postMessage(
      'var say = "Hello";' +
      'class Car {}'
    );
  });

  after(function () {
    this.worker.terminate();
  });
});

@@ -8,16 +8,52 @@
 rict';%0A%0A
+var bluebird = require('bluebird');%0A
 var Work
@@ -56,16 +56,17 @@
 Worker  
+ 
 = requir
@@ -95,16 +95,17 @@
 utility 
+ 
 = requir
@@ -133,18 +133,38 @@
 glob    
+ 
 = 
+bluebird.promisify(
 require(
@@ -170,16 +170,17 @@
 ('glob')
+)
 ;%0A%0Adescr
@@ -313,29 +313,19 @@
 var 
-hljsP
+filep
 ath = 
-glob.sync(
 util
@@ -370,20 +370,68 @@
 t.*.js')
-)
 ;%0A%0A
+    return glob(filepath).then((hljsPath) =%3E %7B%0A  
     this
@@ -461,24 +461,26 @@
 nction () %7B%0A
+  
       self.o
@@ -517,16 +517,18 @@
         
+  
 if (even
@@ -573,16 +573,18 @@
         
+ 
+ 
 importSc
@@ -611,32 +611,34 @@
 ipt);%0A          
+  
 postMessage(1);%0A
@@ -637,16 +637,18 @@
 age(1);%0A
+  
         
@@ -662,24 +662,26 @@
 %7B%0A          
+  
 var result =
@@ -720,32 +720,34 @@
 ata);%0A          
+  
 postMessage(resu
@@ -769,10 +769,14 @@
     
-%7D%0A
+  %7D%0A  
     
@@ -780,16 +780,18 @@
      %7D;%0A
+  
     %7D);%0A
@@ -791,24 +791,26 @@
    %7D);%0A%0A    
+  
 this.worker.
@@ -837,32 +837,34 @@
  () %7B%0A      
+  
 done();%0A
     %7D;%0A%0A    
@@ -843,32 +843,34 @@
         done();%0A
+  
     %7D;%0A%0A    this
@@ -857,32 +857,34 @@
 ;%0A      %7D;%0A%0A    
+  
 this.worker.post
@@ -899,16 +899,18 @@
 %7B%0A      
+  
 action: 
@@ -931,16 +931,18 @@
 ,%0A      
+ 
+ 
 script: 
@@ -957,20 +957,29 @@
 %5B0%5D%0A    
+  
 %7D);%0A
+    %7D)%0A
   %7D);%0A%0A 

Use a promise for glob