Skip to content

Commit

Permalink
chore(jest): configure jest eslint plugin and improve
Browse files Browse the repository at this point in the history
  • Loading branch information
skovy committed Nov 26, 2023
1 parent cfb2caa commit 546dba6
Show file tree
Hide file tree
Showing 17 changed files with 434 additions and 106 deletions.
10 changes: 8 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/* eslint-env node */
module.exports = {
root: true,
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:jest-formatting/strict",
"plugin:jest/recommended",
],
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint", "promise"],
plugins: ["@typescript-eslint", "promise", "jest", "jest-formatting"],
ignorePatterns: ["dist/**"],
overrides: [
{
Expand All @@ -18,5 +23,6 @@ module.exports = {
],
rules: {
"promise/prefer-await-to-then": "error",
"jest/consistent-test-it": ["error", { fn: "it" }],
},
};
46 changes: 29 additions & 17 deletions __tests__/core/alerts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,70 +15,82 @@ describe("alerts", () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const EXPECTED = expect.stringContaining(TEST_ALERT_MSG);

test("should print all messages with verbose log level", () => {
it("should print all messages with verbose log level", () => {
setAlertsLogLevel("verbose");

alerts.error(TEST_ALERT_MSG);

expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
//make sure each alert only calls console.log once
expect(console.log).toBeCalledTimes(1);
expect(console.log).toHaveBeenCalledTimes(1);

alerts.warn(TEST_ALERT_MSG);

expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
expect(console.log).toBeCalledTimes(2);
expect(console.log).toHaveBeenCalledTimes(2);

alerts.notice(TEST_ALERT_MSG);

expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
expect(console.log).toBeCalledTimes(3);
expect(console.log).toHaveBeenCalledTimes(3);

alerts.info(TEST_ALERT_MSG);

expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
expect(console.log).toBeCalledTimes(4);
expect(console.log).toHaveBeenCalledTimes(4);

alerts.success(TEST_ALERT_MSG);

expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
expect(console.log).toBeCalledTimes(5);
expect(console.log).toHaveBeenCalledTimes(5);
});

test("should only print error messages with error log level", () => {
it("should only print error messages with error log level", () => {
setAlertsLogLevel("error");

alerts.error(TEST_ALERT_MSG);

expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
expect(console.log).toBeCalledTimes(1);
expect(console.log).toHaveBeenCalledTimes(1);

alerts.warn(TEST_ALERT_MSG);
alerts.notice(TEST_ALERT_MSG);
alerts.info(TEST_ALERT_MSG);
alerts.success(TEST_ALERT_MSG);

//shouldn't change
expect(console.log).toBeCalledTimes(1);
expect(console.log).toHaveBeenCalledTimes(1);
});

test("should print all but warning messages with info log level", () => {
it("should print all but warning messages with info log level", () => {
setAlertsLogLevel("info");

alerts.error(TEST_ALERT_MSG);

expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
expect(console.log).toBeCalledTimes(1);
expect(console.log).toHaveBeenCalledTimes(1);

alerts.notice(TEST_ALERT_MSG);

expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
expect(console.log).toBeCalledTimes(2);
expect(console.log).toHaveBeenCalledTimes(2);

alerts.info(TEST_ALERT_MSG);

expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
expect(console.log).toBeCalledTimes(3);
expect(console.log).toHaveBeenCalledTimes(3);

alerts.success(TEST_ALERT_MSG);

expect(console.log).toHaveBeenLastCalledWith(EXPECTED);
expect(console.log).toBeCalledTimes(4);
expect(console.log).toHaveBeenCalledTimes(4);

alerts.warn(TEST_ALERT_MSG);
expect(console.log).toBeCalledTimes(4);

expect(console.log).toHaveBeenCalledTimes(4);
});

test("should print no messages with silent log level", () => {
it("should print no messages with silent log level", () => {
setAlertsLogLevel("silent");

alerts.error(TEST_ALERT_MSG);
Expand All @@ -87,6 +99,6 @@ describe("alerts", () => {
alerts.info(TEST_ALERT_MSG);
alerts.success(TEST_ALERT_MSG);

expect(console.log).not.toBeCalled();
expect(console.log).not.toHaveBeenCalled();
});
});
4 changes: 2 additions & 2 deletions __tests__/core/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describeAllImplementations((implementation) => {
console.log = jest.fn(); // avoid console logs showing up
});

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

await generate(pattern, {
Expand All @@ -29,7 +29,7 @@ describeAllImplementations((implementation) => {
outputFolder: null,
});

expect(fs.writeFileSync).toBeCalledTimes(6);
expect(fs.writeFileSync).toHaveBeenCalledTimes(6);
});
});
});
22 changes: 11 additions & 11 deletions __tests__/core/list-different.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describeAllImplementations((implementation) => {
exit.mockRestore();
});

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

await listDifferent(pattern, {
Expand All @@ -41,15 +41,15 @@ describeAllImplementations((implementation) => {
});

expect(exit).toHaveBeenCalledWith(1);
expect(console.log).toBeCalledWith(
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining(`[INVALID TYPES] Check type definitions for`)
);
expect(console.log).toBeCalledWith(
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining(`invalid.scss`)
);
});

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

await listDifferent(pattern, {
Expand All @@ -70,13 +70,13 @@ describeAllImplementations((implementation) => {
});

expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toBeCalledWith(
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining(`Only 1 file found for`)
);
expect(exit).not.toHaveBeenCalled();
});

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

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

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

await listDifferent(pattern, {
Expand All @@ -119,17 +119,17 @@ describeAllImplementations((implementation) => {
});

expect(exit).toHaveBeenCalledWith(1);
expect(console.log).toBeCalledWith(
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining(
`[INVALID TYPES] Type file needs to be generated for`
)
);
expect(console.log).toBeCalledWith(
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining(`no-generated.scss`)
);
});

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

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

expect(exit).not.toHaveBeenCalled();
expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toBeCalledWith(
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining(`No files found`)
);
});
Expand Down
4 changes: 2 additions & 2 deletions __tests__/core/list-files-and-perform-sanity-check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("listAllFilesAndPerformSanityCheck", () => {

listFilesAndPerformSanityChecks(pattern, options);

expect(console.log).toBeCalledWith(
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining("No files found.")
);
});
Expand All @@ -37,7 +37,7 @@ describe("listAllFilesAndPerformSanityCheck", () => {

listFilesAndPerformSanityChecks(pattern, options);

expect(console.log).toBeCalledWith(
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining("Only 1 file found for")
);
});
Expand Down
32 changes: 20 additions & 12 deletions __tests__/core/remove-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,27 @@ describe("removeFile", () => {

removeSCSSTypeDefinitionFile(nonExistingFile, DEFAULT_OPTIONS);

expect(existsSpy).toBeCalledWith(expect.stringMatching(nonExistingFile));
expect(existsSpy).toBeCalledWith(expect.stringMatching(nonExistingTypes));
expect(unlinkSpy).not.toBeCalled();
expect(alertsSpy).not.toBeCalled();
expect(existsSpy).toHaveBeenCalledWith(
expect.stringMatching(nonExistingFile)
);
expect(existsSpy).toHaveBeenCalledWith(
expect.stringMatching(nonExistingTypes)
);
expect(unlinkSpy).not.toHaveBeenCalled();
expect(alertsSpy).not.toHaveBeenCalled();
});

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

expect(existsSpy).toBeCalledWith(expect.stringMatching(existingTypes));
expect(unlinkSpy).toBeCalled();
expect(unlinkSpy).toBeCalledWith(expect.stringMatching(existingTypes));
expect(alertsSpy).toBeCalled();
expect(existsSpy).toHaveBeenCalledWith(
expect.stringMatching(existingTypes)
);
expect(unlinkSpy).toHaveBeenCalled();
expect(unlinkSpy).toHaveBeenCalledWith(
expect.stringMatching(existingTypes)
);
expect(alertsSpy).toHaveBeenCalled();
});

describe("when outputFolder is passed", () => {
Expand All @@ -70,14 +78,14 @@ describe("removeFile", () => {
outputFolder: "__generated__",
});

expect(existsSpy).toBeCalledWith(
expect(existsSpy).toHaveBeenCalledWith(
expect.stringMatching(outputFolderExistingTypes)
);
expect(unlinkSpy).toBeCalled();
expect(unlinkSpy).toBeCalledWith(
expect(unlinkSpy).toHaveBeenCalled();
expect(unlinkSpy).toHaveBeenCalledWith(
expect.stringMatching(outputFolderExistingTypes)
);
expect(alertsSpy).toBeCalled();
expect(alertsSpy).toHaveBeenCalled();
});
});
});
Loading

0 comments on commit 546dba6

Please sign in to comment.