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

New Rule: no-assert-unexpected-type #202

Open
bmish opened this issue Sep 14, 2021 · 4 comments
Open

New Rule: no-assert-unexpected-type #202

bmish opened this issue Sep 14, 2021 · 4 comments

Comments

@bmish
Copy link
Collaborator

bmish commented Sep 14, 2021

I've put together a quick/rough list of what argument data type checks we could add to each qunit assertion. Feel free to comment with additional suggestions/corrections.

I do think we should have just a single rule to check the argument data types of all assertions, similar to how we have a single rule assert-args to verify the correct number of arguments for all assertions.

We would use getStaticValue to statically determine the type of arguments where possible. Types that can't be determined would be ignored.

  • deepEqual / notDeepEqual
  • equal / notEqual / strictEqual / notStrictEqual
    • suggest deepEqual and propEqual for objects
    • suggest deepEqual for arrays
    • nothing yet
  • false / true
    • disallow non-boolean values - assert.true(123);
  • ok / notOk
    • nothing yet
  • propEqual / notPropEqual
    • autofix/suggest strictEqual for literal values - assert.propEqual(foo, 123); - partially covered by qunit/require-object-in-propequal, this existing rule could be deprecated and replaced by the holistic rule
@raycohen
Copy link
Contributor

It can be valid to assert strictEqual on arrays and objects. For example testing that a method you called returns the exact instance of an array that was passed into it.

assert.true and assert.false will fail with a non-boolean type, so does have a lint requirement add value there?

@bmish
Copy link
Collaborator Author

bmish commented Sep 15, 2021

@raycohen makes sense on your first point, I'll remove that.

One of the goals of this rule would be to catch assertions that we know will fail due to the usage of incorrect types (more immediate feedback vs. running the tests), so I think it would be very valuable to catch that for the boolean assertions.

@mongoose700
Copy link

For assert.false/assert.true, it should make sure that there's a possibility that the value is false/true respectively, instead of making sure that it's always a boolean. assert.true should work for a true | undefined, or true | string, but not a false.

@bmish
Copy link
Collaborator Author

bmish commented Sep 16, 2021

@mongoose700 that's true, although I wasn't originally planning to take TypeScript information into account in this rule. Considering TypeScript types when available is a good idea, although I want this rule to have basic capabilities with plain JS too. At least initially, I was just planning to use static analysis, like below:

const x = 123;
assert.true(x); // lint violation since we know it's not a boolean
assert.true('hello world'); // lint violation since we know it's not a boolean
assert.deepEqual(foo, 123); // lint violation since this assertion shouldn't be used with literals
assert.propEqual(foo, 123); // lint violation since this assertion shouldn't be used with literals

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants