Skip to content

Commit

Permalink
Merge pull request #1 from SteffenHummel/develop
Browse files Browse the repository at this point in the history
first version of jest-json-schema-matcher
  • Loading branch information
SteffenHummel authored Jan 8, 2019
2 parents 69c8f00 + 9378b59 commit 876b4f9
Show file tree
Hide file tree
Showing 9 changed files with 4,022 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
/dist
yarn-error.log
coveragenpm
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
52 changes: 52 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "jest-json-schema-matcher",
"version": "1.0.0",
"description": "jest json schema matcher extension",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/**/*"
],
"scripts": {
"prepare": "npm run build",
"prepublishOnly": "npm test && npm run lint",
"preversion": "npm run lint",
"version": "npm run format && git add -A src",
"postversion": "git push && git push --tags",
"test": "jest",
"build": "tsc",
"lint": "tslint -p . --fix 'src/**/*.{ts,tsx}'",
"formatter": "tsfmt -r --useTsfmt ./tsfmt.json --useTslint ./tslint.json --useTsconfig ./tsconfig.json"
},
"repository": {
"type": "git",
"url": "git+https://github.com/SteffenHummel/jest-json-schema-matcher.git"
},
"keywords": [
"jest",
"json",
"schema",
"matcher",
"extension"
],
"author": "Steffen Hummel",
"license": "MIT",
"bugs": {
"url": "https://github.com/SteffenHummel/jest-json-schema-matcher/issues"
},
"homepage": "https://github.com/SteffenHummel/jest-json-schema-matcher#readme",
"devDependencies": {
"@types/jest": "^23.3.12",
"jest": "^23.6.0",
"ts-jest": "^23.10.5",
"tslint": "^5.12.0",
"tslint-config-airbnb": "^5.11.1",
"tslint-config-prettier": "^1.17.0",
"tslint-sonarts": "^1.8.0",
"typescript": "^3.2.2",
"typescript-formatter": "^7.2.2"
},
"dependencies": {
"jsonschema": "^1.2.4"
}
}
13 changes: 13 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { jsonSchemaMatcher } from '../index'
expect.extend(jsonSchemaMatcher)
test('jsonSchemaMatcher matches schema', () => {
const schema = {
properties: {
testProp: {
type: 'integer',
},
},
required: ['testProp'],
}
expect({ testProp: 1 }).toMatchSchema(schema)
})
24 changes: 24 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as Validator from 'jsonschema'

declare global {
namespace jest {
interface Expect {
toMatchSchema<T>(jsonSchema: any): Matchers<T>
}
interface Matchers<R> {
toMatchSchema(jsonSchema: any): R
}
}
}

export const jsonSchemaMatcher: jest.ExpectExtendMap = {
toMatchSchema(received: any, jsonSchema: any): jest.CustomMatcherResult | Promise<jest.CustomMatcherResult> {

const validationResult: Validator.ValidatorResult = Validator.validate(received, jsonSchema)
const message: string = validationResult.valid ? '' : validationResult.toString()
return {
pass: validationResult.valid,
message: () => message,
}
},
}
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"strict": true
},
"include": ["src"],
"exclude": ["node_modules", "**/__tests__/*"]
}
24 changes: 24 additions & 0 deletions tsfmt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"baseIndentSize": 0,
"indentSize": 2,
"tabSize": 2,
"indentStyle": 2,
"newLineCharacter": "\n",
"convertTabsToSpaces": true,
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterConstructor": false,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"insertSpaceAfterTypeAssertion": false,
"insertSpaceBeforeFunctionParenthesis": false,
"insertSpaceBeforeTypeAnnotation": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
10 changes: 10 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": ["tslint-config-airbnb", "tslint-sonarts"],
"rules": {
"import-name": false,
"semicolon": [true, "never"],
"max-line-length": [true, 300],
"no-eval": true,
"no-default-export": true
}
}
Loading

0 comments on commit 876b4f9

Please sign in to comment.