├── .all-contributorsrc ├── .gitignore ├── .huskyrc.js ├── .prettierignore ├── .prettierrc.js ├── LICENSE ├── README.md ├── docs └── jest-partial.gif ├── logo.svg ├── package-lock.json ├── package.json ├── src ├── index.js └── index.test.js └── types └── index.d.ts /.all-contributorsrc: -------------------------------------------------------------------------------- 1 | { 2 | "projectName": "jest-partial", 3 | "projectOwner": "smeijer", 4 | "repoType": "github", 5 | "repoHost": "https://github.com", 6 | "files": [ 7 | "README.md" 8 | ], 9 | "imageSize": 100, 10 | "commit": true, 11 | "commitConvention": "angular", 12 | "contributors": [ 13 | { 14 | "login": "smeijer", 15 | "name": "Stephan Meijer", 16 | "avatar_url": "https://avatars1.githubusercontent.com/u/1196524?v=4", 17 | "profile": "https://github.com/smeijer", 18 | "contributions": [ 19 | "ideas", 20 | "code", 21 | "infra", 22 | "maintenance" 23 | ] 24 | }, 25 | ], 26 | "contributorsPerLine": 7, 27 | "skipCi": true 28 | } 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | node_modules/ 3 | -------------------------------------------------------------------------------- /.huskyrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | hooks: { 3 | 'pre-commit': 'pretty-quick --staged', 4 | }, 5 | }; 6 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | node_modules/ 3 | -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | semi: true, 3 | trailingComma: 'all', 4 | singleQuote: true, 5 | printWidth: 80, 6 | tabWidth: 2, 7 | }; 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Stephan Meijer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jest-partial 2 | 3 | 4 | 5 | [![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-) 6 | 7 | 8 | 9 | **Partial Matcher for Jest Expect** 10 | 11 | ![animation of jest-partial matcher](./docs/jest-partial.gif) 12 | 13 | `jest-partial` asserts that the `provided` object is a subset of the `expected`. We don't always want to verify the entire object that has been given. We often just 14 | want to protect the properties that we need, and ignore everything else. 15 | 16 | ## Installation 17 | 18 | With npm: 19 | 20 | ```sh 21 | npm install --save-dev jest-partial 22 | ``` 23 | 24 | With yarn: 25 | 26 | ```sh 27 | yarn add -D jest-partial 28 | ``` 29 | 30 | ## Setup 31 | 32 | ### Jest >v24 33 | 34 | Add `jest-partial` to your Jest `setupFilesAfterEnv` configuration. [See for help](https://jestjs.io/docs/en/configuration.html#setupfilesafterenv-array) 35 | 36 | ```json 37 | "jest": { 38 | "setupFilesAfterEnv": ["jest-partial"] 39 | } 40 | ``` 41 | 42 | ### Jest 145 | 146 | 147 | 148 | 149 | 150 | 151 |

Stephan Meijer

🤔 💻 🚇 🚧
152 | 153 | 154 | 155 | 156 | 157 | 158 | This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! 159 | -------------------------------------------------------------------------------- /docs/jest-partial.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smeijer/jest-partial/d6e0f5ed0f02bf6a0e2d0a8366da53a2987514f6/docs/jest-partial.gif -------------------------------------------------------------------------------- /logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-partial", 3 | "version": "1.0.1", 4 | "description": "A partial matcher for Jest to simplify validation of complex structures.", 5 | "keywords": ["nodejs", "jest", "testing"], 6 | "source": "src/index.js", 7 | "main": "dist/index.js", 8 | "types": "types/index.d.ts", 9 | "license": "MIT", 10 | "author": "Stephan Meijer ", 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/smeijer/jest-partial.git" 14 | }, 15 | "scripts": { 16 | "test": "jest", 17 | "build": "rimraf ./dist && microbundle -i src/index.js -o dist/index.js --no-pkg-main -f umd --target node", 18 | "watch": "rimraf ./dist && microbundle -i src/index.js -o dist/index.js --no-pkg-main -f umd --sourcemap true --compress false --target node --watch --raw", 19 | "prettier": "prettier . --write", 20 | "prepublish": "npm run test && npm run build", 21 | "bump:patch": "npm version patch -m 'release: cut the %s release'", 22 | "bump:minor": "npm version minor -m 'release: cut the %s release'", 23 | "bump:major": "npm version major -m 'release: cut the %s release'", 24 | "preversion": "npm run test && npm run build", 25 | "postversion": "git push && git push --tags && npm publish" 26 | }, 27 | "files": [ 28 | "docs", 29 | "dist", 30 | "types" 31 | ], 32 | "devDependencies": { 33 | "husky": "^4.3.0", 34 | "jest": "^26.4.2", 35 | "microbundle": "^0.12.4", 36 | "prettier": "^2.1.2", 37 | "pretty-quick": "^3.0.2", 38 | "rimraf": "^3.0.2" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | function partial(obj) { 2 | if (Array.isArray(obj)) { 3 | return expect.arrayContaining(obj.map((x) => partial(x))); 4 | } 5 | 6 | if (typeof obj === 'object') { 7 | for (let key of Object.keys(obj)) { 8 | obj[key] = partial(obj[key]); 9 | } 10 | 11 | return expect.objectContaining(obj); 12 | } 13 | 14 | return obj; 15 | } 16 | 17 | expect.extend({ 18 | toMatchPartial(received, argument) { 19 | const pass = this.equals(received, partial(argument)); 20 | 21 | const receivedMsg = this.utils.printReceived(received); 22 | const expectedMsg = this.utils.printExpected(argument); 23 | 24 | const message = pass 25 | ? () => `expected ${receivedMsg} not to match object ${expectedMsg}` 26 | : () => `expected ${receivedMsg} to match object ${expectedMsg}`; 27 | 28 | return { 29 | message, 30 | pass, 31 | }; 32 | }, 33 | }); 34 | -------------------------------------------------------------------------------- /src/index.test.js: -------------------------------------------------------------------------------- 1 | // require('../dist/index'); 2 | require('./index'); 3 | 4 | const kitchen = { 5 | version: '1', 6 | floor: { 7 | material: 'wood', 8 | color: 'walnut', 9 | }, 10 | drawers: [ 11 | { 12 | contents: [ 13 | { type: 'fork', count: 2 }, 14 | { type: 'spoon', count: 4 }, 15 | ], 16 | }, 17 | ], 18 | }; 19 | 20 | test('the kitchen has walnut colored floor', () => { 21 | expect(kitchen).toMatchPartial({ 22 | version: '1', 23 | floor: { color: 'walnut' }, 24 | }); 25 | }); 26 | 27 | test('the kitchen has a drawer that contains spoons', () => { 28 | expect(kitchen).toMatchPartial({ 29 | drawers: [{ contents: [{ type: 'spoon' }] }], 30 | }); 31 | }); 32 | 33 | test('there is a drawer with forks', () => { 34 | expect(kitchen.drawers).toMatchPartial([ 35 | { 36 | contents: [{ type: 'fork' }], 37 | }, 38 | ]); 39 | }); 40 | 41 | test('the kitchen has a drawer that contains 2 forks', () => { 42 | expect(kitchen).toMatchPartial({ 43 | drawers: [{ contents: [{ type: 'fork', count: 2 }] }], 44 | }); 45 | }); 46 | 47 | test('the kitchen has a drawer that contains forks and spoons', () => { 48 | expect(kitchen).toMatchPartial({ 49 | drawers: [{ contents: [{ type: 'fork' }, { type: 'spoon' }] }], 50 | }); 51 | }); 52 | -------------------------------------------------------------------------------- /types/index.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | 3 | declare namespace jest { 4 | // noinspection JSUnusedGlobalSymbols 5 | interface Matchers { 6 | /** 7 | * Use .`toMatchPartial` when checking of the `expected` object contains given structure 8 | * @param {String} message 9 | */ 10 | toMatchPartial(partial: any): R; 11 | } 12 | } 13 | --------------------------------------------------------------------------------