Skip to content

Commit

Permalink
Update: More detailed assert message for rule-tester (#9769)
Browse files Browse the repository at this point in the history
* Update: More detailed assert message for rule-tester

* [squash]add try/finally block

* [squash]append message instead of overwriting it

* [squash]add parentheses surrounding error message
  • Loading branch information
Weijia Wang authored and ilyavolodin committed Jan 4, 2018
1 parent 9fcfabf commit c64195f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
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;
}

0 comments on commit c64195f

Please sign in to comment.