Skip to content

Commit

Permalink
fix: failed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolm-kee committed Nov 26, 2022
1 parent 274941c commit 43e3f51
Show file tree
Hide file tree
Showing 3 changed files with 625 additions and 558 deletions.
61 changes: 45 additions & 16 deletions test/client/index.test.js
Expand Up @@ -35,15 +35,19 @@ describe("index", () => {
jest.setMock("../../client-src/socket.js", jest.fn());
socket = require("../../client-src/socket");

const send = jest.fn();

// overlay
jest.setMock("../../client-src/overlay.js", {
hide: jest.fn(),
show: jest.fn(),
createOverlay: () => {return {
send,
}},
formatProblem: (item) => {
return { header: "HEADER warning", body: `BODY: ${item}` };
},
});
overlay = require("../../client-src/overlay");
const { createOverlay } = require("../../client-src/overlay");
overlay = createOverlay();

// reloadApp
jest.setMock("../../client-src/utils/reloadApp.js", jest.fn());
Expand Down Expand Up @@ -89,13 +93,13 @@ describe("index", () => {

expect(log.log.info.mock.calls[0][0]).toMatchSnapshot();
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
expect(overlay.hide).not.toBeCalled();
expect(overlay.send).not.toBeCalledWith({ type: "DISMISS" });

// change flags
onSocketMessage.overlay(true);
onSocketMessage["still-ok"]();

expect(overlay.hide).toBeCalled();
expect(overlay.send).toHaveBeenCalledWith({ type: "DISMISS" });
});

test("should run onSocketMessage.progress and onSocketMessage['progress-update']", () => {
Expand Down Expand Up @@ -191,9 +195,14 @@ describe("index", () => {

// change flags
onSocketMessage.overlay({ warnings: true });
onSocketMessage.warnings([]);
onSocketMessage.warnings(["warning message"]);

expect(overlay.show).toBeCalled();
expect(overlay.send).toHaveBeenCalledTimes(1);
expect(overlay.send).toHaveBeenCalledWith({
type: "BUILD_ERROR",
level: "warning",
messages: ["warning message"],
});
});

test("should parse overlay options from resource query", () => {
Expand All @@ -202,51 +211,71 @@ describe("index", () => {
global.__resourceQuery = `?overlay=${encodeURIComponent(
`{"warnings": false}`
)}`;
overlay.show.mockReset();
overlay.send.mockReset();
socket.mockReset();
jest.unmock("../../client-src/utils/parseURL.js");
require("../../client-src");
onSocketMessage = socket.mock.calls[0][1];

onSocketMessage.warnings(["warn1"]);
expect(overlay.show).not.toBeCalled();
expect(overlay.send).not.toBeCalled();

onSocketMessage.errors(["error1"]);
expect(overlay.show).toBeCalledTimes(1);
expect(overlay.send).toBeCalledTimes(1);
expect(overlay.send).toHaveBeenCalledWith({
type: "BUILD_ERROR",
level: "error",
messages: ["error1"],
});
});

jest.isolateModules(() => {
// Pass JSON config with errors disabled
global.__resourceQuery = `?overlay=${encodeURIComponent(
`{"errors": false}`
)}`;
overlay.show.mockReset();
overlay.send.mockReset();
socket.mockReset();
jest.unmock("../../client-src/utils/parseURL.js");
require("../../client-src");
onSocketMessage = socket.mock.calls[0][1];

onSocketMessage.errors(["error1"]);
expect(overlay.show).not.toBeCalled();
expect(overlay.send).not.toBeCalled();

onSocketMessage.warnings(["warn1"]);
expect(overlay.show).toBeCalledTimes(1);
expect(overlay.send).toBeCalledTimes(1);
expect(overlay.send).toHaveBeenCalledWith({
type: "BUILD_ERROR",
level: "warning",
messages: ["warn1"],
});
});

jest.isolateModules(() => {
// Use simple boolean
global.__resourceQuery = "?overlay=true";
jest.unmock("../../client-src/utils/parseURL.js");
socket.mockReset();
overlay.show.mockReset();
overlay.send.mockReset();
require("../../client-src");
onSocketMessage = socket.mock.calls[0][1];

onSocketMessage.warnings(["warn2"]);
expect(overlay.show).toBeCalledTimes(1);
expect(overlay.send).toBeCalledTimes(1);
expect(overlay.send).toHaveBeenLastCalledWith({
type: "BUILD_ERROR",
level: "warning",
messages: ["warn2"],
});

onSocketMessage.errors(["error2"]);
expect(overlay.show).toBeCalledTimes(2);
expect(overlay.send).toBeCalledTimes(2);
expect(overlay.send).toHaveBeenLastCalledWith({
type: "BUILD_ERROR",
level: "error",
messages: ["error2"],
});
});
});

Expand Down
12 changes: 6 additions & 6 deletions test/e2e/__snapshots__/multi-compiler.test.js.snap.webpack5
Expand Up @@ -51,13 +51,13 @@ Array [
"[HMR] Cannot apply update. Need to do a full reload!",
"[HMR] Error: Aborted because ./browser.js is not accepted
Update propagation: ./browser.js
at applyHandler (http://127.0.0.1:8103/browser.js:1044:31)
at http://127.0.0.1:8103/browser.js:743:21
at applyHandler (http://127.0.0.1:8103/browser.js:1077:31)
at http://127.0.0.1:8103/browser.js:776:21
at Array.map (<anonymous>)
at internalApply (http://127.0.0.1:8103/browser.js:742:54)
at http://127.0.0.1:8103/browser.js:712:26
at waitForBlockingPromises (http://127.0.0.1:8103/browser.js:666:48)
at http://127.0.0.1:8103/browser.js:710:24",
at internalApply (http://127.0.0.1:8103/browser.js:775:54)
at http://127.0.0.1:8103/browser.js:745:26
at waitForBlockingPromises (http://127.0.0.1:8103/browser.js:699:48)
at http://127.0.0.1:8103/browser.js:743:24",
"[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading disabled, Progress disabled, Overlay enabled.",
"[HMR] Waiting for update signal from WDS...",
"Hello from the browser",
Expand Down

0 comments on commit 43e3f51

Please sign in to comment.