项目作者: mesaugat

项目描述 :
Exclude keys to compare from a deep equal operation with chai expect or assert.
高级语言: JavaScript
项目地址: git://github.com/mesaugat/chai-exclude.git
创建时间: 2017-10-20T16:19:48Z
项目社区:https://github.com/mesaugat/chai-exclude

开源协议:MIT License

下载


chai-exclude

npm
npm
JavaScript Style Guide
CI Status

Exclude keys to compare from a deep equal operation with chai expect or assert.

Why?

Sometimes you’ll need to exclude object properties that generate unique values while doing an assertion. This plugin makes it easier to remove those properties from comparison.

Works with both objects and array of objects with or without circular references.

Installation

chai-exclude is an ESM package targeting Chai v5 and above.

  1. npm install chai-exclude --save-dev

If you are using Chai v4, you can use the CommonJS version of the package.

  1. npm install chai-exclude@2.1.1 --save-dev

Usage

  1. import { use } from 'chai'
  2. import chaiExclude from 'chai-exclude'
  3. // If you are using TypeScript, the typings for chai-exclude are included in the package
  4. use(chaiExclude)

Examples

All these examples are for JavaScript. If you are using TypeScript and assert, you’ll need to deal with strict types. Take a look at the type definition.

a) excluding

  1. Excluding a top level property from an object
  1. // Object
  2. assert.deepEqualExcluding({ a: 'a', b: 'b' }, { b: 'b' }, 'a')
  3. assert.deepEqualExcluding({ a: 'a', b: 'b' }, { a: 'z', b: 'b' }, 'a')
  4. expect({ a: 'a', b: 'b' }).excluding('a').to.deep.equal({ b: 'b' })
  5. expect({ a: 'a', b: 'b' }).excluding('a').to.deep.equal({ a: 'z', b: 'b' })
  6. // Array
  7. assert.deepEqualExcluding([{ a: 'a', b: 'b' }], [{ b: 'b' }], 'a')
  8. assert.deepEqualExcluding([{ a: 'a', b: 'b' }], [{ a: 'z', b: 'b' }], 'a')
  9. expect([{ a: 'a', b: 'b' }]).excluding('a').to.deep.equal([{ b: 'b' }])
  10. expect([{ a: 'a', b: 'b' }]).excluding('a').to.deep.equal([{ a: 'z', b: 'b' }])
  1. Excluding multiple top level properties from an object
  1. const obj = {
  2. a: 'a',
  3. b: 'b',
  4. c: {
  5. d: 'd',
  6. e: 'e'
  7. }
  8. }
  9. // Object
  10. assert.deepEqualExcluding(obj, { b: 'b' }, ['a', 'c'])
  11. assert.deepEqualExcluding(obj, { a: 'z', b: 'b' }, ['a', 'c'])
  12. expect(obj).excluding(['a', 'c']).to.deep.equal({ b: 'b' })
  13. expect(obj).excluding(['a', 'c']).to.deep.equal({ a: 'z', b: 'b' })
  14. const array = [
  15. {
  16. a: 'a',
  17. b: 'b',
  18. c: {
  19. d: 'd',
  20. e: 'e'
  21. }
  22. }
  23. ]
  24. // Array
  25. assert.deepEqualExcluding(array, [{ b: 'b' }], ['a', 'c'])
  26. assert.deepEqualExcluding(array, [{ a: 'z', b: 'b' }], ['a', 'c'])
  27. expect(array).excluding(['a', 'c']).to.deep.equal([{ b: 'b' }])
  28. expect(array).excluding(['a', 'c']).to.deep.equal([{ a: 'z', b: 'b' }])

b) excludingEvery

  1. Excluding every property in a deeply nested object
  1. const actualObj = {
  2. a: 'a',
  3. b: 'b',
  4. c: {
  5. a: 'a',
  6. b: {
  7. a: 'a',
  8. d: {
  9. a: 'a',
  10. b: 'b',
  11. d: null
  12. }
  13. }
  14. },
  15. d: ['a', 'c']
  16. }
  17. const actualArray = [actualObj]
  18. const expectedObj = {
  19. a: 'z', // a is excluded from comparison
  20. b: 'b',
  21. c: {
  22. b: {
  23. d: {
  24. b: 'b',
  25. d: null
  26. }
  27. }
  28. },
  29. d: ['a', 'c']
  30. }
  31. const expectedArray = [expectedObj]
  32. // Object
  33. assert.deepEqualExcludingEvery(actualObj, expectedObj, 'a')
  34. expect(actualObj).excludingEvery('a').to.deep.equal(expectedObj)
  35. // Array
  36. assert.deepEqualExcludingEvery(actualArray, expectedArray, 'a')
  37. expect(actualArray).excludingEvery('a').to.deep.equal(expectedArray)
  1. Excluding multiple properties in a deeply nested object
  1. const actualObj = {
  2. a: 'a',
  3. b: 'b',
  4. c: {
  5. a: 'a',
  6. b: {
  7. a: 'a',
  8. d: {
  9. a: 'a',
  10. b: 'b',
  11. d: null
  12. }
  13. }
  14. },
  15. d: ['a', 'c']
  16. }
  17. const actualArray = [actualObj]
  18. const expectedObj = {
  19. b: 'b',
  20. c: {
  21. b: {
  22. }
  23. }
  24. }
  25. const expectedArray = [expectedObj]
  26. // Object
  27. assert.deepEqualExcludingEvery(actualObj, expectedObj, ['a', 'd'])
  28. expect(actualObj).excludingEvery(['a', 'd']).to.deep.equal(expectedObj)
  29. // Array
  30. assert.deepEqualExcludingEvery(actualArray, expectedArray, ['a', 'd'])
  31. expect(actualArray).excludingEvery(['a', 'd']).to.deep.equal(expectedArray)

Contributing

Contributions are welcome. If you have any questions create an issue here.

License

MIT