diff --git a/package-lock.json b/package-lock.json index 6714071..ad62eeb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "version": "0.2.2", "license": "Apache-2.0", "dependencies": { - "fast-json-patch": "^3.0.0-1" + "fast-json-patch": "^3.0.0-1", + "js-yaml": "^4.1.0" }, "devDependencies": { "@asyncapi/parser": "^1.13.1", @@ -18,6 +19,7 @@ "@semantic-release/npm": "^7.1.3", "@semantic-release/release-notes-generator": "^9.0.1", "@types/jest": "^26.0.23", + "@types/js-yaml": "^4.0.5", "@types/node": "^15.12.1", "@typescript-eslint/eslint-plugin": "^4.26.0", "@typescript-eslint/parser": "^4.26.0", @@ -48,24 +50,6 @@ "js-yaml": "^4.1.0" } }, - "node_modules/@apidevtools/json-schema-ref-parser/node_modules/argparse": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", - "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", - "dev": true - }, - "node_modules/@apidevtools/json-schema-ref-parser/node_modules/js-yaml": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", - "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", - "dev": true, - "dependencies": { - "argparse": "^2.0.1" - }, - "bin": { - "js-yaml": "bin/js-yaml.js" - } - }, "node_modules/@asyncapi/parser": { "version": "1.13.1", "resolved": "https://registry.npmjs.org/@asyncapi/parser/-/parser-1.13.1.tgz", @@ -83,6 +67,19 @@ "tiny-merge-patch": "^0.1.2" } }, + "node_modules/@asyncapi/parser/node_modules/js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "dev": true, + "dependencies": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + }, + "bin": { + "js-yaml": "bin/js-yaml.js" + } + }, "node_modules/@asyncapi/specs": { "version": "2.12.0", "resolved": "https://registry.npmjs.org/@asyncapi/specs/-/specs-2.12.0.tgz", @@ -628,6 +625,19 @@ "node": ">= 4" } }, + "node_modules/@eslint/eslintrc/node_modules/js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "dev": true, + "dependencies": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + }, + "bin": { + "js-yaml": "bin/js-yaml.js" + } + }, "node_modules/@eslint/eslintrc/node_modules/strip-json-comments": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", @@ -677,6 +687,19 @@ "node": ">=8" } }, + "node_modules/@istanbuljs/load-nyc-config/node_modules/js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "dev": true, + "dependencies": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + }, + "bin": { + "js-yaml": "bin/js-yaml.js" + } + }, "node_modules/@istanbuljs/schema": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/@istanbuljs/schema/-/schema-0.1.3.tgz", @@ -1548,6 +1571,12 @@ "pretty-format": "^26.0.0" } }, + "node_modules/@types/js-yaml": { + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/@types/js-yaml/-/js-yaml-4.0.5.tgz", + "integrity": "sha512-FhpRzf927MNQdRZP0J5DLIdTXhjLYzeUTmLAu69mnVksLH9CJY3IuSeEgbKUki7GQZm0WqDkGzyxju2EZGD2wA==", + "dev": true + }, "node_modules/@types/json-schema": { "version": "7.0.7", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.7.tgz", @@ -2013,7 +2042,6 @@ "version": "1.0.10", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", - "dev": true, "dependencies": { "sprintf-js": "~1.0.2" } @@ -3843,6 +3871,19 @@ "node": ">= 4" } }, + "node_modules/eslint/node_modules/js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "dev": true, + "dependencies": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + }, + "bin": { + "js-yaml": "bin/js-yaml.js" + } + }, "node_modules/eslint/node_modules/levn": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", @@ -4643,6 +4684,19 @@ "node": ">=0.10.0" } }, + "node_modules/gray-matter/node_modules/js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "dev": true, + "dependencies": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + }, + "bin": { + "js-yaml": "bin/js-yaml.js" + } + }, "node_modules/gulp-header": { "version": "1.8.12", "resolved": "https://registry.npmjs.org/gulp-header/-/gulp-header-1.8.12.tgz", @@ -6875,13 +6929,11 @@ "dev": true }, "node_modules/js-yaml": { - "version": "3.14.1", - "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", - "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", - "dev": true, + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", + "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", "dependencies": { - "argparse": "^1.0.7", - "esprima": "^4.0.0" + "argparse": "^2.0.1" }, "bin": { "js-yaml": "bin/js-yaml.js" @@ -11953,8 +12005,7 @@ "node_modules/sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", - "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", - "dev": true + "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=" }, "node_modules/stack-utils": { "version": "2.0.3", @@ -13052,23 +13103,6 @@ "@types/json-schema": "^7.0.6", "call-me-maybe": "^1.0.1", "js-yaml": "^4.1.0" - }, - "dependencies": { - "argparse": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", - "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", - "dev": true - }, - "js-yaml": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", - "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", - "dev": true, - "requires": { - "argparse": "^2.0.1" - } - } } }, "@asyncapi/parser": { @@ -13086,6 +13120,18 @@ "lodash.clonedeep": "^4.5.0", "node-fetch": "^2.6.0", "tiny-merge-patch": "^0.1.2" + }, + "dependencies": { + "js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "dev": true, + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + } } }, "@asyncapi/specs": { @@ -13554,6 +13600,16 @@ "integrity": "sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg==", "dev": true }, + "js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "dev": true, + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + }, "strip-json-comments": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", @@ -13588,6 +13644,18 @@ "get-package-type": "^0.1.0", "js-yaml": "^3.13.1", "resolve-from": "^5.0.0" + }, + "dependencies": { + "js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "dev": true, + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + } } }, "@istanbuljs/schema": { @@ -14349,6 +14417,12 @@ "pretty-format": "^26.0.0" } }, + "@types/js-yaml": { + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/@types/js-yaml/-/js-yaml-4.0.5.tgz", + "integrity": "sha512-FhpRzf927MNQdRZP0J5DLIdTXhjLYzeUTmLAu69mnVksLH9CJY3IuSeEgbKUki7GQZm0WqDkGzyxju2EZGD2wA==", + "dev": true + }, "@types/json-schema": { "version": "7.0.7", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.7.tgz", @@ -14673,7 +14747,6 @@ "version": "1.0.10", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", - "dev": true, "requires": { "sprintf-js": "~1.0.2" } @@ -15737,6 +15810,16 @@ "integrity": "sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg==", "dev": true }, + "js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "dev": true, + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + }, "levn": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", @@ -16728,6 +16811,18 @@ "extend-shallow": "^2.0.1", "js-yaml": "^3.8.1", "toml": "^2.3.2" + }, + "dependencies": { + "js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "dev": true, + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + } } }, "gulp-header": { @@ -18491,13 +18586,11 @@ "dev": true }, "js-yaml": { - "version": "3.14.1", - "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", - "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", - "dev": true, + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", + "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", "requires": { - "argparse": "^1.0.7", - "esprima": "^4.0.0" + "argparse": "^2.0.1" } }, "jsdom": { @@ -22242,8 +22335,7 @@ "sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", - "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", - "dev": true + "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=" }, "stack-utils": { "version": "2.0.3", diff --git a/package.json b/package.json index a14ca08..dcc25db 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,8 @@ }, "homepage": "https://github.com/asyncapi/diff#readme", "dependencies": { - "fast-json-patch": "^3.0.0-1" + "fast-json-patch": "^3.0.0-1", + "js-yaml": "^4.1.0" }, "devDependencies": { "@asyncapi/parser": "^1.13.1", @@ -50,6 +51,7 @@ "@semantic-release/npm": "^7.1.3", "@semantic-release/release-notes-generator": "^9.0.1", "@types/jest": "^26.0.23", + "@types/js-yaml": "^4.0.5", "@types/node": "^15.12.1", "@typescript-eslint/eslint-plugin": "^4.26.0", "@typescript-eslint/parser": "^4.26.0", diff --git a/src/asyncapidiff.ts b/src/asyncapidiff.ts index 289800b..89833d8 100644 --- a/src/asyncapidiff.ts +++ b/src/asyncapidiff.ts @@ -1,5 +1,12 @@ -import { Output, DiffOutputItem } from './types'; +import { + Output, + OutputType, + JSONOutput, + Changes, + AsyncAPIDiffOptions, +} from './types'; import { breaking, nonBreaking, unclassified } from './constants'; +import toProperFormat from './helpers/output/toProperFormat'; /** * Implements methods to deal with diff output. @@ -8,38 +15,52 @@ import { breaking, nonBreaking, unclassified } from './constants'; * @returns {AsyncAPIDiff} AsynAPIDiff */ export default class AsyncAPIDiff { - private output: Output; + private output: JSONOutput; + private outputType: OutputType; - constructor(output: string) { + constructor(output: string, options: AsyncAPIDiffOptions) { // output is a stringified JSON this.output = JSON.parse(output); + this.outputType = options.outputType; } /** - * @returns {Array.} All the breaking changes + * @returns {Changes} All the breaking changes */ - breaking(): DiffOutputItem[] { - return this.output.changes.filter((diff) => diff.type === breaking); + breaking(): Changes { + const breakingChanges = this.output.changes.filter( + (diff) => diff.type === breaking + ); + + return toProperFormat(breakingChanges, this.outputType); } /** - * @returns {Array.} All the non-breaking changes + * @returns {Changes} All the non-breaking changes */ - nonBreaking(): DiffOutputItem[] { - return this.output.changes.filter((diff) => diff.type === nonBreaking); + nonBreaking(): Changes { + const nonBreakingChanges = this.output.changes.filter( + (diff) => diff.type === nonBreaking + ); + + return toProperFormat(nonBreakingChanges, this.outputType); } /** - * @returns {Array.} All the unclassified changes + * @returns {Changes} All the unclassified changes */ - unclassified(): DiffOutputItem[] { - return this.output.changes.filter((diff) => diff.type === unclassified); + unclassified(): Changes { + const unclassifiedChanges = this.output.changes.filter( + (diff) => diff.type === unclassified + ); + + return toProperFormat(unclassifiedChanges, this.outputType); } /** - * @returns {Output} The JSON output + * @returns {Output} The full output */ getOutput(): Output { - return this.output; + return toProperFormat(this.output, this.outputType); } } diff --git a/src/helpers/output/convertToYAML.ts b/src/helpers/output/convertToYAML.ts new file mode 100644 index 0000000..79c7d82 --- /dev/null +++ b/src/helpers/output/convertToYAML.ts @@ -0,0 +1,10 @@ +import yaml from 'js-yaml'; + +/** + * Converts an object to YAML + * @param object The input object + * @returns YAML output + */ +export default function convertToYAML(object: any): string { + return yaml.dump(object); +} diff --git a/src/helpers/output/toProperFormat.ts b/src/helpers/output/toProperFormat.ts new file mode 100644 index 0000000..dff4bbf --- /dev/null +++ b/src/helpers/output/toProperFormat.ts @@ -0,0 +1,18 @@ +import { OutputType } from '../../types'; +import convertToYAML from './convertToYAML'; + +/** + * Converts diff data to the specified format + * @param data The diff data + * @returns formatted diff output + */ +export default function toProperFormat( + data: T, + outputType: OutputType +): T | string { + if (outputType === 'yaml' || outputType === 'yml') { + return convertToYAML(data); + } + + return data; +} diff --git a/src/main.ts b/src/main.ts index 3e11921..89edfc5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,8 +9,9 @@ import { mergeStandard } from './mergeStandard'; * Generates diff between two AsyncAPI documents * @param firstDocument The parsed AsyncAPI document * @param secondDocument The parsed AsyncAPI document - * @param {Object} config Configuration options - * @param {Object} [config.override] Object to override the standard + * @param config Configuration options + * @param {object} [config.override] Object to override the standard + * @param {string} [config.outputType] The type of output * @returns {AsyncAPIDiff} The diff data * * @example @@ -22,7 +23,8 @@ import { mergeStandard } from './mergeStandard'; * remove: 'breaking', // when a property has been removed from the AsyncAPI document * edit: 'unclassified' // when a property has been edited in the AsyncAPI document * } - * } + * }, + * outputType: "yaml" * }) * ``` */ @@ -40,5 +42,7 @@ export function diff( const diffOutput = generateDiff(firstDocument, secondDocument); const output = categorizeChanges(standard as OverrideStandard, diffOutput); - return new AsyncAPIDiff(JSON.stringify(output)); + return new AsyncAPIDiff(JSON.stringify(output), { + outputType: config.outputType || 'json', + }); } diff --git a/src/types.ts b/src/types.ts index abc11e6..873754e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -28,10 +28,14 @@ export type DiffOutputItem = DiffOutput & { type: ChangeType; }; -export interface Output { +export type Changes = DiffOutputItem[] | string; + +export interface JSONOutput { changes: DiffOutputItem[]; } +export type Output = JSONOutput | string; + export type ValueOperation = ReplaceOperation | AddOperation; export type StandardType = typeof standard; @@ -42,6 +46,13 @@ export interface OverrideObject { export type OverrideStandard = StandardType & OverrideObject; +export type OutputType = 'json' | 'yaml' | 'yml'; + +export interface AsyncAPIDiffOptions { + outputType: OutputType; +} + export interface Config { override?: OverrideObject; + outputType?: OutputType; } diff --git a/test/asycnapidiff.spec.ts b/test/asycnapidiff.spec.ts index a3ebfd0..a3b9027 100644 --- a/test/asycnapidiff.spec.ts +++ b/test/asycnapidiff.spec.ts @@ -5,32 +5,72 @@ import { inputDiff, nonbreakingChanges, unclassifiedChanges, + YAMLBreakingChanges, + YAMLNonbreakingChanges, + YAMLOutputDiff, + YAMLUnclassifiedChanges, } from './fixtures/asyncapidiff.fixtures'; describe('AsyncAPIDiff wrapper', () => { test('checks the instance', () => { - expect(new AsyncAPIDiff(JSON.stringify(inputDiff))).toBeInstanceOf( - AsyncAPIDiff - ); + expect( + new AsyncAPIDiff(JSON.stringify(inputDiff), { outputType: 'json' }) + ).toBeInstanceOf(AsyncAPIDiff); }); - test('checks the original output', () => { - const diff = new AsyncAPIDiff(JSON.stringify(inputDiff)); + test('JSON: checks the original output', () => { + const diff = new AsyncAPIDiff(JSON.stringify(inputDiff), { + outputType: 'json', + }); expect(diff.getOutput()).toEqual(inputDiff); }); - test('returns breaking changes', () => { - const diff = new AsyncAPIDiff(JSON.stringify(inputDiff)); + test('JSON: returns breaking changes', () => { + const diff = new AsyncAPIDiff(JSON.stringify(inputDiff), { + outputType: 'json', + }); expect(diff.breaking()).toEqual(breakingChanges); }); - test('returns non-breaking changes', () => { - const diff = new AsyncAPIDiff(JSON.stringify(inputDiff)); + test('JSON: returns non-breaking changes', () => { + const diff = new AsyncAPIDiff(JSON.stringify(inputDiff), { + outputType: 'json', + }); expect(diff.nonBreaking()).toEqual(nonbreakingChanges); }); - test('returns unclassified changes', () => { - const diff = new AsyncAPIDiff(JSON.stringify(inputDiff)); + test('JSON: returns unclassified changes', () => { + const diff = new AsyncAPIDiff(JSON.stringify(inputDiff), { + outputType: 'json', + }); expect(diff.unclassified()).toEqual(unclassifiedChanges); }); + + test('YAML: checks the original output', () => { + const diff = new AsyncAPIDiff(JSON.stringify(inputDiff), { + outputType: 'yaml', + }); + expect(diff.getOutput()).toEqual(YAMLOutputDiff); + }); + + test('YAML: returns breaking changes', () => { + const diff = new AsyncAPIDiff(JSON.stringify(inputDiff), { + outputType: 'yaml', + }); + expect(diff.breaking()).toEqual(YAMLBreakingChanges); + }); + + test('YAML: returns non-breaking changes', () => { + const diff = new AsyncAPIDiff(JSON.stringify(inputDiff), { + outputType: 'yaml', + }); + expect(diff.nonBreaking()).toEqual(YAMLNonbreakingChanges); + }); + + test('YAML: returns unclassified changes', () => { + const diff = new AsyncAPIDiff(JSON.stringify(inputDiff), { + outputType: 'yaml', + }); + expect(diff.unclassified()).toEqual(YAMLUnclassifiedChanges); + }); }); diff --git a/test/fixtures/asyncapidiff.fixtures.ts b/test/fixtures/asyncapidiff.fixtures.ts index 917e26b..a95ed7a 100644 --- a/test/fixtures/asyncapidiff.fixtures.ts +++ b/test/fixtures/asyncapidiff.fixtures.ts @@ -35,3 +35,24 @@ export const unclassifiedChanges = [ path: '/info', }, ]; + +export const YAMLOutputDiff = `changes: + - type: breaking + path: /servers + - type: non-breaking + path: /channels + - type: unclassified + path: /info +`; + +export const YAMLBreakingChanges = `- type: breaking + path: /servers +`; + +export const YAMLNonbreakingChanges = `- type: non-breaking + path: /channels +`; + +export const YAMLUnclassifiedChanges = `- type: unclassified + path: /info +`; diff --git a/test/fixtures/main.fixtures.ts b/test/fixtures/main.fixtures.ts index 6c58190..355c1ee 100644 --- a/test/fixtures/main.fixtures.ts +++ b/test/fixtures/main.fixtures.ts @@ -26,11 +26,11 @@ export const breakingChanges = [ properties: { 'some-common-header': { type: 'string', - 'x-parser-schema-id': '' + 'x-parser-schema-id': '', }, }, type: 'object', - 'x-parser-schema-id': '' + 'x-parser-schema-id': '', }, 'x-some-extension': 'some extension', }, @@ -76,11 +76,11 @@ export const nonBreakingChanges = [ properties: { 'some-common-header': { type: 'string', - 'x-parser-schema-id': '' + 'x-parser-schema-id': '', }, }, type: 'object', - 'x-parser-schema-id': '' + 'x-parser-schema-id': '', }, 'x-some-extension': 'some extension', }, @@ -120,11 +120,11 @@ export const diffOutput = { properties: { 'some-common-header': { type: 'string', - 'x-parser-schema-id': '' + 'x-parser-schema-id': '', }, }, type: 'object', - 'x-parser-schema-id': '' + 'x-parser-schema-id': '', }, 'x-some-extension': 'some extension', }, @@ -160,11 +160,11 @@ export const diffOutput = { properties: { 'some-common-header': { type: 'string', - 'x-parser-schema-id': '' + 'x-parser-schema-id': '', }, }, type: 'object', - 'x-parser-schema-id': '' + 'x-parser-schema-id': '', }, 'x-some-extension': 'some extension', }, @@ -225,11 +225,11 @@ export const changesWithOverrides = { properties: { 'some-common-header': { type: 'string', - 'x-parser-schema-id': '' + 'x-parser-schema-id': '', }, }, type: 'object', - 'x-parser-schema-id': '' + 'x-parser-schema-id': '', }, 'x-some-extension': 'some extension', }, @@ -265,11 +265,11 @@ export const changesWithOverrides = { properties: { 'some-common-header': { type: 'string', - 'x-parser-schema-id': '' + 'x-parser-schema-id': '', }, }, type: 'object', - 'x-parser-schema-id': '' + 'x-parser-schema-id': '', }, 'x-some-extension': 'some extension', }, @@ -324,3 +324,11 @@ export const arrayChanges = { }, ], }; + +export const YAMLArrayChanges = `changes: + - action: remove + path: /servers/google/variables/port/enum/1 + isArrayIndex: true + before: 2 + type: breaking +`; diff --git a/test/helpers/output/convertToYAML.spec.ts b/test/helpers/output/convertToYAML.spec.ts new file mode 100644 index 0000000..30d9517 --- /dev/null +++ b/test/helpers/output/convertToYAML.spec.ts @@ -0,0 +1,17 @@ +import convertToYAML from '../../../src/helpers/output/convertToYAML'; + +describe('YAML output', () => { + test('should return YAML with mock object', () => { + expect(convertToYAML({ hello: 'world' })).toEqual('hello: world\n'); + }); + + test('should return YAML with mock array', () => { + expect(convertToYAML(['hello', 'world'])).toEqual('- hello\n- world\n'); + }); + + test('should return YAML with mock array', () => { + expect(convertToYAML(['hello', { hello: 'world' }])).toEqual( + '- hello\n- hello: world\n' + ); + }); +}); diff --git a/test/main.spec.ts b/test/main.spec.ts index b7572c9..419db0f 100644 --- a/test/main.spec.ts +++ b/test/main.spec.ts @@ -15,6 +15,7 @@ import { specDocument1, specDocument2, arrayChanges, + YAMLArrayChanges, } from './fixtures/main.fixtures'; describe('main function', () => { @@ -65,4 +66,9 @@ describe('main function', () => { const output = diff(specDocument1, specDocument2); expect(output.getOutput()).toEqual(arrayChanges); }); + + test('YAML: checks output with array changes', () => { + const output = diff(specDocument1, specDocument2, { outputType: 'yaml' }); + expect(output.getOutput()).toEqual(YAMLArrayChanges); + }); });