Skip to content
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

fix: Correctly consume RuleTester statics #15507

Merged
merged 1 commit into from Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/rule-tester/rule-tester.js
Expand Up @@ -977,10 +977,10 @@ class RuleTester {
* This creates a mocha test suite and pipes all supplied info through
* one of the templates above.
*/
RuleTester.describe(ruleName, () => {
RuleTester.describe("valid", () => {
this.constructor.describe(ruleName, () => {
this.constructor.describe("valid", () => {
test.valid.forEach(valid => {
RuleTester[valid.only ? "itOnly" : "it"](
this.constructor[valid.only ? "itOnly" : "it"](
sanitize(typeof valid === "object" ? valid.name || valid.code : valid),
() => {
testValidTemplate(valid);
Expand All @@ -989,9 +989,9 @@ class RuleTester {
});
});

RuleTester.describe("invalid", () => {
this.constructor.describe("invalid", () => {
test.invalid.forEach(invalid => {
RuleTester[invalid.only ? "itOnly" : "it"](
this.constructor[invalid.only ? "itOnly" : "it"](
sanitize(invalid.name || invalid.code),
() => {
testInvalidTemplate(invalid);
Expand Down
81 changes: 61 additions & 20 deletions tests/lib/rule-tester/rule-tester.js
Expand Up @@ -2295,27 +2295,27 @@ describe("RuleTester", () => {
});
});

describe("naming test cases", () => {

/**
* Asserts that a particular value will be emitted from an EventEmitter.
* @param {EventEmitter} emitter The emitter that should emit a value
* @param {string} emitType The type of emission to listen for
* @param {any} expectedValue The value that should be emitted
* @returns {Promise<void>} A Promise that fulfills if the value is emitted, and rejects if something else is emitted.
* The Promise will be indefinitely pending if no value is emitted.
*/
function assertEmitted(emitter, emitType, expectedValue) {
return new Promise((resolve, reject) => {
emitter.once(emitType, emittedValue => {
if (emittedValue === expectedValue) {
resolve();
} else {
reject(new Error(`Expected ${expectedValue} to be emitted but ${emittedValue} was emitted instead.`));
}
});
/**
* Asserts that a particular value will be emitted from an EventEmitter.
* @param {EventEmitter} emitter The emitter that should emit a value
* @param {string} emitType The type of emission to listen for
* @param {any} expectedValue The value that should be emitted
* @returns {Promise<void>} A Promise that fulfills if the value is emitted, and rejects if something else is emitted.
* The Promise will be indefinitely pending if no value is emitted.
*/
function assertEmitted(emitter, emitType, expectedValue) {
return new Promise((resolve, reject) => {
emitter.once(emitType, emittedValue => {
if (emittedValue === expectedValue) {
resolve();
} else {
reject(new Error(`Expected ${expectedValue} to be emitted but ${emittedValue} was emitted instead.`));
}
});
}
});
}

describe("naming test cases", () => {

it("should use the first argument as the name of the test suite", () => {
const assertion = assertEmitted(ruleTesterTestEmitter, "describe", "this-is-a-rule-name");
Expand Down Expand Up @@ -2628,4 +2628,45 @@ describe("RuleTester", () => {
});
});

it("should allow subclasses to set the describe/it/itOnly statics and should correctly use those values", () => {
const assertionDescribe = assertEmitted(ruleTesterTestEmitter, "custom describe", "this-is-a-rule-name");
const assertionIt = assertEmitted(ruleTesterTestEmitter, "custom it", "valid(code);");
const assertionItOnly = assertEmitted(ruleTesterTestEmitter, "custom itOnly", "validOnly(code);");

/**
* Subclass for testing
*/
class RuleTesterSubclass extends RuleTester { }
RuleTesterSubclass.describe = function(text, method) {
ruleTesterTestEmitter.emit("custom describe", text, method);
return method.call(this);
};
RuleTesterSubclass.it = function(text, method) {
ruleTesterTestEmitter.emit("custom it", text, method);
return method.call(this);
};
RuleTesterSubclass.itOnly = function(text, method) {
ruleTesterTestEmitter.emit("custom itOnly", text, method);
return method.call(this);
};

const ruleTesterSubclass = new RuleTesterSubclass();

ruleTesterSubclass.run("this-is-a-rule-name", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [
"valid(code);",
{
code: "validOnly(code);",
only: true
}
],
invalid: []
});

return Promise.all([
assertionDescribe,
assertionIt,
assertionItOnly
]);
});
});