|
| 1 | +const SpeedMeasurePlugin = require("../../.."); |
| 2 | +const webpack = require("webpack"); |
| 3 | +const { readFileSync } = require("fs"); |
| 4 | +const webpackConfig = require("./webpack.config"); |
| 5 | + |
| 6 | +const genSmpWebpackConfig = smp => |
| 7 | + smp.wrap( |
| 8 | + Object.assign({}, webpackConfig, { |
| 9 | + output: { |
| 10 | + path: webpackConfig.output.path + "/_smp_" + new Date().getTime(), |
| 11 | + }, |
| 12 | + }) |
| 13 | + ); |
| 14 | + |
| 15 | +const runWebpack = config => |
| 16 | + new Promise((resolve, reject) => { |
| 17 | + webpack(config, (err, stats) => { |
| 18 | + if (err || stats.hasErrors()) return reject(err || stats); |
| 19 | + resolve(readFileSync(config.output.path + "/bundle.js").toString()); |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | +describe("smp - " + __dirname.split("/").pop(), () => { |
| 24 | + let distApp; |
| 25 | + beforeAll(() => runWebpack(webpackConfig).then(file => (distApp = file))); |
| 26 | + |
| 27 | + describe("vanilla config", () => { |
| 28 | + let smpOutput; |
| 29 | + let smpDistApp; |
| 30 | + const smp = new SpeedMeasurePlugin({ |
| 31 | + outputTarget: output => (smpOutput = output), |
| 32 | + }); |
| 33 | + const smpWebpackConfig = genSmpWebpackConfig(smp); |
| 34 | + |
| 35 | + beforeAll(() => |
| 36 | + runWebpack(smpWebpackConfig).then(file => (smpDistApp = file)) |
| 37 | + ); |
| 38 | + |
| 39 | + it("should generate the same app.js content", () => { |
| 40 | + expect(smpDistApp).toEqual(distApp); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should generate the same app.js content after 2 runs", () => { |
| 44 | + const dupSmpWebpackConfig = genSmpWebpackConfig(smp); |
| 45 | + |
| 46 | + return runWebpack(dupSmpWebpackConfig).then(dupSmpDistApp => { |
| 47 | + expect(dupSmpDistApp).toEqual(smpDistApp); |
| 48 | + expect(dupSmpDistApp).toEqual(distApp); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should state the time taken overall", () => { |
| 53 | + expect(smpOutput).toMatch( |
| 54 | + /General output time took .*([0-9]+ mins? )?[0-9]+(\.[0-9]+)? secs/ |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should state the time taken by the plugins", () => { |
| 59 | + expect(smpOutput).toMatch( |
| 60 | + /DefinePlugin.* took .*([0-9]+ mins? )?[0-9]+(\.[0-9]+)? secs/ |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should state the time taken by the loaders", () => { |
| 65 | + expect(smpOutput).toMatch( |
| 66 | + /babel-loader.* took .*([0-9]+ mins? )?[0-9]+(\.[0-9]+)? secs.*\n\s+module count\s+= [0-9]+/ |
| 67 | + ); |
| 68 | + }); |
| 69 | + }); |
| 70 | +}); |
0 commit comments