Skip to content

Commit 15489c2

Browse files
committed
Add jest and some starter tests (one fails)
1 parent 59b7496 commit 15489c2

File tree

6 files changed

+211
-1
lines changed

6 files changed

+211
-1
lines changed

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "node_modules/.bin/jest"
88
},
99
"type": "module",
1010
"author": "",
@@ -14,5 +14,17 @@
1414
"jsonpath": "^1.1.1",
1515
"mergician": "^1.0.3",
1616
"openapi-format": "^1.13.0"
17+
},
18+
"devDependencies": {
19+
"@babel/core": "^7.20.12",
20+
"@babel/preset-env": "^7.20.2",
21+
"babel-jest": "^29.4.1",
22+
"jest": "^29.4.1"
23+
},
24+
"jest": {
25+
"transform": {
26+
"^.+\\.js$": "babel-jest"
27+
},
28+
"testEnvironment": "node"
1729
}
1830
}

test/expected/output1.yaml

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: 'http://petstore.swagger.io/v1'
9+
paths:
10+
/pets:
11+
get:
12+
summary: {}
13+
operationId: listPets
14+
tags:
15+
- pets
16+
- wildcard-done
17+
- overlayed
18+
parameters:
19+
- name: limit
20+
in: query
21+
description: How many items to return at one time (max 100)
22+
required: false
23+
schema:
24+
type: integer
25+
maximum: 100
26+
format: int32
27+
responses:
28+
'200':
29+
description: A paged array of pets
30+
headers:
31+
x-next:
32+
description: A link to the next page of responses
33+
schema:
34+
type: string
35+
content:
36+
application/json:
37+
schema:
38+
$ref: '#/components/schemas/Pets'
39+
default:
40+
description: unexpected error
41+
content:
42+
application/json:
43+
schema:
44+
$ref: '#/components/schemas/Error'
45+
info:
46+
x-overlay-applied: structured-overlay
47+
description: This is an added description
48+
post:
49+
foo:
50+
bar: hello
51+
'/pets/{petId}':
52+
get:
53+
summary: Info for a specific pet
54+
operationId: showPetById
55+
tags:
56+
- pets
57+
- wildcard-done
58+
parameters:
59+
- name: petId
60+
in: path
61+
required: true
62+
description: The id of the pet to retrieve
63+
schema:
64+
type: string
65+
responses:
66+
'200':
67+
description: Expected response to a valid request
68+
content:
69+
application/json:
70+
schema:
71+
$ref: '#/components/schemas/Pet'
72+
default:
73+
description: unexpected error
74+
content:
75+
application/json:
76+
schema:
77+
$ref: '#/components/schemas/Error'
78+
post:
79+
foo:
80+
bar: hello
81+
components:
82+
schemas:
83+
Pet:
84+
type: object
85+
required:
86+
- id
87+
- name
88+
properties:
89+
id:
90+
type: integer
91+
format: int64
92+
name:
93+
type: string
94+
tag:
95+
type: string
96+
Pets:
97+
type: array
98+
maxItems: 100
99+
items:
100+
$ref: '#/components/schemas/Pet'
101+
Error:
102+
type: object
103+
required:
104+
- code
105+
- message
106+
properties:
107+
code:
108+
type: integer
109+
format: int32
110+
message:
111+
type: string
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
openapi: "3.1.0"
2+
info:
3+
version: 1.0.0
4+
title: Imaginary town
5+
servers:
6+
- url: https://example.com
7+
description: Example server
8+
paths:
9+
'/buildings':
10+
get:
11+
summary: "All of the available buildings"
12+
operationId: buildingsAll
13+
description: "This is the summary of getting the buildings"
14+
15+
responses:
16+
'200':
17+
description: Return all known buildings
18+
content:
19+
application/json:
20+
schema:
21+
type: object
22+
properties:
23+
message:
24+
type: string
25+
example: "house"
26+

test/openapi/town.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
openapi: "3.1.0"
2+
info:
3+
version: 1.0.0
4+
title: Imaginary town
5+
servers:
6+
- url: https://example.com
7+
description: Example server
8+
paths:
9+
'/buildings':
10+
get:
11+
summary: All buildings
12+
operationId: buildingsAll
13+
responses:
14+
'200':
15+
description: Return all known buildings
16+
content:
17+
application/json:
18+
schema:
19+
type: object
20+
properties:
21+
message:
22+
type: string
23+
example: "house"
24+

test/overlay.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {overlayFiles} from '../src/overlay.js'
2+
import fs from 'fs';
3+
4+
5+
test('apply an overlay and check the output', () => {
6+
const openapiFile = "test/openapi/petstore.yaml";
7+
const overlayFile = "test/overlays/overlay.yaml";
8+
const expectedFile = "test/expected/output1.yaml";
9+
const expectedOutput = fs.readFileSync(expectedFile, 'utf8');
10+
11+
const result = overlayFiles(openapiFile, overlayFile);
12+
13+
expect(result).toEqual(expectedOutput);
14+
});
15+
16+
test('apply an overlay and check the output', () => {
17+
const openapiFile = "test/openapi/town.yaml";
18+
const overlayFile = "test/overlays/building-description.yaml";
19+
const expectedFile = "test/expected/town-building-description.yaml";
20+
const expectedOutput = fs.readFileSync(expectedFile, 'utf8');
21+
22+
const result = overlayFiles(openapiFile, overlayFile);
23+
24+
expect(result).toEqual(expectedOutput);
25+
});
26+
27+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
overlay: 1.0.0
2+
info:
3+
title: Add a building endpoint description
4+
version: 1.0.0
5+
actions:
6+
- target: $.paths['/buildings'].get
7+
update:
8+
description: "This is the summary of getting the buildings"
9+
summary: "All of the available buildings"
10+

0 commit comments

Comments
 (0)