Skip to content

Commit 546dba6

Browse files
committed
chore(jest): configure jest eslint plugin and improve
1 parent cfb2caa commit 546dba6

17 files changed

+434
-106
lines changed

.eslintrc.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
/* eslint-env node */
22
module.exports = {
33
root: true,
4-
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
4+
extends: [
5+
"eslint:recommended",
6+
"plugin:@typescript-eslint/recommended",
7+
"plugin:jest-formatting/strict",
8+
"plugin:jest/recommended",
9+
],
510
parser: "@typescript-eslint/parser",
6-
plugins: ["@typescript-eslint", "promise"],
11+
plugins: ["@typescript-eslint", "promise", "jest", "jest-formatting"],
712
ignorePatterns: ["dist/**"],
813
overrides: [
914
{
@@ -18,5 +23,6 @@ module.exports = {
1823
],
1924
rules: {
2025
"promise/prefer-await-to-then": "error",
26+
"jest/consistent-test-it": ["error", { fn: "it" }],
2127
},
2228
};

__tests__/core/alerts.test.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,70 +15,82 @@ describe("alerts", () => {
1515
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
1616
const EXPECTED = expect.stringContaining(TEST_ALERT_MSG);
1717

18-
test("should print all messages with verbose log level", () => {
18+
it("should print all messages with verbose log level", () => {
1919
setAlertsLogLevel("verbose");
2020

2121
alerts.error(TEST_ALERT_MSG);
22+
2223
expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
2324
//make sure each alert only calls console.log once
24-
expect(console.log).toBeCalledTimes(1);
25+
expect(console.log).toHaveBeenCalledTimes(1);
2526

2627
alerts.warn(TEST_ALERT_MSG);
28+
2729
expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
28-
expect(console.log).toBeCalledTimes(2);
30+
expect(console.log).toHaveBeenCalledTimes(2);
2931

3032
alerts.notice(TEST_ALERT_MSG);
33+
3134
expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
32-
expect(console.log).toBeCalledTimes(3);
35+
expect(console.log).toHaveBeenCalledTimes(3);
3336

3437
alerts.info(TEST_ALERT_MSG);
38+
3539
expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
36-
expect(console.log).toBeCalledTimes(4);
40+
expect(console.log).toHaveBeenCalledTimes(4);
3741

3842
alerts.success(TEST_ALERT_MSG);
43+
3944
expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
40-
expect(console.log).toBeCalledTimes(5);
45+
expect(console.log).toHaveBeenCalledTimes(5);
4146
});
4247

43-
test("should only print error messages with error log level", () => {
48+
it("should only print error messages with error log level", () => {
4449
setAlertsLogLevel("error");
4550

4651
alerts.error(TEST_ALERT_MSG);
52+
4753
expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
48-
expect(console.log).toBeCalledTimes(1);
54+
expect(console.log).toHaveBeenCalledTimes(1);
4955

5056
alerts.warn(TEST_ALERT_MSG);
5157
alerts.notice(TEST_ALERT_MSG);
5258
alerts.info(TEST_ALERT_MSG);
5359
alerts.success(TEST_ALERT_MSG);
60+
5461
//shouldn't change
55-
expect(console.log).toBeCalledTimes(1);
62+
expect(console.log).toHaveBeenCalledTimes(1);
5663
});
5764

58-
test("should print all but warning messages with info log level", () => {
65+
it("should print all but warning messages with info log level", () => {
5966
setAlertsLogLevel("info");
6067

6168
alerts.error(TEST_ALERT_MSG);
69+
6270
expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
63-
expect(console.log).toBeCalledTimes(1);
71+
expect(console.log).toHaveBeenCalledTimes(1);
6472

6573
alerts.notice(TEST_ALERT_MSG);
74+
6675
expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
67-
expect(console.log).toBeCalledTimes(2);
76+
expect(console.log).toHaveBeenCalledTimes(2);
6877

6978
alerts.info(TEST_ALERT_MSG);
79+
7080
expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
71-
expect(console.log).toBeCalledTimes(3);
81+
expect(console.log).toHaveBeenCalledTimes(3);
7282

7383
alerts.success(TEST_ALERT_MSG);
84+
7485
expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
75-
expect(console.log).toBeCalledTimes(4);
86+
expect(console.log).toHaveBeenCalledTimes(4);
7687

7788
alerts.warn(TEST_ALERT_MSG);
78-
expect(console.log).toBeCalledTimes(4);
89+
90+
expect(console.log).toHaveBeenCalledTimes(4);
7991
});
8092

81-
test("should print no messages with silent log level", () => {
93+
it("should print no messages with silent log level", () => {
8294
setAlertsLogLevel("silent");
8395

8496
alerts.error(TEST_ALERT_MSG);
@@ -87,6 +99,6 @@ describe("alerts", () => {
8799
alerts.info(TEST_ALERT_MSG);
88100
alerts.success(TEST_ALERT_MSG);
89101

90-
expect(console.log).not.toBeCalled();
102+
expect(console.log).not.toHaveBeenCalled();
91103
});
92104
});

__tests__/core/generate.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describeAllImplementations((implementation) => {
1010
console.log = jest.fn(); // avoid console logs showing up
1111
});
1212

13-
test("generates types for all files matching the pattern", async () => {
13+
it("generates types for all files matching the pattern", async () => {
1414
const pattern = `${__dirname}/../dummy-styles/**/*.scss`;
1515

1616
await generate(pattern, {
@@ -29,7 +29,7 @@ describeAllImplementations((implementation) => {
2929
outputFolder: null,
3030
});
3131

32-
expect(fs.writeFileSync).toBeCalledTimes(6);
32+
expect(fs.writeFileSync).toHaveBeenCalledTimes(6);
3333
});
3434
});
3535
});

__tests__/core/list-different.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describeAllImplementations((implementation) => {
1414
exit.mockRestore();
1515
});
1616

17-
test("logs invalid type definitions and exits with 1", async () => {
17+
it("logs invalid type definitions and exits with 1", async () => {
1818
const pattern = `${__dirname}/../**/*.scss`;
1919

2020
await listDifferent(pattern, {
@@ -41,15 +41,15 @@ describeAllImplementations((implementation) => {
4141
});
4242

4343
expect(exit).toHaveBeenCalledWith(1);
44-
expect(console.log).toBeCalledWith(
44+
expect(console.log).toHaveBeenCalledWith(
4545
expect.stringContaining(`[INVALID TYPES] Check type definitions for`)
4646
);
47-
expect(console.log).toBeCalledWith(
47+
expect(console.log).toHaveBeenCalledWith(
4848
expect.stringContaining(`invalid.scss`)
4949
);
5050
});
5151

52-
test("logs nothing and does not exit when formatted using Prettier", async () => {
52+
it("logs nothing and does not exit when formatted using Prettier", async () => {
5353
const pattern = `${__dirname}/list-different/formatted.scss`;
5454

5555
await listDifferent(pattern, {
@@ -70,13 +70,13 @@ describeAllImplementations((implementation) => {
7070
});
7171

7272
expect(console.log).toHaveBeenCalledTimes(1);
73-
expect(console.log).toBeCalledWith(
73+
expect(console.log).toHaveBeenCalledWith(
7474
expect.stringContaining(`Only 1 file found for`)
7575
);
7676
expect(exit).not.toHaveBeenCalled();
7777
});
7878

79-
test("logs nothing and does not exit if all files are valid", async () => {
79+
it("logs nothing and does not exit if all files are valid", async () => {
8080
const pattern = `${__dirname}/../dummy-styles/**/style.scss`;
8181

8282
await listDifferent(pattern, {
@@ -99,7 +99,7 @@ describeAllImplementations((implementation) => {
9999
expect(console.log).not.toHaveBeenCalled();
100100
});
101101

102-
test("logs not generated type file and exits with 1", async () => {
102+
it("logs not generated type file and exits with 1", async () => {
103103
const pattern = `${__dirname}/list-different/no-generated.scss`;
104104

105105
await listDifferent(pattern, {
@@ -119,17 +119,17 @@ describeAllImplementations((implementation) => {
119119
});
120120

121121
expect(exit).toHaveBeenCalledWith(1);
122-
expect(console.log).toBeCalledWith(
122+
expect(console.log).toHaveBeenCalledWith(
123123
expect.stringContaining(
124124
`[INVALID TYPES] Type file needs to be generated for`
125125
)
126126
);
127-
expect(console.log).toBeCalledWith(
127+
expect(console.log).toHaveBeenCalledWith(
128128
expect.stringContaining(`no-generated.scss`)
129129
);
130130
});
131131

132-
test("ignores ignored files", async () => {
132+
it("ignores ignored files", async () => {
133133
const pattern = `${__dirname}/list-different/no-generated.scss`;
134134

135135
await listDifferent(pattern, {
@@ -150,7 +150,7 @@ describeAllImplementations((implementation) => {
150150

151151
expect(exit).not.toHaveBeenCalled();
152152
expect(console.log).toHaveBeenCalledTimes(1);
153-
expect(console.log).toBeCalledWith(
153+
expect(console.log).toHaveBeenCalledWith(
154154
expect.stringContaining(`No files found`)
155155
);
156156
});

__tests__/core/list-files-and-perform-sanity-check.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe("listAllFilesAndPerformSanityCheck", () => {
2727

2828
listFilesAndPerformSanityChecks(pattern, options);
2929

30-
expect(console.log).toBeCalledWith(
30+
expect(console.log).toHaveBeenCalledWith(
3131
expect.stringContaining("No files found.")
3232
);
3333
});
@@ -37,7 +37,7 @@ describe("listAllFilesAndPerformSanityCheck", () => {
3737

3838
listFilesAndPerformSanityChecks(pattern, options);
3939

40-
expect(console.log).toBeCalledWith(
40+
expect(console.log).toHaveBeenCalledWith(
4141
expect.stringContaining("Only 1 file found for")
4242
);
4343
});

__tests__/core/remove-file.test.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,27 @@ describe("removeFile", () => {
4848

4949
removeSCSSTypeDefinitionFile(nonExistingFile, DEFAULT_OPTIONS);
5050

51-
expect(existsSpy).toBeCalledWith(expect.stringMatching(nonExistingFile));
52-
expect(existsSpy).toBeCalledWith(expect.stringMatching(nonExistingTypes));
53-
expect(unlinkSpy).not.toBeCalled();
54-
expect(alertsSpy).not.toBeCalled();
51+
expect(existsSpy).toHaveBeenCalledWith(
52+
expect.stringMatching(nonExistingFile)
53+
);
54+
expect(existsSpy).toHaveBeenCalledWith(
55+
expect.stringMatching(nonExistingTypes)
56+
);
57+
expect(unlinkSpy).not.toHaveBeenCalled();
58+
expect(alertsSpy).not.toHaveBeenCalled();
5559
});
5660

5761
it("removes *.scss.d.ts types file for *.scss", () => {
5862
removeSCSSTypeDefinitionFile(originalTestFile, DEFAULT_OPTIONS);
5963

60-
expect(existsSpy).toBeCalledWith(expect.stringMatching(existingTypes));
61-
expect(unlinkSpy).toBeCalled();
62-
expect(unlinkSpy).toBeCalledWith(expect.stringMatching(existingTypes));
63-
expect(alertsSpy).toBeCalled();
64+
expect(existsSpy).toHaveBeenCalledWith(
65+
expect.stringMatching(existingTypes)
66+
);
67+
expect(unlinkSpy).toHaveBeenCalled();
68+
expect(unlinkSpy).toHaveBeenCalledWith(
69+
expect.stringMatching(existingTypes)
70+
);
71+
expect(alertsSpy).toHaveBeenCalled();
6472
});
6573

6674
describe("when outputFolder is passed", () => {
@@ -70,14 +78,14 @@ describe("removeFile", () => {
7078
outputFolder: "__generated__",
7179
});
7280

73-
expect(existsSpy).toBeCalledWith(
81+
expect(existsSpy).toHaveBeenCalledWith(
7482
expect.stringMatching(outputFolderExistingTypes)
7583
);
76-
expect(unlinkSpy).toBeCalled();
77-
expect(unlinkSpy).toBeCalledWith(
84+
expect(unlinkSpy).toHaveBeenCalled();
85+
expect(unlinkSpy).toHaveBeenCalledWith(
7886
expect.stringMatching(outputFolderExistingTypes)
7987
);
80-
expect(alertsSpy).toBeCalled();
88+
expect(alertsSpy).toHaveBeenCalled();
8189
});
8290
});
8391
});

0 commit comments

Comments
 (0)