Skip to content

Commit 47661d3

Browse files
test: add unit tests
1 parent 891bf89 commit 47661d3

4 files changed

Lines changed: 74 additions & 1 deletion

File tree

test/api/__snapshots__/scaffold-utils.test.js.snap.webpack4 renamed to test/api/generators/__snapshots__/scaffold-utils.test.js.snap.webpack4

File renamed without changes.

test/api/__snapshots__/scaffold-utils.test.js.snap.webpack5 renamed to test/api/generators/__snapshots__/scaffold-utils.test.js.snap.webpack5

File renamed without changes.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const path = require("path");
2+
3+
const utilsDirectory = {
4+
cli: "../../../packages/webpack-cli/lib/utils",
5+
generators: "../../../packages/generators/src/utils",
6+
};
7+
8+
jest.setMock(path.join(utilsDirectory.cli, "get-available-installers"), jest.fn());
9+
jest.mock(path.join(utilsDirectory.generators, "scaffold-utils"), () => ({
10+
List: jest.fn(),
11+
}));
12+
13+
const getAvailableInstallers = require(path.join(utilsDirectory.cli, "get-available-installers"));
14+
const getPackageManager = require(path.join(utilsDirectory.cli, "get-package-manager"));
15+
const logger = require(path.join(utilsDirectory.cli, "logger"));
16+
17+
const { getInstaller, getTemplate } = require(path.join(utilsDirectory.generators, "helpers"));
18+
const { List } = require(path.join(utilsDirectory.generators, "scaffold-utils"));
19+
20+
const context = {
21+
prompt: () => {},
22+
supportedTemplates: ["default"],
23+
utils: {
24+
getAvailableInstallers,
25+
getPackageManager,
26+
logger,
27+
},
28+
};
29+
30+
describe("helpers", () => {
31+
it("getInstaller() returns the available installer", async () => {
32+
// Multiple installers are not available
33+
getAvailableInstallers.mockReturnValue(["npm"]);
34+
35+
// Invoke the helper function
36+
const installer = await getInstaller.call(context);
37+
expect(installer).toBe("npm");
38+
});
39+
40+
it("getInstaller() invokes a List prompt if multiple installers are available", async () => {
41+
// Multiple installers are available
42+
getAvailableInstallers.mockReturnValue(["npm", "yarn", "pnpm"]);
43+
44+
// User chose "pnpm"
45+
List.mockReturnValue({ packager: "pnpm" });
46+
47+
// Invoke the helper function
48+
const installer = await getInstaller.call(context);
49+
expect(installer).toBe("pnpm");
50+
});
51+
52+
it("getTemplate() returns with the valid template", async () => {
53+
context.template = "default";
54+
55+
// Invoke the helper function
56+
const template = await getTemplate.call(context);
57+
expect(template).toBe("default");
58+
});
59+
60+
it("getTemplate() invokes a List prompt on supplying an invalid template", async () => {
61+
context.template = "unknown";
62+
63+
// User chose "default"
64+
List.mockReturnValue({ selectedTemplate: "default" });
65+
66+
const loggerMock = jest.spyOn(logger, "warn").mockImplementation(() => {});
67+
68+
// Invoke the helper function`
69+
const template = await getTemplate.call(context);
70+
expect(template).toBe("default");
71+
expect(loggerMock).toHaveBeenCalled();
72+
});
73+
});

test/api/scaffold-utils.test.js renamed to test/api/generators/scaffold-utils.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const {
44
InputValidate,
55
Input,
66
// eslint-disable-next-line node/no-missing-require
7-
} = require("../../packages/generators/src/utils/scaffold-utils");
7+
} = require("../../../packages/generators/src/utils/scaffold-utils");
88

99
describe("utils", () => {
1010
let mockSelf;

0 commit comments

Comments
 (0)