worker.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. let bluebird = require('bluebird');
  3. let Worker = require('tiny-worker');
  4. let utility = require('../utility');
  5. let glob = bluebird.promisify(require('glob'));
  6. describe('web worker', function() {
  7. before(function(done) {
  8. // Will match both `highlight.pack.js` and `highlight.min.js`
  9. const filepath = utility.buildPath('..', 'build', 'highlight.*.js');
  10. return glob(filepath).then(hljsPath => {
  11. this.worker = new Worker(function() {
  12. self.onmessage = function(event) {
  13. if (event.data.action === 'importScript') {
  14. importScripts(event.data.script);
  15. postMessage(1);
  16. } else {
  17. var result = self.hljs.highlight('javascript', event.data);
  18. postMessage(result.value);
  19. }
  20. };
  21. });
  22. this.worker.onmessage = () => done();
  23. this.worker.postMessage({
  24. action: 'importScript',
  25. script: hljsPath[0]
  26. });
  27. });
  28. });
  29. it('should highlight text', function(done) {
  30. this.worker.onmessage = event => {
  31. const actual = event.data;
  32. actual.should.equal(this.expect);
  33. done();
  34. };
  35. this.worker.postMessage(this.text);
  36. });
  37. after(function() {
  38. this.worker.terminate();
  39. });
  40. });