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

Update: More detailed assert message for rule-tester #9769

Merged
merged 4 commits into from Jan 4, 2018
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
26 changes: 22 additions & 4 deletions lib/testers/rule-tester.js
Expand Up @@ -131,13 +131,31 @@ const DESCRIBE = Symbol("describe");
const IT = Symbol("it");

/**
* This is `it` or `describe` if those don't exist.
* This is `it` default handler if `it` don't exist.
* @this {Mocha}
* @param {string} text - The description of the test case.
* @param {Function} method - The logic of the test case.
* @returns {any} Returned value of `method`.
*/
function defaultHandler(text, method) {
function itDefaultHandler(text, method) {
try {
return method.apply(this);
} catch (err) {
if (err instanceof assert.AssertionError) {
err.message += ` (${util.inspect(err.actual)} ${err.operator} ${util.inspect(err.expected)})`;
}
throw err;
}
}

/**
* This is `describe` default handler if `describe` don't exist.
* @this {Mocha}
* @param {string} text - The description of the test case.
* @param {Function} method - The logic of the test case.
* @returns {any} Returned value of `method`.
*/
function describeDefaultHandler(text, method) {
return method.apply(this);
}

Expand Down Expand Up @@ -212,7 +230,7 @@ class RuleTester {
static get describe() {
return (
this[DESCRIBE] ||
(typeof describe === "function" ? describe : defaultHandler)
(typeof describe === "function" ? describe : describeDefaultHandler)
);
}

Expand All @@ -223,7 +241,7 @@ class RuleTester {
static get it() {
return (
this[IT] ||
(typeof it === "function" ? it : defaultHandler)
(typeof it === "function" ? it : itDefaultHandler)
);
}

Expand Down
35 changes: 35 additions & 0 deletions tests/lib/testers/no-test-runners.js
@@ -0,0 +1,35 @@
/**
* @fileoverview Tests for RuleTester without any test runner
* @author Weijia Wang <starkwang@126.com>
*/
"use strict";

/* global describe, it */
/* eslint-disable no-global-assign*/
const assert = require("assert"),
RuleTester = require("../../../lib/testers/rule-tester");
const tmpIt = it;
const tmpDescribe = describe;

it = null;
describe = null;

try {
const ruleTester = new RuleTester();

assert.throws(() => {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [
"bar = baz;"
],
invalid: [
{ code: "var foo = bar;", output: "invalid output", errors: 1 }
]
});
}, /Output is incorrect\. \(' foo = bar;' == 'invalid output'\)$/);
} catch (e) {
throw e;
} finally {
it = tmpIt;
describe = tmpDescribe;
}