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

Improve "Error type should be" assertion in rule-tester #6106

Closed
DrewML opened this issue May 7, 2016 · 10 comments
Closed

Improve "Error type should be" assertion in rule-tester #6106

DrewML opened this issue May 7, 2016 · 10 comments
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion cli Relates to ESLint's command-line interface enhancement This change enhances an existing feature of ESLint good first issue Good for people who haven't worked on ESLint before help wanted The team would welcome a contribution from the community for this issue

Comments

@DrewML
Copy link
Contributor

DrewML commented May 7, 2016

This issue is very similar to #5917, but it's about a different assertion message.

When using the type property in an errors object for invalid code, and the test value does not match the expected value, the following message is currently spit to stdout:

     AssertionError: Error type should be CallExpression
      at testInvalidTemplate (lib/testers/rule-tester.js:9:12161)
      at Context.<anonymous> (lib/testers/rule-tester.js:9:14634)

For debugging purposes, it would be helpful to know what value was provided for the type, not just the expected value.

You can replicate the existing behavior with the following:

Use this descriptor for invalid code in a rule-tester run

    {
        code: "put some code that will fail",
        errors: [{
            line: 1,
            column: 5,
            message: "Some message",
            type: "CallExpression"
        }]
    }

Use this code in a basic rule

context.report({
   // Note that we're not passing a `node` property to `report`
   message: "Some Message",
   loc: {line: 0, column: 0}
});

The source of this problem is in /lib/testers/rule-tester.js on line 400:

assert.equal(messages[i].nodeType, item.errors[i].type, "Error type should be " + item.errors[i].type);

Augmenting the assertion message to include the value for messages[i].nodeType would make debugging a bit easier.

Is there any interest in a PR to improve this?

@eslintbot eslintbot added the triage An ESLint team member will look at this issue soon label May 7, 2016
@alberto alberto added enhancement This change enhances an existing feature of ESLint build This change relates to ESLint's build process evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion core Relates to ESLint's core APIs and features cli Relates to ESLint's command-line interface and removed build This change relates to ESLint's build process triage An ESLint team member will look at this issue soon core Relates to ESLint's core APIs and features labels May 7, 2016
@alberto
Copy link
Member

alberto commented May 7, 2016

@nzakas Would changing the message be a breaking change? Asking because you mentioned it in #6084

@ilyavolodin
Copy link
Member

@alberto This is changing message in rule-tester only, so it will only affect developers of the plugins + us. So pretty sure this will be non-breaking.
@DrewML I think ideally we would want to changing this so you don't get stack trace at all (since it's pretty useless) and instead get before/after. Problem is, most of as are already getting before/after instead of stack trace, and we haven't been able to figure out why.

@DrewML
Copy link
Contributor Author

DrewML commented May 12, 2016

Problem is, most of as are already getting before/after instead of stack trace, and we haven't been able to figure out why.

@ilyavolodin I do get the correct output from most of the assertions, but this particular one seems to be an issue. Can you check whether you see the before/after or a stack trace when using the example I posted above?

@nzakas
Copy link
Member

nzakas commented May 15, 2016

@alberto not breaking. #6084 changed the type of asserting being done, this is just changing the message.

What I don't understand if why the actual and expected aren't being output already. I thought Chai did that when using comparisons by default?

@DrewML
Copy link
Contributor Author

DrewML commented May 15, 2016

@nzakas I don't think Chai's assert API is being used here. I believe the behavior we're seeing is just the behavior of node's built in assert module when a message is passed.

Example:

> var assert = require("assert");
undefined
> assert.equal(1,2)
AssertionError: 1 == 2
    at repl:1:8
    at REPLServer.defaultEval (repl.js:272:27)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:441:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:188:7)
    at REPLServer.Interface._onLine (readline.js:219:10)
    at REPLServer.Interface._line (readline.js:561:8)
    at REPLServer.Interface._ttyWrite (readline.js:838:14)
> assert.equal(1,2, "message")
AssertionError: message
    at repl:1:8
    at REPLServer.defaultEval (repl.js:272:27)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:441:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:188:7)
    at REPLServer.Interface._onLine (readline.js:219:10)
    at REPLServer.Interface._line (readline.js:561:8)
    at REPLServer.Interface._ttyWrite (readline.js:838:14)

I'm guessing Chai doesn't instrument the code before a run, so it probably only sees whatever the assert API spits out.

@nzakas
Copy link
Member

nzakas commented May 16, 2016

@DrewML ah, good call. I forgot we weren't using Chai.

I'm 👍 For this.

@nzakas nzakas added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels May 16, 2016
@DrewML
Copy link
Contributor Author

DrewML commented May 16, 2016

Cool - I'll start working on this tonight. Safe to assume we want to do this for all the assertions in that same area?

For reference:

            if (typeof item.errors === "number") {
                assert.equal(messages.length, item.errors, util.format("Should have %d error%s but had %d: %s",
                    item.errors, item.errors === 1 ? "" : "s", messages.length, util.inspect(messages)));
            } else {
                assert.equal(messages.length, item.errors.length,
                    util.format("Should have %d error%s but had %d: %s",
                    item.errors.length, item.errors.length === 1 ? "" : "s", messages.length, util.inspect(messages)));

                for (var i = 0, l = item.errors.length; i < l; i++) {
                    assert.ok(!("fatal" in messages[i]), "A fatal parsing error occurred: " + messages[i].message);
                    assert.equal(messages[i].ruleId, ruleName, "Error rule name should be the same as the name of the rule being tested");

                    if (typeof item.errors[i] === "string") {

                        // Just an error message.
                        assert.equal(messages[i].message, item.errors[i], "Error message should be " + item.errors[i]);
                    } else if (typeof item.errors[i] === "object") {

                        /*
                         * Error object.
                         * This may have a message, node type, line, and/or
                         * column.
                         */
                        if (item.errors[i].message) {
                            assert.equal(messages[i].message, item.errors[i].message, "Error message should be " + item.errors[i].message);
                        }

                        if (item.errors[i].type) {
                            assert.equal(messages[i].nodeType, item.errors[i].type, "Error type should be " + item.errors[i].type);
                        }

                        if (item.errors[i].hasOwnProperty("line")) {
                            assert.equal(messages[i].line, item.errors[i].line, "Error line should be " + item.errors[i].line);
                        }

                        if (item.errors[i].hasOwnProperty("column")) {
                            assert.equal(messages[i].column, item.errors[i].column, "Error column should be " + item.errors[i].column);
                        }
                    } else {

                        // Only string or object errors are valid.
                        assert.fail(messages[i], null, "Error should be a string or object.");
                    }
                }

                if (item.hasOwnProperty("output")) {
                    var fixResult = SourceCodeFixer.applyFixes(eslint.getSourceCode(), messages);

                    assert.equal(fixResult.output, item.output, "Output is incorrect.");
                }

            }

@nzakas
Copy link
Member

nzakas commented May 16, 2016

Yes, I think so.

@nzakas nzakas added good first issue Good for people who haven't worked on ESLint before help wanted The team would welcome a contribution from the community for this issue labels Jun 25, 2016
@gyandeeps
Copy link
Member

gyandeeps commented Aug 10, 2016

@DrewML Since you have the work done (assuming since u began working on it), would like to send a PR?

@not-an-aardvark
Copy link
Member

This is fixed as of #7550.

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion cli Relates to ESLint's command-line interface enhancement This change enhances an existing feature of ESLint good first issue Good for people who haven't worked on ESLint before help wanted The team would welcome a contribution from the community for this issue
Projects
None yet
Development

No branches or pull requests

7 participants