Skip to content
/ diff Public

Commit 341081f

Browse files
authoredAug 18, 2021
feat: add the main diff function (#30)
1 parent deaf355 commit 341081f

13 files changed

+554
-25
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
# Disable specific duplicate code since it would introduce more complexity to reduce it.
22
sonar.cpd.exclusions=src/standard.ts
3-
sonar.exclusions=src/standard.ts

‎src/asyncapidiff.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Output, DiffOutputItem } from './types';
2+
import { breaking, nonBreaking, unclassified } from './constants';
23

34
/**
45
* Implements functions to deal with the diff.
@@ -10,28 +11,28 @@ export default class AsyncAPIDiff {
1011

1112
constructor(output: string) {
1213
// output is a stringified JSON
13-
this.output= JSON.parse(output);
14+
this.output = JSON.parse(output);
1415
}
1516

1617
/**
1718
* @returns All the breaking changes
1819
*/
1920
breaking(): DiffOutputItem[] {
20-
return this.output.changes.filter((diff) => diff.type === 'breaking');
21+
return this.output.changes.filter((diff) => diff.type === breaking);
2122
}
2223

2324
/**
2425
* @returns All the non-breaking changes
2526
*/
2627
nonBreaking(): DiffOutputItem[] {
27-
return this.output.changes.filter((diff) => diff.type === 'non-breaking');
28+
return this.output.changes.filter((diff) => diff.type === nonBreaking);
2829
}
2930

3031
/**
3132
* @returns All the unclassified changes
3233
*/
3334
unclassified(): DiffOutputItem[] {
34-
return this.output.changes.filter((diff) => diff.type === 'unclassified');
35+
return this.output.changes.filter((diff) => diff.type === unclassified);
3536
}
3637

3738
/**

‎src/classifier.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Disabling this since the property we are accessing will always have `/` as the prefix
33
// Thus preventing the prototype chain attacks
44

5+
import { unclassified } from './constants';
56
import { generateClassifierPath } from './helpers/ClassifierHelpers';
67
import { Classifier, OverrideStandard } from './types';
78

@@ -18,9 +19,9 @@ export default function classifier(
1819
const classifierPath = generateClassifierPath(standard, path);
1920
if (!classifierPath) {
2021
return {
21-
add: 'unclassified',
22-
remove: 'unclassified',
23-
edit: 'unclassified',
22+
add: unclassified,
23+
remove: unclassified,
24+
edit: unclassified,
2425
};
2526
}
2627
return standard[classifierPath];

‎src/constants.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const breaking = 'breaking';
2+
export const nonBreaking = 'non-breaking';
3+
export const unclassified = 'unclassified';

‎src/diff.ts ‎src/generateDiff.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { DiffOutput } from './types';
99
* @param {*} secondDocument The second document in JSON format
1010
* @returns {DiffOutput[]} An array containing all the diffs
1111
*/
12-
export default function diff(
12+
export default function generateDiff(
1313
firstDocument: any,
1414
secondDocument: any
1515
): DiffOutput[] {

‎src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export {};
1+
export * from './main';
2+
export * from './types';

‎src/main.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Config, OverrideStandard } from './types';
2+
import generateDiff from './generateDiff';
3+
import { standard } from './standard';
4+
import categorizeChanges from './categorizeChanges';
5+
import AsyncAPIDiff from './asyncapidiff';
6+
import { mergeStandard } from './mergeStandard';
7+
8+
/**
9+
* Generates diff between two AsyncAPI documents
10+
* @param firstDocument The parsed AsyncAPI document
11+
* @param secondDocument The parsed AsyncAPI document
12+
* @param {Object} config Configuration options
13+
* @param {Object} [config.override] Object to override the standard
14+
* @returns {AsyncAPIDiff} The diff data
15+
*
16+
* @example
17+
* const output = diff(firstDocument, secondDocument, {
18+
* override: {
19+
* '/servers': {
20+
* add: 'non-breaking', // when a property has been added in the AsyncAPI document
21+
* remove: 'breaking', // when a property has been removed from the AsyncAPI document
22+
* edit: 'unclassified' // when a property has been edited in the AsyncAPI document
23+
* }
24+
* }
25+
* })
26+
*/
27+
export function diff(
28+
firstDocument: any,
29+
secondDocument: any,
30+
config: Config = {}
31+
): AsyncAPIDiff {
32+
if (config.override) {
33+
if (typeof config.override !== 'object') {
34+
throw new TypeError('Override data must be an object');
35+
}
36+
mergeStandard(standard, config.override);
37+
}
38+
39+
const diffOutput = generateDiff(firstDocument, secondDocument);
40+
const output = categorizeChanges(standard as OverrideStandard, diffOutput);
41+
return new AsyncAPIDiff(JSON.stringify(output));
42+
}

‎src/standard.ts

+96-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const breaking = 'breaking';
2-
const nonBreaking = 'non-breaking';
3-
const unclassified = 'unclassified';
1+
import { breaking, nonBreaking, unclassified } from './constants';
42

53
/**
64
* The standard object
@@ -126,6 +124,11 @@ export const standard = {
126124
remove: breaking,
127125
edit: breaking,
128126
},
127+
'/servers/*/variables/*/enum/*': {
128+
add: nonBreaking,
129+
remove: breaking,
130+
edit: breaking,
131+
},
129132
'/servers/*/variables/*/default': {
130133
add: breaking,
131134
remove: breaking,
@@ -141,11 +144,21 @@ export const standard = {
141144
remove: nonBreaking,
142145
edit: nonBreaking,
143146
},
147+
'/servers/*/variables/*/examples/*': {
148+
add: nonBreaking,
149+
remove: nonBreaking,
150+
edit: nonBreaking,
151+
},
144152
'/servers/*/security': {
145153
add: breaking,
146154
remove: breaking,
147155
edit: breaking,
148156
},
157+
'/servers/*/security/*': {
158+
add: breaking,
159+
remove: breaking,
160+
edit: breaking,
161+
},
149162
'/servers/*/bindings': {
150163
add: unclassified,
151164
remove: unclassified,
@@ -196,6 +209,11 @@ export const standard = {
196209
remove: nonBreaking,
197210
edit: nonBreaking,
198211
},
212+
'/channels/*/subscribe/tags/*': {
213+
add: nonBreaking,
214+
remove: nonBreaking,
215+
edit: nonBreaking,
216+
},
199217
'/channels/*/subscribe/externalDocs': {
200218
add: nonBreaking,
201219
remove: nonBreaking,
@@ -211,6 +229,11 @@ export const standard = {
211229
remove: breaking,
212230
edit: breaking,
213231
},
232+
'/channels/*/subscribe/traits/*': {
233+
add: nonBreaking,
234+
remove: breaking,
235+
edit: breaking,
236+
},
214237
'/channels/*/subscribe/traits/operationId': {
215238
add: nonBreaking,
216239
remove: breaking,
@@ -231,6 +254,11 @@ export const standard = {
231254
remove: nonBreaking,
232255
edit: nonBreaking,
233256
},
257+
'/channels/*/subscribe/traits/tags/*': {
258+
add: nonBreaking,
259+
remove: nonBreaking,
260+
edit: nonBreaking,
261+
},
234262
'/channels/*/subscribe/traits/externalDocs': {
235263
add: nonBreaking,
236264
remove: nonBreaking,
@@ -296,6 +324,11 @@ export const standard = {
296324
remove: nonBreaking,
297325
edit: nonBreaking,
298326
},
327+
'/channels/*/subscribe/message/tags/*': {
328+
add: nonBreaking,
329+
remove: nonBreaking,
330+
edit: nonBreaking,
331+
},
299332
'/channels/*/subscribe/message/externalDocs': {
300333
add: nonBreaking,
301334
remove: nonBreaking,
@@ -311,11 +344,21 @@ export const standard = {
311344
remove: nonBreaking,
312345
edit: nonBreaking,
313346
},
347+
'/channels/*/subscribe/message/examples/*': {
348+
add: nonBreaking,
349+
remove: nonBreaking,
350+
edit: nonBreaking,
351+
},
314352
'/channels/*/subscribe/message/traits': {
315353
add: nonBreaking,
316354
remove: breaking,
317355
edit: breaking,
318356
},
357+
'/channels/*/subscribe/message/traits/*': {
358+
add: nonBreaking,
359+
remove: breaking,
360+
edit: breaking,
361+
},
319362
'/channels/*/subscribe/message/traits/headers': {
320363
add: unclassified,
321364
remove: unclassified,
@@ -366,6 +409,11 @@ export const standard = {
366409
remove: nonBreaking,
367410
edit: nonBreaking,
368411
},
412+
'/channels/*/subscribe/message/traits/tags/*': {
413+
add: nonBreaking,
414+
remove: nonBreaking,
415+
edit: nonBreaking,
416+
},
369417
'/channels/*/subscribe/message/traits/externalDocs': {
370418
add: nonBreaking,
371419
remove: nonBreaking,
@@ -381,6 +429,11 @@ export const standard = {
381429
remove: nonBreaking,
382430
edit: nonBreaking,
383431
},
432+
'/channels/*/subscribe/message/traits/examples/*': {
433+
add: nonBreaking,
434+
remove: nonBreaking,
435+
edit: nonBreaking,
436+
},
384437
'/channels/*/subscribe/message/description': {
385438
add: nonBreaking,
386439
remove: nonBreaking,
@@ -416,6 +469,11 @@ export const standard = {
416469
remove: nonBreaking,
417470
edit: nonBreaking,
418471
},
472+
'/channels/*/publish/tags/*': {
473+
add: nonBreaking,
474+
remove: nonBreaking,
475+
edit: nonBreaking,
476+
},
419477
'/channels/*/publish/externalDocs': {
420478
add: nonBreaking,
421479
remove: nonBreaking,
@@ -431,6 +489,11 @@ export const standard = {
431489
remove: breaking,
432490
edit: breaking,
433491
},
492+
'/channels/*/publish/traits/*': {
493+
add: nonBreaking,
494+
remove: breaking,
495+
edit: breaking,
496+
},
434497
'/channels/*/publish/traits/operationId': {
435498
add: nonBreaking,
436499
remove: breaking,
@@ -451,6 +514,11 @@ export const standard = {
451514
remove: nonBreaking,
452515
edit: nonBreaking,
453516
},
517+
'/channels/*/publish/traits/tags/*': {
518+
add: nonBreaking,
519+
remove: nonBreaking,
520+
edit: nonBreaking,
521+
},
454522
'/channels/*/publish/traits/externalDocs': {
455523
add: nonBreaking,
456524
remove: nonBreaking,
@@ -516,6 +584,11 @@ export const standard = {
516584
remove: nonBreaking,
517585
edit: nonBreaking,
518586
},
587+
'/channels/*/publish/message/tags/*': {
588+
add: nonBreaking,
589+
remove: nonBreaking,
590+
edit: nonBreaking,
591+
},
519592
'/channels/*/publish/message/externalDocs': {
520593
add: nonBreaking,
521594
remove: nonBreaking,
@@ -531,11 +604,21 @@ export const standard = {
531604
remove: nonBreaking,
532605
edit: nonBreaking,
533606
},
607+
'/channels/*/publish/message/examples/*': {
608+
add: nonBreaking,
609+
remove: nonBreaking,
610+
edit: nonBreaking,
611+
},
534612
'/channels/*/publish/message/traits': {
535613
add: nonBreaking,
536614
remove: breaking,
537615
edit: breaking,
538616
},
617+
'/channels/*/publish/message/traits/*': {
618+
add: nonBreaking,
619+
remove: breaking,
620+
edit: breaking,
621+
},
539622
'/channels/*/publish/message/traits/headers': {
540623
add: unclassified,
541624
remove: unclassified,
@@ -586,6 +669,11 @@ export const standard = {
586669
remove: nonBreaking,
587670
edit: nonBreaking,
588671
},
672+
'/channels/*/publish/message/traits/tags/*': {
673+
add: nonBreaking,
674+
remove: nonBreaking,
675+
edit: nonBreaking,
676+
},
589677
'/channels/*/publish/message/traits/externalDocs': {
590678
add: nonBreaking,
591679
remove: nonBreaking,
@@ -601,6 +689,11 @@ export const standard = {
601689
remove: nonBreaking,
602690
edit: nonBreaking,
603691
},
692+
'/channels/*/publish/message/traits/examples/*': {
693+
add: nonBreaking,
694+
remove: nonBreaking,
695+
edit: nonBreaking,
696+
},
604697
'/channels/*/publish/message/description': {
605698
add: nonBreaking,
606699
remove: nonBreaking,

‎src/types.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { ReplaceOperation, AddOperation } from 'fast-json-patch';
22

33
import { standard } from './standard';
4+
import { breaking, nonBreaking, unclassified } from './constants';
45

56
export type ActionType = 'add' | 'remove' | 'edit';
67

7-
export type ChangeType = 'breaking' | 'non-breaking' | 'unclassified';
8+
export type ChangeType =
9+
| typeof breaking
10+
| typeof nonBreaking
11+
| typeof unclassified;
812

913
export interface Classifier {
1014
add: ChangeType;
@@ -38,6 +42,5 @@ export interface OverrideObject {
3842
export type OverrideStandard = StandardType & OverrideObject;
3943

4044
export interface Config {
41-
parse?: boolean;
4245
override?: OverrideObject;
4346
}

‎test/fixtures/main.fixtures.ts

+314
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
/* eslint-disable sonarjs/no-duplicate-string */
2+
export const breakingChanges = [
3+
{
4+
action: 'remove',
5+
path: '/channels/mychannel',
6+
type: 'breaking',
7+
before: {
8+
publish: {
9+
message: {
10+
headers: {
11+
properties: {
12+
'some-common-header': {
13+
type: 'string',
14+
'x-parser-schema-id': '<anonymous-schema-2>',
15+
},
16+
},
17+
type: 'object',
18+
'x-parser-schema-id': '<anonymous-schema-1>',
19+
},
20+
schemaFormat: 'application/vnd.aai.asyncapi;version=2.0.0',
21+
'x-parser-message-name': 'channelMessage',
22+
'x-parser-message-parsed': true,
23+
'x-parser-original-traits': [
24+
{
25+
headers: {
26+
properties: {
27+
'some-common-header': {
28+
type: 'string',
29+
},
30+
},
31+
type: 'object',
32+
},
33+
'x-some-extension': 'some extension',
34+
},
35+
],
36+
'x-some-extension': 'some extension',
37+
},
38+
},
39+
},
40+
},
41+
{
42+
action: 'edit',
43+
path: '/info/version',
44+
type: 'breaking',
45+
after: '1.1.0',
46+
before: '1.0.0',
47+
},
48+
];
49+
50+
export const nonBreakingChanges = [
51+
{
52+
action: 'add',
53+
path: '/channels/anotherChannel',
54+
type: 'non-breaking',
55+
after: {
56+
publish: {
57+
message: {
58+
headers: {
59+
properties: {
60+
'some-common-header': {
61+
type: 'string',
62+
'x-parser-schema-id': '<anonymous-schema-2>',
63+
},
64+
},
65+
type: 'object',
66+
'x-parser-schema-id': '<anonymous-schema-1>',
67+
},
68+
schemaFormat: 'application/vnd.aai.asyncapi;version=2.0.0',
69+
'x-parser-message-name': 'channelMessage',
70+
'x-parser-message-parsed': true,
71+
'x-parser-original-traits': [
72+
{
73+
headers: {
74+
properties: {
75+
'some-common-header': {
76+
type: 'string',
77+
},
78+
},
79+
type: 'object',
80+
},
81+
'x-some-extension': 'some extension',
82+
},
83+
],
84+
'x-some-extension': 'some extension',
85+
},
86+
},
87+
},
88+
},
89+
];
90+
91+
export const diffOutput = {
92+
changes: [
93+
{
94+
action: 'remove',
95+
path: '/channels/mychannel',
96+
type: 'breaking',
97+
before: {
98+
publish: {
99+
message: {
100+
headers: {
101+
properties: {
102+
'some-common-header': {
103+
type: 'string',
104+
'x-parser-schema-id': '<anonymous-schema-2>',
105+
},
106+
},
107+
type: 'object',
108+
'x-parser-schema-id': '<anonymous-schema-1>',
109+
},
110+
schemaFormat: 'application/vnd.aai.asyncapi;version=2.0.0',
111+
'x-parser-message-name': 'channelMessage',
112+
'x-parser-message-parsed': true,
113+
'x-parser-original-traits': [
114+
{
115+
headers: {
116+
properties: {
117+
'some-common-header': {
118+
type: 'string',
119+
},
120+
},
121+
type: 'object',
122+
},
123+
'x-some-extension': 'some extension',
124+
},
125+
],
126+
'x-some-extension': 'some extension',
127+
},
128+
},
129+
},
130+
},
131+
{
132+
action: 'add',
133+
path: '/channels/anotherChannel',
134+
type: 'non-breaking',
135+
after: {
136+
publish: {
137+
message: {
138+
headers: {
139+
properties: {
140+
'some-common-header': {
141+
type: 'string',
142+
'x-parser-schema-id': '<anonymous-schema-2>',
143+
},
144+
},
145+
type: 'object',
146+
'x-parser-schema-id': '<anonymous-schema-1>',
147+
},
148+
schemaFormat: 'application/vnd.aai.asyncapi;version=2.0.0',
149+
'x-parser-message-name': 'channelMessage',
150+
'x-parser-message-parsed': true,
151+
'x-parser-original-traits': [
152+
{
153+
headers: {
154+
properties: {
155+
'some-common-header': {
156+
type: 'string',
157+
},
158+
},
159+
type: 'object',
160+
},
161+
'x-some-extension': 'some extension',
162+
},
163+
],
164+
'x-some-extension': 'some extension',
165+
},
166+
},
167+
},
168+
},
169+
{
170+
action: 'edit',
171+
path: '/info/version',
172+
type: 'breaking',
173+
after: '1.1.0',
174+
before: '1.0.0',
175+
},
176+
],
177+
};
178+
179+
export const overrides = {
180+
'/channels/*': {
181+
add: 'breaking',
182+
remove: 'non-breaking',
183+
edit: 'unclassified',
184+
},
185+
'/info/version': {
186+
add: 'breaking',
187+
remove: 'non-breaking',
188+
edit: 'unclassified',
189+
},
190+
};
191+
192+
export const changesWithOverrides = {
193+
changes: [
194+
{
195+
action: 'remove',
196+
path: '/channels/mychannel',
197+
type: 'non-breaking',
198+
before: {
199+
publish: {
200+
message: {
201+
headers: {
202+
properties: {
203+
'some-common-header': {
204+
type: 'string',
205+
'x-parser-schema-id': '<anonymous-schema-2>',
206+
},
207+
},
208+
type: 'object',
209+
'x-parser-schema-id': '<anonymous-schema-1>',
210+
},
211+
schemaFormat: 'application/vnd.aai.asyncapi;version=2.0.0',
212+
'x-parser-message-name': 'channelMessage',
213+
'x-parser-message-parsed': true,
214+
'x-parser-original-traits': [
215+
{
216+
headers: {
217+
properties: {
218+
'some-common-header': {
219+
type: 'string',
220+
},
221+
},
222+
type: 'object',
223+
},
224+
'x-some-extension': 'some extension',
225+
},
226+
],
227+
'x-some-extension': 'some extension',
228+
},
229+
},
230+
},
231+
},
232+
{
233+
action: 'add',
234+
path: '/channels/anotherChannel',
235+
type: 'breaking',
236+
after: {
237+
publish: {
238+
message: {
239+
headers: {
240+
properties: {
241+
'some-common-header': {
242+
type: 'string',
243+
'x-parser-schema-id': '<anonymous-schema-2>',
244+
},
245+
},
246+
type: 'object',
247+
'x-parser-schema-id': '<anonymous-schema-1>',
248+
},
249+
schemaFormat: 'application/vnd.aai.asyncapi;version=2.0.0',
250+
'x-parser-message-name': 'channelMessage',
251+
'x-parser-message-parsed': true,
252+
'x-parser-original-traits': [
253+
{
254+
headers: {
255+
properties: {
256+
'some-common-header': {
257+
type: 'string',
258+
},
259+
},
260+
type: 'object',
261+
},
262+
'x-some-extension': 'some extension',
263+
},
264+
],
265+
'x-some-extension': 'some extension',
266+
},
267+
},
268+
},
269+
},
270+
{
271+
action: 'edit',
272+
path: '/info/version',
273+
type: 'unclassified',
274+
after: '1.1.0',
275+
before: '1.0.0',
276+
},
277+
],
278+
};
279+
280+
export const specDocument1 = {
281+
servers: {
282+
google: {
283+
variables: {
284+
port: {
285+
enum: [1, 2],
286+
},
287+
},
288+
},
289+
},
290+
};
291+
292+
export const specDocument2 = {
293+
servers: {
294+
google: {
295+
variables: {
296+
port: {
297+
enum: [1],
298+
},
299+
},
300+
},
301+
},
302+
};
303+
304+
export const arrayChanges = {
305+
changes: [
306+
{
307+
path: '/servers/google/variables/port/enum/1',
308+
type: 'breaking',
309+
isArrayIndex: true,
310+
action: 'remove',
311+
before: 2,
312+
},
313+
],
314+
};

‎test/fixtures/mergeStandard.fixture.ts

+3-3
Large diffs are not rendered by default.

‎test/diff.spec.ts ‎test/generateDiff.spec.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { parse } from '@asyncapi/parser';
22
import { readFileSync } from 'fs';
33
import { resolve } from 'path';
44

5-
import diff from '../src/diff';
5+
import generateDiff from '../src/generateDiff';
66

77
import {
88
firstDocument,
@@ -13,11 +13,13 @@ import {
1313

1414
describe('Diff', () => {
1515
test('Check if diff is an empty array for same inputs', () => {
16-
expect(diff(firstDocument, firstDocument)).toStrictEqual([]);
16+
expect(generateDiff(firstDocument, firstDocument)).toStrictEqual([]);
1717
});
1818

1919
test('Check diff output with local inputs', () => {
20-
expect(diff(firstDocument, secondDocument)).toStrictEqual(diffLocalOutput);
20+
expect(generateDiff(firstDocument, secondDocument)).toStrictEqual(
21+
diffLocalOutput
22+
);
2123
});
2224

2325
test('Check diff output through parser with no difference', async () => {
@@ -26,7 +28,9 @@ describe('Diff', () => {
2628
'utf-8'
2729
);
2830
const firstDocument = await parse(specDocument);
29-
expect(diff(firstDocument.json(), firstDocument.json())).toStrictEqual([]);
31+
expect(
32+
generateDiff(firstDocument.json(), firstDocument.json())
33+
).toStrictEqual([]);
3034
});
3135

3236
test('Check diff output through parser with difference input', async () => {
@@ -40,8 +44,8 @@ describe('Diff', () => {
4044
);
4145
const firstDocument = await parse(firstSpecDocument);
4246
const secondDocument = await parse(secondSpecDocument);
43-
expect(diff(firstDocument.json(), secondDocument.json())).toStrictEqual(
44-
diffOutput
45-
);
47+
expect(
48+
generateDiff(firstDocument.json(), secondDocument.json())
49+
).toStrictEqual(diffOutput);
4650
});
4751
});

‎test/main.spec.ts

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { parse } from '@asyncapi/parser';
2+
import { readFileSync } from 'fs';
3+
import { resolve } from 'path';
4+
5+
import AsyncAPIDiff from '../src/asyncapidiff';
6+
import { diff } from '../src/main';
7+
import { OverrideObject } from '../src/types';
8+
9+
import {
10+
diffOutput,
11+
breakingChanges,
12+
nonBreakingChanges,
13+
overrides,
14+
changesWithOverrides,
15+
specDocument1,
16+
specDocument2,
17+
arrayChanges,
18+
} from './fixtures/main.fixtures';
19+
20+
describe('main function', () => {
21+
test('runs the diff function', async () => {
22+
const firstSpecDocument = readFileSync(
23+
resolve('./test/spec/asyncapi.yml'),
24+
'utf-8'
25+
);
26+
const secondSpecDocument = readFileSync(
27+
resolve('./test/spec/diffSpec.yml'),
28+
'utf-8'
29+
);
30+
const firstDocument = await parse(firstSpecDocument);
31+
const secondDocument = await parse(secondSpecDocument);
32+
const output = diff(firstDocument.json(), secondDocument.json());
33+
expect(output).toBeInstanceOf(AsyncAPIDiff);
34+
expect(output.getOutput()).toEqual(diffOutput);
35+
expect(output.breaking()).toEqual(breakingChanges);
36+
expect(output.nonBreaking()).toEqual(nonBreakingChanges);
37+
});
38+
39+
test('runs the diff function with empty spec', () => {
40+
const firstSpec = {};
41+
const secondSpec = {};
42+
expect(diff(firstSpec, secondSpec).getOutput()).toEqual({
43+
changes: [],
44+
});
45+
});
46+
47+
test('runs the diff function with overrided changes', async () => {
48+
const firstSpecDocument = readFileSync(
49+
resolve('./test/spec/asyncapi.yml'),
50+
'utf-8'
51+
);
52+
const secondSpecDocument = readFileSync(
53+
resolve('./test/spec/diffSpec.yml'),
54+
'utf-8'
55+
);
56+
const firstDocument = await parse(firstSpecDocument);
57+
const secondDocument = await parse(secondSpecDocument);
58+
const output = diff(firstDocument.json(), secondDocument.json(), {
59+
override: overrides as OverrideObject,
60+
});
61+
expect(output.getOutput()).toEqual(changesWithOverrides);
62+
});
63+
64+
test('checks output with array changes', () => {
65+
const output = diff(specDocument1, specDocument2);
66+
expect(output.getOutput()).toEqual(arrayChanges);
67+
});
68+
});

0 commit comments

Comments
 (0)
Please sign in to comment.