Skip to content

Commit

Permalink
fix: Do not provide watch option to webpack to fix deprecation warning
Browse files Browse the repository at this point in the history
Fixes #188
  • Loading branch information
danez committed Apr 18, 2021
1 parent 3c9e5af commit 5decc0d
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 42 deletions.
1 change: 0 additions & 1 deletion src/options/__tests__/WebpackOptionHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ describe("WebpackOptionHelper", () => {
const result = helper.getWebpackOptions();

expect(result).toEqual({
watch: true,
watchOptions: { aggregateTimeout: 300, poll: 1000 },
});
});
Expand Down
1 change: 1 addition & 0 deletions src/options/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const gruntOptions = {
? options.some((option) => option.watch)
: !!options.watch;
},
watch: null,
};

const webpackOptions = {
Expand Down
100 changes: 62 additions & 38 deletions tests/integration/fixtures.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ function runExec(code, opts) {
{
fs,
path,
assertGrunt: assertGruntFactory(opts.returnCode, opts.stdout),
assertGrunt: assertGruntFactory(
opts.returnCode,
opts.stdout,
opts.timeout,
),
},
opts,
);
Expand Down Expand Up @@ -51,7 +55,11 @@ files.forEach((file) => {
describe("Fixture Tests", () => {
let cwd;
beforeEach(async () => {
cwd = path.join(TMP_DIRECTORY, "integration", crypto.randomBytes(20).toString("hex"));
cwd = path.join(
TMP_DIRECTORY,
"integration",
crypto.randomBytes(20).toString("hex"),
);
await fs.ensureDir(cwd);
});
afterEach(async () => {
Expand All @@ -66,42 +74,58 @@ describe("Fixture Tests", () => {
const directoryParts = relativeDirectory.split("/");
const testFunc = directoryParts.pop().startsWith(".") ? test.skip : test;

testFunc(name, async () => {
await fs.copy(directory, cwd);
let optionsLoc = path.join(cwd, "options.json");
let options;
if (await fs.exists(optionsLoc)) {
options = require(optionsLoc);
} else {
options = {
args: [directoryParts.shift()],
};
}

options.args.unshift("--stack");

const execLoc = path.join(cwd, "exec.js");
let execCode;
if (await fs.exists(execLoc)) {
execCode = await fs.readFile(execLoc, "utf-8");
}
const grunt = spawn(GRUNT_BIN, options.args, { cwd });

let stdout = "";
let stderr = "";
grunt.stdout.on("data", (data) => {
stdout += data.toString();
});
grunt.stderr.on("data", (data) => {
stderr += data.toString();
});

return new Promise((resolve) => {
grunt.on("close", (returnCode) => {
if (execCode) runExec(execCode, { returnCode, cwd, stderr, stdout });
resolve();
testFunc(
name,
async () => {
await fs.copy(directory, cwd);
let optionsLoc = path.join(cwd, "options.json");
let options;
if (await fs.exists(optionsLoc)) {
options = require(optionsLoc);
} else {
options = {
args: [directoryParts.shift()],
};
}

options.args.unshift("--stack");

const execLoc = path.join(cwd, "exec.js");
let execCode;
if (await fs.exists(execLoc)) {
execCode = await fs.readFile(execLoc, "utf-8");
}
const grunt = spawn(GRUNT_BIN, options.args, { cwd });

let stdout = "";
let stderr = "";
grunt.stdout.on("data", (data) => {
stdout += data.toString();
});
grunt.stderr.on("data", (data) => {
stderr += data.toString();
});

return new Promise((resolve) => {
const timeout = setTimeout(() => {
grunt.removeAllListeners();
const killed = grunt.kill();
if (!killed) console.error("cannot kill grunt");
finish({ timeout: true });
}, 5000);

const finish = (result) => {
clearTimeout(timeout);
if (execCode) runExec(execCode, { cwd, stderr, stdout, ...result });
resolve();
};

grunt.on("close", (returnCode) => {
finish({ returnCode, timeout: false });
});
});
});
});
},
10000,
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const path = require('path');
const webpack = require('webpack');
const loadGruntWebpackTasks = require('../../../../utils/loadGruntWebpackTasks');

module.exports = function (grunt) {
grunt.initConfig({
webpack: {
test: {
watch: true,
mode: "none",
entry: path.join(__dirname, "entry"),
output: {
path: __dirname,
filename: "output.js",
},
plugins: [
new webpack.DefinePlugin({ okey: JSON.stringify("dokey") }),
],
},
},
});

loadGruntWebpackTasks(grunt);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(okey);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
assertGrunt.timeout();

console.log(stdout, stderr);

const content = fs.readFileSync(path.join(cwd, "output.js"), "utf-8");
expect(content).toMatch(/console\.log\("dokey"\)/);

expect(stdout).not.toMatch(/DEP_WEBPACK_WATCH_WITHOUT_CALLBACK/);
expect(stderr).not.toMatch(/DEP_WEBPACK_WATCH_WITHOUT_CALLBACK/);
11 changes: 8 additions & 3 deletions tests/utils/assertGrunt.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
module.exports = function (returnCode, stdout) {
module.exports = function (returnCode, stdout, timeout) {
return {
success: function assertGruntSuccess() {
expect(timeout).toBe(false);
expect(returnCode).toBe(0);
expect(stdout).not.toMatch(/ERROR/i);
},
failed: function assertGruntSuccess() {
failed: function assertGruntFailed() {
expect(timeout).toBe(false);
expect(returnCode).toBeGreaterThan(0);
expect(stdout).toMatch(/ERROR/i);
}
},
timeout: function assertGruntTimeout() {
expect(timeout).toBe(true);
},
};
};

0 comments on commit 5decc0d

Please sign in to comment.