|
| 1 | +var webpack = require('webpack'); |
| 2 | +var Promise = require('bluebird'); |
| 3 | +var ps = Promise.promisifyAll(require('ps-node')); |
| 4 | +var fs = Promise.promisifyAll(require('fs')); |
| 5 | +var expect = require('expect'); |
| 6 | + |
| 7 | +var path = require('path'); |
| 8 | +var loader = path.resolve(__dirname, '../dist.babel'); |
| 9 | +var ForkCheckerPlugin = require(loader).ForkCheckerPlugin; |
| 10 | +var globalConfig = { |
| 11 | + watch: true, |
| 12 | + module: { |
| 13 | + loaders: [ |
| 14 | + { |
| 15 | + test: /\.ts?/, |
| 16 | + loader: loader + '?doTypeCheck&+forkChecker', |
| 17 | + }, |
| 18 | + ], |
| 19 | + }, |
| 20 | + plugins: [ |
| 21 | + new ForkCheckerPlugin(), |
| 22 | + ], |
| 23 | +}; |
| 24 | + |
| 25 | +var outputDir = path.resolve(__dirname, './output/'); |
| 26 | + |
| 27 | +describe('checker test', function() { |
| 28 | + var toCheckFilename = './test/fixtures/to-check.ts'; |
| 29 | + |
| 30 | + this.timeout(5000); |
| 31 | + |
| 32 | + var outputFile = path.resolve(outputDir, toCheckFilename); |
| 33 | + var config = { |
| 34 | + output: { |
| 35 | + path: outputDir, |
| 36 | + filename: toCheckFilename, |
| 37 | + }, |
| 38 | + entry: toCheckFilename, |
| 39 | + }; |
| 40 | + var getCheckerRuntimeProcess = () => { |
| 41 | + return ps.lookupAsync({ |
| 42 | + command: new RegExp('node'), |
| 43 | + arguments: new RegExp('checker-runtime'), |
| 44 | + psargs: 'aux' |
| 45 | + }).then(res => { |
| 46 | + return res[0]; |
| 47 | + }); |
| 48 | + }; |
| 49 | + |
| 50 | + var utilizeProcess = (p) => { |
| 51 | + expect(p).toExist(); |
| 52 | + |
| 53 | + return ps.killAsync(p.pid); |
| 54 | + }; |
| 55 | + |
| 56 | + it('should fork checker in separate process', function(done) { |
| 57 | + // assert that checker starts on startup |
| 58 | + new Promise((resolve, reject) => { |
| 59 | + webpack(Object.assign(globalConfig, config), function(err, stat) { |
| 60 | + resolve(); |
| 61 | + }); |
| 62 | + }) |
| 63 | + .then(getCheckerRuntimeProcess) |
| 64 | + .then(p => { |
| 65 | + expect(p).toExist(); |
| 66 | + return utilizeProcess(p); |
| 67 | + }) |
| 68 | + .then(() => { |
| 69 | + done(); |
| 70 | + }) |
| 71 | + }); |
| 72 | + |
| 73 | + it('should fork only one checker after multiple changes', function(done) { |
| 74 | + // I didn't get how to test it more precise, so it's more like a proof of work |
| 75 | + var fileContent = fs.readFileSync(toCheckFilename); |
| 76 | + var updateFile = () => fs.writeFileAsync(toCheckFilename, Date.now()); |
| 77 | + var timeoutAsync = (ms) => { |
| 78 | + return new Promise((resolve, reject) => { |
| 79 | + setTimeout(resolve, ms); |
| 80 | + }); |
| 81 | + }; |
| 82 | + |
| 83 | + new Promise((resolve, reject) => { |
| 84 | + webpack(Object.assign(globalConfig, config), function(err, stat) { |
| 85 | + resolve(); |
| 86 | + }); |
| 87 | + }) |
| 88 | + .then(getCheckerRuntimeProcess) |
| 89 | + .then(p => { |
| 90 | + expect(p).toExist(); |
| 91 | + var times = []; |
| 92 | + var i = 10; |
| 93 | + while(i--) { times.push(i); } |
| 94 | + |
| 95 | + return Promise.each(times, () => { |
| 96 | + return updateFile().then(() => timeoutAsync(50)); |
| 97 | + }).then(() => fs.writeFileAsync(toCheckFilename, fileContent)); |
| 98 | + }) |
| 99 | + .then(() => timeoutAsync(2000)) |
| 100 | + .then(() => { done(); }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments