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

feat(std/testing): Add support for object assertion against object subset #8001

Merged
merged 5 commits into from
Oct 21, 2020
Merged

Conversation

lowlighter
Copy link
Contributor

This add supports for a new assertion function assertObjectMatch which allows to test an actual object against an expected object subset (i.e. inclusivity, not equality)

Why ?

It's often useful to test an object partly without testing the remaining properties (like for web api response, where you might want to only check a few fields).

This function filters out actual object with expected object subsets keys before calling assertEquals (so it's actually a wrapper which acts as a syntaxic sugar).

const a  = {
   foo: true,
   bar: true,
   baz: {
     qux: true
   }
}

// Simple assertions
assertObjectMatch(a, {foo: true})  // OK 
assertObjectMatch(a, {bar: true})  // OK
assertObjectMatch(a, {foo: false}) // KO 

// Nested assertions
assertObjectMatch(a, {baz: {qux: true}}) // OK
assertObjectMatch(a, {baz: {qux:null}})  // KO

@CLAassistant
Copy link

CLAassistant commented Oct 16, 2020

CLA assistant check
All committers have signed the CLA.

@lowlighter lowlighter marked this pull request as draft October 16, 2020 17:39
@lowlighter lowlighter marked this pull request as ready for review October 16, 2020 18:57
@bartlomieju
Copy link
Member

Thanks for the contribution @lowlighter! Is there some prior art for this assertion?

@nayeemrmn PTAL

Comment on lines 437 to 438
actual: { [key: string]: unknown },
expected: { [key: string]: unknown },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should symbols be allowed?

@lowlighter
Copy link
Contributor Author

Is there some prior art for this assertion?

Yes, it's heavily inspired from expect.toMatchObject(object) from Jest JavaScript testing framework, though implementation may differ a bit. Since a lot of logic has already been done in assertEquals, I thought it would easier to filter the actual object with the expected one so it'll behave the same.

Should symbols be allowed?

Yes, I'll add them and add tests to cover them !

@bartlomieju
Copy link
Member

Yes, it's heavily inspired from expect.toMatchObject(object) from Jest JavaScript testing framework, though implementation may differ a bit. Since a lot of logic has already been done in assertEquals, I thought it would easier to filter the actual object with the expected one so it'll behave the same.

@lowlighter thanks, that sounds good to me. I'll wait for @nayeemrmn's review before landing

// and filter recursively on object references
const filtered = {} as loose;
for (
const [key, value] of Object.entries(a).filter(([key]) => key in b)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think Object.entries() includes symbols. This would be shown if the tests included an erroneous case for symbols. You can use [...Object.getOwnPropertyNames(a), ...Object.getOwnPropertySymbols(a)] to get a complete list of keys.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced Object.entries() with your suggestion and it works like a charm, and changed string|symbol by PropertyKey which seems more accurate.

I got some trouble with using symbol as index signature so I casted them into string but not sure it's the cleanest way...

I wanted to add an erroneous case like you suggested, but seems that assertEquals does not handle symbols perfectly currently (unless I missed something) :

import { assertEquals } from "https://deno.land/std/testing/asserts.ts"
const foo = Symbol("foo")
Deno.test("symbol1", () => assertEquals({[foo]:"bar"}, {[foo]:"bar"}))
Deno.test("symbol2", () => assertEquals({[foo]:"bar"}, {[Symbol("foo")]:"bar"}))
running 2 tests
test symbol1 ... ok (2ms)
test symbol2 ... ok (1ms)

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (4ms)

Copy link
Collaborator

@nayeemrmn nayeemrmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @lowlighter

@bartlomieju bartlomieju merged commit 23df1c5 into denoland:master Oct 21, 2020
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 21, 2021
…bset (denoland/deno#8001)

This commit add supports for a new assertion function 
"assertObjectMatch" which allows to test an actual object 
against an expected object subset (i.e. inclusivity, not equality).
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
…denoland/deno#8001)

This commit add supports for a new assertion function 
"assertObjectMatch" which allows to test an actual object 
against an expected object subset (i.e. inclusivity, not equality).
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
…denoland/deno#8001)

This commit add supports for a new assertion function 
"assertObjectMatch" which allows to test an actual object 
against an expected object subset (i.e. inclusivity, not equality).
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
…denoland/deno#8001)

This commit add supports for a new assertion function 
"assertObjectMatch" which allows to test an actual object 
against an expected object subset (i.e. inclusivity, not equality).
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
…denoland/deno#8001)

This commit add supports for a new assertion function 
"assertObjectMatch" which allows to test an actual object 
against an expected object subset (i.e. inclusivity, not equality).
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
…denoland/deno#8001)

This commit add supports for a new assertion function 
"assertObjectMatch" which allows to test an actual object 
against an expected object subset (i.e. inclusivity, not equality).
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
…denoland/deno#8001)

This commit add supports for a new assertion function 
"assertObjectMatch" which allows to test an actual object 
against an expected object subset (i.e. inclusivity, not equality).
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
…denoland/deno#8001)

This commit add supports for a new assertion function 
"assertObjectMatch" which allows to test an actual object 
against an expected object subset (i.e. inclusivity, not equality).
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Feb 1, 2021
…denoland/deno#8001)

This commit add supports for a new assertion function 
"assertObjectMatch" which allows to test an actual object 
against an expected object subset (i.e. inclusivity, not equality).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants