Skip to content

Commit f0e282e

Browse files
committed
test: improve code coverage
1 parent ff6b17f commit f0e282e

File tree

8 files changed

+196
-4
lines changed

8 files changed

+196
-4
lines changed

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
"types",
1818
"docs",
1919
"templates",
20+
"packages/plugins/lesy-plugin-demo",
2021
],
2122
globals: {
2223
"ts-jest": {
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1+
import { LesyTestBed } from "@lesy/testbed";
12
import configMw from "../src/middlewares/config.mw";
3+
import { resolve } from "path";
4+
// tslint:disable-next-line: import-name
5+
import PluginData from "../src";
26

37
describe("lesy-plugin-config", () => {
4-
it("should load config from file", () => {
5-
const data = configMw.run({
8+
let testBed;
9+
beforeEach(() => {
10+
testBed = new LesyTestBed({
11+
isTypescriptApp: true,
12+
loadDefaultPlugins: false,
613
root: __dirname,
14+
commands: [
15+
{
16+
run: ({ config }) => console.log(config),
17+
},
18+
],
719
config: {
820
"@lesy/lesy-plugin-config": {
921
name: "test-config",
1022
},
1123
},
24+
...PluginData,
1225
});
13-
expect(data.config).toHaveProperty("name", "dummy-config-value-from-file");
26+
});
27+
28+
it("should load config from file", async () => {
29+
const response = await testBed.run([]);
30+
expect(response).toContain("dummy-config-value-from-file");
1431
});
1532
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "test-file"
3+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { LesyTestBed } from "@lesy/testbed";
2+
import { resolve } from "path";
3+
import configstore from "configstore";
4+
// tslint:disable-next-line: import-name
5+
import PluginData from "../src";
6+
7+
jest.mock("inquirer", () => {
8+
return {
9+
prompt: "mock prompt",
10+
};
11+
});
12+
13+
describe("@lesy/lesy-plugin-prompt", () => {
14+
let testBed;
15+
beforeEach(() => {
16+
testBed = new LesyTestBed({
17+
isTypescriptApp: true,
18+
loadDefaultPlugins: false,
19+
root: resolve(__dirname, "./fixtures"),
20+
commands: [
21+
{
22+
run: ({ feature }) => console.log(feature.prompt),
23+
},
24+
{
25+
name: "customprompt",
26+
run: ({ feature }) => {
27+
feature.promptConfig.customPrompt = "custom prompt";
28+
console.log(feature.prompt);
29+
},
30+
},
31+
],
32+
...PluginData,
33+
});
34+
});
35+
36+
it("should call prompt plugin", async () => {
37+
const response = await testBed.run([]);
38+
expect(response).toContain("mock prompt");
39+
});
40+
41+
it("should call custom prompt", async () => {
42+
const response = await testBed.run(["customprompt"]);
43+
expect(response).toContain("custom prompt");
44+
});
45+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"version": "2.4.0",
3+
"name": "test-file"
4+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { LesyTestBed } from "@lesy/testbed";
2+
import { resolve } from "path";
3+
import configstore from "configstore";
4+
// tslint:disable-next-line: import-name
5+
import PluginData from "../src";
6+
7+
describe("@lesy/lesy-plugin-sidekick", () => {
8+
describe("Features", () => {
9+
let testBed;
10+
beforeEach(() => {
11+
testBed = new LesyTestBed({
12+
isTypescriptApp: true,
13+
loadDefaultPlugins: false,
14+
root: resolve(__dirname, "./fixtures"),
15+
commands: [
16+
{
17+
run: ({ feature }) => console.log(feature.pkg.name),
18+
},
19+
],
20+
...PluginData,
21+
});
22+
});
23+
24+
it("should fetch package json file", async () => {
25+
const response = await testBed.run([]);
26+
expect(response).toContain("test-file");
27+
});
28+
29+
it("should return empty object if no package file found", async () => {
30+
testBed.data.root = resolve(__dirname, "./fixtures/xyz");
31+
const response = await testBed.run([]);
32+
expect(response).toContain("undefined");
33+
});
34+
});
35+
36+
describe("Error Middleware", () => {
37+
let testBed;
38+
beforeEach(() => {
39+
testBed = new LesyTestBed({
40+
isTypescriptApp: true,
41+
loadDefaultPlugins: false,
42+
root: resolve(__dirname, "../"),
43+
commands: [
44+
{
45+
run: () => {
46+
throw new Error("error occured");
47+
},
48+
},
49+
{
50+
name: "hello",
51+
run: () => console.log("hello everyone"),
52+
},
53+
],
54+
...PluginData,
55+
});
56+
});
57+
58+
it("should display error stack on runtime errors", async () => {
59+
const response = await testBed.run([]);
60+
expect(response).toContain("error occured");
61+
expect(response).toContain("at Object.run");
62+
expect(response).toContain("at LesyCoreClass.run");
63+
});
64+
65+
it("should not do anything if no error occurs", async () => {
66+
const response = await testBed.run(["hello"]);
67+
expect(response).toContain("hello everyone");
68+
});
69+
});
70+
71+
describe("Version Middleware", () => {
72+
let testBed;
73+
let mockExit;
74+
beforeEach(() => {
75+
testBed = new LesyTestBed({
76+
isTypescriptApp: true,
77+
loadDefaultPlugins: false,
78+
root: resolve(__dirname, "./fixtures"),
79+
config: {
80+
version: {
81+
enable: true,
82+
flags: ["-v", "--version"],
83+
},
84+
},
85+
commands: [
86+
{
87+
name: "hello",
88+
run: () => console.log("hello everyone"),
89+
},
90+
],
91+
...PluginData,
92+
});
93+
mockExit = jest
94+
.spyOn(process, "exit")
95+
.mockImplementationOnce((() => {}) as never);
96+
});
97+
afterEach(() => {
98+
mockExit.mockRestore();
99+
mockExit.mockReset();
100+
});
101+
102+
it("should display version", async () => {
103+
const response = await testBed.run(["hello", "-v"]);
104+
expect(response).toContain("version 2.4.0");
105+
expect(mockExit).toBeCalledTimes(1);
106+
});
107+
108+
it("should display version if expanded flag is provided", async () => {
109+
const response = await testBed.run(["hello", "--version"]);
110+
expect(response).toContain("version 2.4.0");
111+
expect(mockExit).toBeCalledTimes(1);
112+
});
113+
114+
it("should not display version if config is disabled", async () => {
115+
testBed.data.config.version.enable = false;
116+
const response = await testBed.run(["hello", "--version"]);
117+
expect(response).toContain("hello everyone");
118+
expect(mockExit).not.toBeCalled();
119+
});
120+
});
121+
});

packages/plugins/lesy-plugin-sidekick/src/middlewares/error.middleware.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export default {
55
const { stack } = data.cmdRunError;
66
console.log(stack);
77
}
8+
return data;
89
},
910
};

packages/plugins/lesy-plugin-store/__tests__/store.command.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe("@lesy/lesy-plugin-store", () => {
2424
afterEach(() => {
2525
store.clear();
2626
});
27-
fit("should show no keys found if no keys exist", async () => {
27+
it("should show no keys found if no keys exist", async () => {
2828
const response = await testBed.run(["store"]);
2929
expect(response).toContain("No keys found");
3030
});

0 commit comments

Comments
 (0)