webpack/test/ProfilingPlugin.test.js

50 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-12-29 07:21:24 +08:00
"use strict";
const path = require("path");
const fs = require("graceful-fs");
2018-12-29 07:21:24 +08:00
const webpack = require("../");
const rimraf = require("rimraf");
2020-03-29 06:10:15 +08:00
describe("Profiling Plugin", function () {
jest.setTimeout(120000);
2018-12-29 07:21:24 +08:00
it("should handle output path with folder creation", done => {
const outputPath = path.join(__dirname, "js/profilingPath");
const finalPath = path.join(outputPath, "events.json");
2018-12-29 07:21:24 +08:00
rimraf(outputPath, () => {
const startTime = process.hrtime();
2018-12-29 07:21:24 +08:00
const compiler = webpack({
context: __dirname,
2018-12-29 07:21:24 +08:00
entry: "./fixtures/a.js",
output: {
path: path.join(__dirname, "js/profilingOut")
},
2018-12-29 07:21:24 +08:00
plugins: [
new webpack.debug.ProfilingPlugin({
outputPath: finalPath
})
]
});
compiler.run(err => {
if (err) return done(err);
const testDuration = process.hrtime(startTime);
2018-12-29 07:21:24 +08:00
if (!fs.existsSync(outputPath))
return done(new Error("Folder should be created."));
2021-01-20 23:41:00 +08:00
const data = require(finalPath);
const maxTs = data.reduce((max, entry) => Math.max(max, entry.ts), 0);
const minTs = data[0].ts;
const duration = maxTs - minTs;
expect(duration).toBeLessThan(
testDuration[0] * 1000000 + testDuration[1] / 1000
);
2021-01-20 23:41:00 +08:00
const cpuProfile = data.find(entry => entry.name === "CpuProfile");
expect(cpuProfile).toBeTypeOf("object");
const profile = cpuProfile.args.data.cpuProfile;
expect(profile.startTime).toBeGreaterThanOrEqual(minTs);
expect(profile.endTime).toBeLessThanOrEqual(maxTs);
2018-12-29 07:21:24 +08:00
done();
});
});
});
});