mesaugat / chai-exclude Goto Github PK
View Code? Open in Web Editor NEWExclude keys to compare from a deep equal operation with chai expect or assert.
Home Page: https://yarn.pm/chai-exclude
License: MIT License
Exclude keys to compare from a deep equal operation with chai expect or assert.
Home Page: https://yarn.pm/chai-exclude
License: MIT License
expect(interception.request.body).excludingEvery('streetnumber').deep.equal({ kenmerk: KENMERK, aangevraagdePercelen: [ { type: 'PERCEEL', adres: { straatnaam: 'street', streetnumber: 'randomSomething' } } ] });
we would expect streetnumber to be excluded in all aangevraagdePercelen
assert.deepEqualExcluding
and assert.deepEqualExcludingEvery
declarationsThe test below always passes. If you remove the .excluding, it always fails.
mongodb - 4.17.1
mongoose - 6.12.3
chai - 4.3.10
chai-exclude - 2.1.0
describe('ObjectId comparison', () => {
it('Compare ObjectIds with .excluding', () => {
expect(new ObjectId()).excluding('anything').to.deep.equal(new ObjectId())
})
})
I believe the culprit is fclone, which appears to strip out the internals of ObjectIds
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'}));
sometime I might want to ignore properties only at certain level, eg:
expect({
id: 123,
a: {
id: 345,
b: "c",
}
}).excludingPath(["a", "id"]).to.deep.equals({
id: 123,
a: {
b: "c",
}
})
do you think excludingPath
would be useful ? I’ve worked around it for now by not testing some properties but I’d like to make a PR sometime if you think that would be useful
Things to do:
d.ts
file for chai-excludeHello,
If you could check this repo: https://github.com/luigidt/exclude-issue
It gives me:
mocha --recursive
1) compare dates with exclude
√ compare dates without exclude
1 passing (11ms)
1 failing
1) compare dates with exclude:
AssertionError: expected { a: {} } to deeply equal { a: Thu, 01 Jan 1970 00:00:00 GMT }
+ expected - actual
{
- "a": {}
+ "a": [Date: 1970-01-01T00:00:00.000Z]
}
at Context.<anonymous> (test\index.js:10:16)
at processImmediate (internal/timers.js:456:21)
Node version: v12.18.4
Is this behaviour expected?
Thank you
I'm not sure how the syntax should be for this case:
Input:
{
"success": true,
"data": [
{ id: 1, value: "Hello", created: "2019-10-02 14:30:00" },
{ id: 2, value: "There", created: "2019-10-02 14:30:00" },
{ id: 3, value: "General", created: "2019-10-02 14:30:00" }
]
}
Assert:
expect(res.body).excludingEvery('created').to.eql({
"success": true,
"data": [
{ id: 1, value: "Hello" },
{ id: 2, value: "There" },
{ id: 3, value: "General" }
]
});
I ended up separating the top level, but I was wondering if there is a way to exclude in a single assert? Perhaps something like .excludingEvery('data.created')
?
Separated:
expect(res.body.success).to.eql(true);
expect(res.body.data).excludingEvery('created').to.eql([
{ id: 1, value: "Hello" },
{ id: 2, value: "There" },
{ id: 3, value: "General" }
]);
Hello there, I'm wondering if Chai v5 support is going to be added?
First: Thanks for providing this useful plugin.
Unfortunately I cannot use it in typescript. When trying to import the module via the code from your documentation
import * as chai from 'chai';
import chaiExclude from 'chai-exclude';
chai.use(chaiExclude);
Typescript complains about the default export of chai-exclude:
Module '"chai-exclude"' has no default export.ts
I am using deepExcludingEvery and works perfect in my use case. But i also have another use case where i have to assert ignoring the order of elements present in JSON array.
Here's my test case:
expect(transaction).excluding(['id', 'createdAt']).to.deep.equal({
...testTransaction,
totalCredit: 10.4,
totalDebit: 0
})
{
"credits": [
{
"amount": 10,
- "id": "5a306e849db894d8cf0918d0",
"name": "A book",
"partyIds": [
"123",
"321"
]
},
{
"amount": 0.4,
- "id": "5a306e849db894d8cf0918cf",
"name": "Taxes",
"partyIds": []
}
]
}
Unless I'm missing something, it seems like the arrays are getting passed over. Using version 1.0.3
Classes (or objects whose constructor isn't Object
) are being treated as arrays.
PR #12
I got RangeError: Maximum call stack size exceeded
with deepEqualExlcudingEvery
Deep equal working fine, without Maximum call stack size exceeded
Call:
assert.deepEqualExcludingEvery(structure, expectedStructure, ['start', 'end']);
Expected Object:
{
"declarations": [
{
"id": {
"metadata": {
"ignoredNode": false
}
"name": "abc"
"parentNode": [Circular]
"type": "Identifier"
}
"init": {
"metadata": {
"ignoredNode": false
}
"parentNode": [Circular]
"raw": "'cde'"
"type": "Literal"
"value": "cde"
"x-verbatim-property": {
"content": "'cde'"
"precedence": 20
}
}
"metadata": {
"ignoredNode": false
}
"parentNode": {
"declarations": [Circular]
"kind": "var"
"metadata": {
"ignoredNode": false
}
"parentNode": {
"body": [
[Circular]
]
"metadata": {
"ignoredNode": false
}
"parentNode": [Circular]
"sourceType": "script"
"type": "Program"
}
"type": "VariableDeclaration"
}
"type": "VariableDeclarator"
}
]
"kind": "var"
"metadata": {
"ignoredNode": false
}
"parentNode": {
"body": [
{
"declarations": [
{
"id": {
"metadata": {
"ignoredNode": false
}
"name": "abc"
"parentNode": [Circular]
"type": "Identifier"
}
"init": {
"metadata": {
"ignoredNode": false
}
"parentNode": [Circular]
"raw": "'cde'"
"type": "Literal"
"value": "cde"
"x-verbatim-property": {
"content": "'cde'"
"precedence": 20
}
}
"metadata": {
"ignoredNode": false
}
"parentNode": [Circular]
"type": "VariableDeclarator"
}
]
"kind": "var"
"metadata": {
"ignoredNode": false
}
"parentNode": [Circular]
"type": "VariableDeclaration"
}
]
"metadata": {
"ignoredNode": false
}
"parentNode": [Circular]
"sourceType": "script"
"type": "Program"
}
"type": "VariableDeclaration"
}
Actual Object:
{
"declarations": [
{
"end": 32
"id": {
"end": 24
"metadata": {
"ignoredNode": false
}
"name": "abc"
"parentNode": [Circular]
"start": 21
"type": "Identifier"
}
"init": {
"end": 32
"metadata": {
"ignoredNode": false
}
"parentNode": [Circular]
"raw": "'cde'"
"start": 27
"type": "Literal"
"value": "cde"
"x-verbatim-property": {
"content": "'cde'"
"precedence": 20
}
}
"metadata": {
"ignoredNode": false
}
"parentNode": {
"declarations": [Circular]
"end": 33
"kind": "var"
"metadata": {
"ignoredNode": false
}
"parentNode": {
"body": [
[Circular]
]
"end": 46
"metadata": {
"ignoredNode": false
}
"parentNode": [Circular]
"sourceType": "script"
"start": 0
"type": "Program"
}
"start": 17
"type": "VariableDeclaration"
}
"start": 21
"type": "VariableDeclarator"
}
]
"kind": "var"
"metadata": {
"ignoredNode": false
}
"parentNode": {
"body": [
{
"declarations": [
{
"end": 32
"id": {
"end": 24
"metadata": {
"ignoredNode": false
}
"name": "abc"
"parentNode": [Circular]
"start": 21
"type": "Identifier"
}
"init": {
"end": 32
"metadata": {
"ignoredNode": false
}
"parentNode": [Circular]
"raw": "'cde'"
"start": 27
"type": "Literal"
"value": "cde"
"x-verbatim-property": {
"content": "'cde'"
"precedence": 20
}
}
"metadata": {
"ignoredNode": false
}
"parentNode": [Circular]
"start": 21
"type": "VariableDeclarator"
}
]
"end": 33
"kind": "var"
"metadata": {
"ignoredNode": false
}
"parentNode": [Circular]
"start": 17
"type": "VariableDeclaration"
}
]
"end": 46
"metadata": {
"ignoredNode": false
}
"parentNode": [Circular]
"sourceType": "script"
"start": 0
"type": "Program"
}
"type": "VariableDeclaration"
}
It is possible to add assert
variant of this plugin?
For a JSON object like this removing the line
key should remove all line
properties no matter how deep it is.
{
"type": "Program",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 34
}
},
"comments": [],
"range": [
0,
34
],
"sourceType": "module",
"body": [
{
"type": "ImportDeclaration",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 34
}
},
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"start": 7,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 16
}
},
"local": {
"type": "Identifier",
"start": 7,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 16
},
"identifierName": "something"
},
"name": "something",
"range": [
7,
16
],
"_babelType": "Identifier"
},
"range": [
7,
16
],
"_babelType": "ImportDefaultSpecifier"
}
],
"importKind": "value",
"source": {
"type": "Literal",
"start": 22,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 22
},
"end": {
"line": 1,
"column": 33
}
},
"extra": {
"rawValue": "something",
"raw": "'something'"
},
"value": "something",
"range": [
22,
33
],
"_babelType": "StringLiteral",
"raw": "'something'"
},
"range": [
0,
34
],
"_babelType": "ImportDeclaration"
}
]
}
The API will look similar to excluding
.
expect(obj).excludingEvery('line').to.deep.equal(someObj);
expect(obj).excludingEvery(['line', 'column']).to.deep.equal(someObj);
Not sure how useful this is going to be but we may need it sometimes.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.