Skip to content

Eslint rule to prevent use of mocha's beforeEach #24492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions common/build/eslint-plugin-fluid/src/rules/no-before-each.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = {
meta: {
type: "problem",
docs: {
description: "Disallow calls to the 'beforeEach' function.",
category: "Best Practices",
recommended: false,
},
messages: {
noBeforeEach: "Calls to 'beforeEach' are not allowed.",
},
schema: [], // No options for this rule
},
create(context) {
return {
CallExpression(node) {
// Check if the callee is a function named "beforeEach"
if (
node.callee.type === "Identifier" &&
node.callee.name === "beforeEach"
) {
context.report({
node,
messageId: "noBeforeEach",
});
}
},
};
},
};
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ const assert = require("assert");
const path = require("path");
const { ESLint } = require("eslint");

describe("ESLint Rule Tests", function () {
describe("no-unchecked-record-access rule", function () {
async function lintFile(file) {
const eslint = new ESLint({
useEslintrc: false,
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe("Example test suite", function () {
beforeEach(function () {
// Example setup code
});

it("Example test", function () {
// Test code
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe("Example test suite", function () {
it("Example test", function () {
// Test code
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const assert = require("assert");
const path = require("path");
const { ESLint } = require("eslint");

describe("no-before-each rule", function () {
async function lintFile(file) {
const eslint = new ESLint({
useEslintrc: false,
overrideConfig: {
rules: {
"no-before-each": "error",
},
parser: "@typescript-eslint/parser",
parserOptions: {
project: path.join(__dirname, "../example/tsconfig.json"),
},
},
rulePaths: [path.join(__dirname, "../../rules")],
});
const fileToLint = path.join(__dirname, file);
const results = await eslint.lintFiles([fileToLint]);
return results[0];
}

it("Should report an error for using beforeEach", async function () {
const result = await lintFile("fixtures/with-before-each.js");
assert.strictEqual(result.errorCount, 1, "Should have 1 error");
assert.strictEqual(
result.messages[0].message,
"Calls to 'beforeEach' are not allowed.",
);
});

it("Should not report an error when beforeEach is not used", async function () {
const result = await lintFile("fixtures/without-before-each.js");
assert.strictEqual(result.errorCount, 0, "Should have no errors");
});
});
36 changes: 36 additions & 0 deletions packages/utils/telemetry-utils/src/test/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/

import { strict as assert } from "node:assert";

describe.only("My test suite", () => {
beforeEach(() => {
throw new Error("Fake error");
});

it("Test 1", () => {
assert.equal(1, 1);
});

it("Test 2", () => {
assert.equal(2, 2);
});
});

describe.only("My second test suite", () => {
function setup(): void {
throw new Error("Fake error");
}

it("Test 1", () => {
setup();
assert.equal(1, 1);
});

it("Test 2", () => {
setup();
assert.equal(2, 2);
});
});
Loading
Oops, something went wrong.