Skip to content

Commit

Permalink
WIP New: Add only to RuleTester (refs eslint/rfcs#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
btmills committed Jan 1, 2021
1 parent cc48713 commit 376648d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile.js
Expand Up @@ -545,7 +545,7 @@ target.mocha = () => {

echo("Running unit tests");

lastReturn = exec(`${getBinFile("nyc")} -- ${MOCHA} -R progress -t ${MOCHA_TIMEOUT} -c ${TEST_FILES}`);
lastReturn = exec(`${getBinFile("nyc")} -- ${MOCHA} --forbid-only -R progress -t ${MOCHA_TIMEOUT} -c ${TEST_FILES}`);
if (lastReturn.code !== 0) {
errors++;
}
Expand Down
56 changes: 48 additions & 8 deletions lib/rule-tester/rule-tester.js
Expand Up @@ -120,7 +120,8 @@ const RuleTesterParameters = [
"filename",
"options",
"errors",
"output"
"output",
"only"
];

/*
Expand Down Expand Up @@ -281,6 +282,7 @@ function wrapParser(parser) {
// default separators for testing
const DESCRIBE = Symbol("describe");
const IT = Symbol("it");
const IT_ONLY = Symbol("itOnly");

/**
* This is `it` default handler if `it` don't exist.
Expand Down Expand Up @@ -400,6 +402,38 @@ class RuleTester {
this[IT] = value;
}

static only(item) {
if (typeof item === "string") {
return { code: item, only: true };
}

return { ...item, only: true };
}

static get itOnly() {
if (typeof this[IT_ONLY] === "function") {
return this[IT_ONLY];
}
if (typeof it === "function" && typeof it.only === "function") {
return Function.bind.call(it.only, it);
}

if (typeof RuleTester[DESCRIBE] === "function" || typeof RuleTester[IT] === "function") {
throw new Error(
"Set `RuleTester.itOnly` to use `only` with a custom test framework.\n" +
"See https://eslint.org/docs/developer-guide/nodejs-api#customizing-ruletester for more."
);
}
if (typeof it === "function") {
throw new Error("The current test framework does not support `only`.");
}
throw new Error("To use `only`, use RuleTester with a test framework that provides `it.only()` like Mocha.");
}

static set itOnly(value) {
this[IT_ONLY] = value;
}

/**
* Define a rule for one particular run of tests.
* @param {string} name The name of the rule to define.
Expand Down Expand Up @@ -881,23 +915,29 @@ class RuleTester {
RuleTester.describe(ruleName, () => {
RuleTester.describe("valid", () => {
test.valid.forEach(valid => {
RuleTester.it(sanitize(typeof valid === "object" ? valid.code : valid), () => {
testValidTemplate(valid);
});
RuleTester[valid.only ? "itOnly" : "it"](
sanitize(typeof valid === "object" ? valid.code : valid),
() => {
testValidTemplate(valid);
}
);
});
});

RuleTester.describe("invalid", () => {
test.invalid.forEach(invalid => {
RuleTester.it(sanitize(invalid.code), () => {
testInvalidTemplate(invalid);
});
RuleTester[invalid.only ? "itOnly" : "it"](
sanitize(invalid.code),
() => {
testInvalidTemplate(invalid);
}
);
});
});
});
}
}

RuleTester[DESCRIBE] = RuleTester[IT] = null;
RuleTester[DESCRIBE] = RuleTester[IT] = RuleTester[IT_ONLY] = null;

module.exports = RuleTester;

0 comments on commit 376648d

Please sign in to comment.