mesaugat / chai-exclude

Exclude keys to compare from a deep equal operation with chai expect or assert.
https://yarn.pm/chai-exclude
MIT License
34 stars 11 forks source link

Exclude specific nested key and not every? #41

Closed TomerFi closed 1 year ago

TomerFi commented 1 year ago

Hello

Cool project, thank you very much for sharing! Is there a way for excluding a specific nested key? not every key.

Instead of this:

expect({a: 'aa', b: { a: 'hey', c: 'dd' }, c: 'dd1'})
    .excludingEvery('c')
    .to.deep.equal(({a: 'aa', b: { a: 'hey'}}));

Something like this:

expect({a: 'aa', b: { a: 'hey', c: 'dd' }, c: 'dd1'})
    .excluding('b.c')
    .to.deep.equal(({a: 'aa', b: { a: 'hey'}, c: 'dd1'}));
mesaugat commented 1 year ago

@TomerFi There's no such support at the moment. However, you can just expect/assert b separately by taking it out of the main object.

const { b } = {a: 'aa', b: { a: 'hey', c: 'dd' }, c: 'dd1'};
expect(b).excluding(c).to.deep.equal(b);

Hope it solves your issue.

Relates to #40

TomerFi commented 1 year ago

Yes, thanks. For the record, this should be coupled with another statement because I need to assert for the outer keys as well, so this should be something like:

const res = {a: 'aa', b: { a: 'hey', c: 'dd' }, c: 'dd1'};
expect(res).excluding('b').to.deep.equal({a: 'aa', c: 'dd1'});
expect(res.b).excluding('c').to.deep.equal({ a: 'hey' });

Thanks @mesaugat this is a really useful plugin, I'll watch #40. Unless you want to keep it open for tracking, feel free to close this issue.

mesaugat commented 1 year ago

@TomerFi Yes, you are correct. I left out the outer keys in hopes that you would understand.

mesaugat commented 1 year ago

Closing the issue.