Skip to content

Commit

Permalink
Enable and apply ESLint Jest rules (MagicMirrorOrg#3270)
Browse files Browse the repository at this point in the history
Jest was in the plugin array of the ESLint configuration, but no rules
were enabled. So ESLint hasn't checked any Jest rules yet.

So I activated the recommended Jest rules and added a few more. Then I
fixed the issues (mostly automatically). I have deactivated the rules
"jest/expect-expect" and "jest/no-done-callback" for the time being, as
they would have entailed major changes. I didn't want to make the PR too
big.

I'm not a Jest expert, but the changes so far look good to me. What do
you think of that @khassel? 🙂
  • Loading branch information
KristjanESPERANTO authored Nov 20, 2023
1 parent 679a413 commit 7098f1e
Show file tree
Hide file tree
Showing 32 changed files with 113 additions and 105 deletions.
11 changes: 9 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": ["eslint:recommended", "plugin:import/recommended", "plugin:jsdoc/recommended", "plugin:prettier/recommended"],
"plugins": ["jest"],
"extends": ["eslint:recommended", "plugin:import/recommended", "plugin:jest/recommended", "plugin:jsdoc/recommended", "plugin:prettier/recommended"],
"plugins": [],
"env": {
"browser": true,
"es2022": true,
Expand All @@ -26,6 +26,13 @@
"import/order": "error",
"import/extensions": "error",
"import/newline-after-import": "error",
"jest/consistent-test-it": "warn",
"jest/expect-expect": "off",
"jest/no-done-callback": "off",
"jest/prefer-expect-resolves": "warn",
"jest/prefer-mock-promise-shorthand": "warn",
"jest/prefer-to-be": "warn",
"jest/prefer-to-have-length": "warn",
"no-param-reassign": "error",
"no-prototype-builtins": "off",
"no-throw-literal": "error",
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ _This release is scheduled to be released on 2024-01-01._
- Added updatenotification Updater (for 3rd party modules)
- Added node 21 to the test matrix
- Added transform object to calendar:customEvents
- Added ESLint rules for jest

### Removed

Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/animateCSS_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ describe("AnimateCSS integration Test", () => {
const doTest = async (animationIn, animationOut) => {
await helpers.getDocument();
let elem = await helpers.waitForElement(`.compliments`);
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
let styles = window.getComputedStyle(elem);

if (animationIn && animationIn !== "") {
expect(styles._values["animation-name"]).toBe(animationIn);
} else {
expect(styles._values["animation-name"]).toBe(undefined);
expect(styles._values["animation-name"]).toBeUndefined();
}

if (animationOut && animationOut !== "") {
elem = await helpers.waitForElement(`.compliments.animate__animated.animate__${animationOut}`);
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
styles = window.getComputedStyle(elem);
expect(styles._values["animation-name"]).toBe(animationOut);
} else {
expect(styles._values["animation-name"]).toBe(undefined);
expect(styles._values["animation-name"]).toBeUndefined();
}
};

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/env_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("App environment", () => {

it("should show the title MagicMirror²", async () => {
const elem = await helpers.waitForElement("title");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toBe("MagicMirror²");
});
});
2 changes: 1 addition & 1 deletion tests/e2e/fonts_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("All font files from roboto.css should be downloadable", () => {
await helpers.stopApplication();
});

test.each(fontFiles)("should return 200 HTTP code for file '%s'", async (fontFile) => {
it.each(fontFiles)("should return 200 HTTP code for file '%s'", async (fontFile) => {
const fontUrl = `http://localhost:8080/fonts/${fontFile}`;
const res = await fetch(fontUrl);
expect(res.status).toBe(200);
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/helpers/global-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ exports.waitForAllElements = (selector) => {

exports.testMatch = async (element, regex) => {
const elem = await this.waitForElement(element);
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toMatch(regex);
};
2 changes: 1 addition & 1 deletion tests/e2e/helpers/weather-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const helpers = require("./global-setup");

exports.getText = async (element, result) => {
const elem = await helpers.waitForElement(element);
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(
elem.textContent
.trim()
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/modules/alert_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("Alert module", () => {

it("should show the welcome message", async () => {
const elem = await helpers.waitForElement(".ns-box .ns-box-inner .light.bright.small");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toContain("Welcome, start was successful!");
});
});
8 changes: 4 additions & 4 deletions tests/e2e/modules/calendar_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ describe("Calendar module", () => {
*/
const testElementLength = async (element, result, not) => {
const elem = await helpers.waitForAllElements(element);
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
if (not === "not") {
expect(elem.length).not.toBe(result);
expect(elem).not.toHaveLength(result);
} else {
expect(elem.length).toBe(result);
expect(elem).toHaveLength(result);
}
};

const testTextContain = async (element, text) => {
const elem = await helpers.waitForElement(element, "undefinedLoading");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toContain(text);
};

Expand Down
14 changes: 7 additions & 7 deletions tests/e2e/modules/clock_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe("Clock module", () => {

it("should not show the time when digital clock is shown", async () => {
const elem = document.querySelector(".clock .digital .time");
expect(elem).toBe(null);
expect(elem).toBeNull();
});
});

Expand All @@ -84,12 +84,12 @@ describe("Clock module", () => {

it("should show the sun times", async () => {
const elem = await helpers.waitForElement(".clock .digital .sun");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
});

it("should show the moon times", async () => {
const elem = await helpers.waitForElement(".clock .digital .moon");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
});
});

Expand All @@ -108,7 +108,7 @@ describe("Clock module", () => {
const currentWeekNumber = moment().week();
const weekToShow = `Week ${currentWeekNumber}`;
const elem = await helpers.waitForElement(".clock .week");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toBe(weekToShow);
});
});
Expand All @@ -121,7 +121,7 @@ describe("Clock module", () => {

it("should show the analog clock face", async () => {
const elem = helpers.waitForElement(".clock-circle");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
});
});

Expand All @@ -133,9 +133,9 @@ describe("Clock module", () => {

it("should show the analog clock face and the date", async () => {
const elemClock = helpers.waitForElement(".clock-circle");
await expect(elemClock).not.toBe(null);
await expect(elemClock).not.toBeNull();
const elemDate = helpers.waitForElement(".clock .date");
await expect(elemDate).not.toBe(null);
await expect(elemDate).not.toBeNull();
});
});
});
6 changes: 3 additions & 3 deletions tests/e2e/modules/compliments_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ describe("Compliments module", () => {
*/
const doTest = async (complimentsArray) => {
let elem = await helpers.waitForElement(".compliments");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
elem = await helpers.waitForElement(".module-content");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(complimentsArray).toContain(elem.textContent);
};

Expand All @@ -18,7 +18,7 @@ describe("Compliments module", () => {
});

describe("Feature anytime in compliments module", () => {
describe("Set anytime and empty compliments for morning, evening and afternoon ", () => {
describe("Set anytime and empty compliments for morning, evening and afternoon", () => {
beforeAll(async () => {
await helpers.startApplication("tests/configs/modules/compliments/compliments_anytime.js");
await helpers.getDocument();
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/modules/helloworld_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("Test helloworld module", () => {

it("Test message helloworld module", async () => {
const elem = await helpers.waitForElement(".helloworld");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toContain("Test HelloWorld Module");
});
});
Expand All @@ -26,7 +26,7 @@ describe("Test helloworld module", () => {

it("Test message helloworld module", async () => {
const elem = await helpers.waitForElement(".helloworld");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toContain("Hello World!");
});
});
Expand Down
16 changes: 8 additions & 8 deletions tests/e2e/modules/newsfeed_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ describe("Newsfeed module", () => {

it("should show the newsfeed title", async () => {
const elem = await helpers.waitForElement(".newsfeed .newsfeed-source");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toContain("Rodrigo Ramirez Blog");
});

it("should show the newsfeed article", async () => {
const elem = await helpers.waitForElement(".newsfeed .newsfeed-title");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toContain("QPanel");
});

it("should NOT show the newsfeed description", async () => {
await helpers.waitForElement(".newsfeed");
const elem = document.querySelector(".newsfeed .newsfeed-desc");
expect(elem).toBe(null);
expect(elem).toBeNull();
});
});

Expand All @@ -38,14 +38,14 @@ describe("Newsfeed module", () => {

it("should not show articles with prohibited words", async () => {
const elem = await helpers.waitForElement(".newsfeed .newsfeed-title");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toContain("Problema VirtualBox");
});

it("should show the newsfeed description", async () => {
const elem = await helpers.waitForElement(".newsfeed .newsfeed-desc");
expect(elem).not.toBe(null);
expect(elem.textContent.length).not.toBe(0);
expect(elem).not.toBeNull();
expect(elem.textContent).not.toHaveLength(0);
});
});

Expand All @@ -57,7 +57,7 @@ describe("Newsfeed module", () => {

it("should show malformed url warning", async () => {
const elem = await helpers.waitForElement(".newsfeed .small", "No news at the moment.");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toContain("Error in the Newsfeed module. Malformed url.");
});
});
Expand All @@ -70,7 +70,7 @@ describe("Newsfeed module", () => {

it("should show empty items info message", async () => {
const elem = await helpers.waitForElement(".newsfeed .small");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toContain("No news at the moment.");
});
});
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/modules/weather_current_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe("Weather module", () => {

it("should render windDirection with an arrow", async () => {
const elem = await helpers.waitForElement(".weather .normal.medium sup i.fa-long-arrow-alt-down");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.outerHTML).toContain("transform:rotate(250deg)");
});

Expand Down
12 changes: 6 additions & 6 deletions tests/e2e/modules/weather_forecast_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("Weather module: Weather Forecast", () => {
for (const [index, icon] of icons.entries()) {
it(`should render icon ${icon}`, async () => {
const elem = await helpers.waitForElement(`.weather table.small tr:nth-child(${index + 1}) td:nth-child(2) span.wi-${icon}`);
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
});
}

Expand All @@ -46,7 +46,7 @@ describe("Weather module: Weather Forecast", () => {
for (const [index, opacity] of opacities.entries()) {
it(`should render fading of rows with opacity=${opacity}`, async () => {
const elem = await helpers.waitForElement(`.weather table.small tr:nth-child(${index + 1})`);
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.outerHTML).toContain(`<tr style="opacity: ${opacity};">`);
});
}
Expand All @@ -72,14 +72,14 @@ describe("Weather module: Weather Forecast", () => {

it("should render custom table class", async () => {
const elem = await helpers.waitForElement(".weather table.myTableClass");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
});

it("should render colored rows", async () => {
const table = await helpers.waitForElement(".weather table.myTableClass");
expect(table).not.toBe(null);
expect(table.rows).not.toBe(null);
expect(table.rows.length).toBe(5);
expect(table).not.toBeNull();
expect(table.rows).not.toBeNull();
expect(table.rows).toHaveLength(5);
});

const precipitations = [undefined, "2.51 mm"];
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/modules_display_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ describe("Display of modules", () => {

it("should show the test header", async () => {
const elem = await helpers.waitForElement("#module_0_helloworld .module-header");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
// textContent gibt hier lowercase zurück, das uppercase wird durch css realisiert, was daher nicht in textContent landet
expect(elem.textContent).toBe("test_header");
});

it("should show no header if no header text is specified", async () => {
const elem = await helpers.waitForElement("#module_1_helloworld .module-header");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toBe("undefined");
});
});
4 changes: 2 additions & 2 deletions tests/e2e/modules_empty_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ describe("Check configuration without modules", () => {

it("shows the message MagicMirror² title", async () => {
const elem = await helpers.waitForElement("#module_1_helloworld .module-content");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toContain("MagicMirror²");
});

it("shows the url of michael's website", async () => {
const elem = await helpers.waitForElement("#module_5_helloworld .module-content");
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toContain("www.michaelteeuw.nl");
});
});
2 changes: 1 addition & 1 deletion tests/e2e/modules_position_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("Position of modules", () => {
const className = position.replace("_", ".");
it(`should show text in ${position}`, async () => {
const elem = await helpers.waitForElement(`.${className}`);
expect(elem).not.toBe(null);
expect(elem).not.toBeNull();
expect(elem.textContent).toContain(`Text in ${position}`);
});
}
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/translations_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("Translations", () => {

await MMM.loadTranslations();

expect(Translator.load.args.length).toBe(1);
expect(Translator.load.args).toHaveLength(1);
expect(Translator.load.calledWith(MMM, "translations/en.json", false)).toBe(true);

done();
Expand All @@ -72,7 +72,7 @@ describe("Translations", () => {

await MMM.loadTranslations();

expect(Translator.load.args.length).toBe(2);
expect(Translator.load.args).toHaveLength(2);
expect(Translator.load.calledWith(MMM, "translations/de.json", false)).toBe(true);
expect(Translator.load.calledWith(MMM, "translations/en.json", true)).toBe(true);

Expand All @@ -91,7 +91,7 @@ describe("Translations", () => {

await MMM.loadTranslations();

expect(Translator.load.args.length).toBe(1);
expect(Translator.load.args).toHaveLength(1);
expect(Translator.load.calledWith(MMM, "translations/en.json", true)).toBe(true);

done();
Expand Down
Loading

0 comments on commit 7098f1e

Please sign in to comment.