-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0e5a959
Showing
13 changed files
with
2,979 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.idea | ||
coverage | ||
node_modules | ||
publish | ||
yarn-error.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.idea | ||
coverage | ||
node_modules | ||
yarn-error.log | ||
yarn.lock | ||
|
||
src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2017 Dean Merchant | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# toMatchShapeOf | ||
|
||
Provides two matchers which make it easy to test that an object is roughly the shape expect | ||
|
||
## Installation | ||
|
||
```bash | ||
yarn add jest-to-match-shape-of | ||
``` | ||
|
||
```bash | ||
npm install jest-to-match-shape-of | ||
``` | ||
|
||
In your setupTestEnvironment.js | ||
```javascript | ||
// src/setupTestEnvironment.js | ||
require('jest-to-match-shape-of'); | ||
``` | ||
or if you are using typescript | ||
|
||
```typescript | ||
// src/setupTestEnvironment.ts | ||
import 'jest-to-match-shape-of'; | ||
``` | ||
|
||
Then in the "jest" section of your package.json add the following: | ||
`"setupTestFrameworkScriptFile": "<rootDir>/src/setupTestEnvironment.ts"` | ||
|
||
or for typescript: | ||
`"setupTestFrameworkScriptFile": "<rootDir>/src/setupTestEnvironment.ts"` | ||
|
||
## Usage | ||
|
||
``` | ||
expect(someThing).toMatchOneOf([someOtherThingA, someOtherThingB, someOtherThingC]); | ||
expect(someThing).toMatchShapeOf(someOtherThing); | ||
``` | ||
|
||
Works particularly well when being used with typescript to write integration tests e.g. | ||
|
||
```typescript | ||
type Resource = { | ||
someString: string, | ||
maybeNumber: number | null, | ||
}; | ||
|
||
const testResource: Resource = { | ||
someString: 'some realish looking data', | ||
maybeNumber: 6, | ||
}; | ||
|
||
const testResource: Resource = { | ||
someString: 'some realish looking data', | ||
maybeNumber: null, | ||
}; | ||
|
||
describe('an api', () => { | ||
it('returns what I was expecting', () => { | ||
return fetch('/resources/1').then(response => response.json()).then((data) => { | ||
expect(data).toMatchShapeOf(testResource); | ||
}); | ||
}); | ||
|
||
it('could return a couple of different things', () => { | ||
return fetch('/resources/1').then(response => response.json()).then((data) => { | ||
expect(data).toMatchOneOf([testResource, testResourceAlt]); | ||
}); | ||
}) | ||
}) | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"author": "Dean Merchant", | ||
"dependencies": { | ||
"chalk": "^2.0.1", | ||
"lodash": "^4.17.4" | ||
}, | ||
"description": "A jest matcher to verify the structure of an object, particularly useful for api integration tests", | ||
"devDependencies": { | ||
"@types/chalk": "0.4.31", | ||
"@types/jest": "20.0.5", | ||
"@types/lodash": "4.14.71", | ||
"jest": "20.0.4", | ||
"ts-jest": "20.0.7", | ||
"typescript": "2.4.2" | ||
}, | ||
"jest": { | ||
"collectCoverageFrom": [ | ||
"src/**/*.{js,jsx,ts,tsx}" | ||
], | ||
"coveragePathIgnorePatterns": [ | ||
"<rootDir>/src/common-types.ts", | ||
"<rootDir>/src/index.ts" | ||
], | ||
"coverageReporters": ["text"], | ||
"mapCoverage": true, | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"js", | ||
"json" | ||
], | ||
"testEnvironment": "node", | ||
"testMatch": [ | ||
"<rootDir>/src/**/?(*.)(spec|test).ts?(x)" | ||
], | ||
"transform": { | ||
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js" | ||
} | ||
}, | ||
"license": "MIT", | ||
"main": "publish/index.js", | ||
"types": "publish/index.d.ts", | ||
"name": "jest-to-match-shape-of", | ||
"peerDependencies": { | ||
"jest": "20.x" | ||
}, | ||
"repository": "[email protected]:Dean177/jest-to-match-shape-of.git", | ||
"scripts": { | ||
"build": "rm -rf publish && tsc", | ||
"prepublish": "npm test && npm run build", | ||
"test": "jest" | ||
}, | ||
"version": "0.1.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export type MatcherFactory = jasmine.CustomMatcherFactory; | ||
export type Result = jasmine.CustomMatcherResult; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { toMatchOneOf, toMatchShapeOf } from './toMatchShapeOf'; | ||
|
||
declare namespace jest { | ||
interface Matchers<R> { | ||
toMatchOneOf(expected: Array<any>): R, | ||
toMatchShapeOf(expected: any): R, | ||
} | ||
} | ||
|
||
jasmine.addMatchers({ | ||
toMatchOneOf, | ||
toMatchShapeOf, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { toBeTypeOfCompare } from './toBeTypeOf'; | ||
|
||
describe('toBeTypeOf', () => { | ||
it('Returns no error when the types match', () => { | ||
expect(toBeTypeOfCompare(false, true).pass).toBeTruthy(); | ||
|
||
expect(toBeTypeOfCompare(1, 0).pass).toBeTruthy(); | ||
|
||
expect(toBeTypeOfCompare('a', 'b').pass).toBeTruthy(); | ||
expect(toBeTypeOfCompare('a', '').pass).toBeTruthy(); | ||
|
||
expect(toBeTypeOfCompare([1, 2, 3], [1, 2, 3]).pass).toBeTruthy(); | ||
expect(toBeTypeOfCompare([1, 2, 3], []).pass).toBeTruthy(); | ||
expect(toBeTypeOfCompare([1, 2, 3], ['a', 'b', 'c']).pass).toBeTruthy(); | ||
|
||
expect(toBeTypeOfCompare(() => {}, () => {}).pass).toBeTruthy(); | ||
expect(toBeTypeOfCompare(() => {}, function() {}).pass).toBeTruthy(); | ||
|
||
expect(toBeTypeOfCompare(null, null).pass).toBeTruthy(); | ||
|
||
expect(toBeTypeOfCompare(undefined, undefined).pass).toBeTruthy(); | ||
}); | ||
|
||
it(`fails when the types don't match`, () => { | ||
expect(toBeTypeOfCompare('a', false).pass).toBeFalsy(); | ||
expect(toBeTypeOfCompare(1, 'b').pass).toBeFalsy(); | ||
expect(toBeTypeOfCompare('a', []).pass).toBeFalsy(); | ||
expect(toBeTypeOfCompare(null, 1).pass).toBeFalsy(); | ||
expect(toBeTypeOfCompare(null, () => {}).pass).toBeFalsy(); | ||
|
||
expect(toBeTypeOfCompare({}, []).pass).toBeFalsy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { green, red } from 'chalk'; | ||
import { Result } from './common-types'; | ||
|
||
export const toBeTypeOfCompare = (actual: any, expected: any): Result => { | ||
if (actual == null && expected != null) { | ||
return { | ||
message: `expected '${green(typeof expected)}' but got '${red(actual)}'`, | ||
pass: false, | ||
}; | ||
} | ||
|
||
if (Array.isArray(expected)) { | ||
const pass = Array.isArray(actual); | ||
const message = (!pass) ? | ||
`expected '${green('array')}', but was '${red(typeof actual)}'` : | ||
''; | ||
return { message, pass }; | ||
} | ||
|
||
const pass = typeof actual === typeof expected; | ||
const message = (!pass) ? | ||
`expected '${green(typeof expected)}', but was '${red(typeof actual)}' for ${JSON.stringify(actual)}` : | ||
''; | ||
return { message, pass }; | ||
}; |
Oops, something went wrong.