-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathPortman.test.ts
More file actions
413 lines (340 loc) · 13 KB
/
Copy pathPortman.test.ts
File metadata and controls
413 lines (340 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import SwaggerParser from '@apidevtools/swagger-parser'
import fs from 'fs-extra'
import * as Either from 'fp-ts/lib/Either'
import { ValidationError } from '@apideck/better-ajv-errors'
import { OpenAPI } from 'openapi-types'
import { CollectionDefinition } from 'postman-collection'
import * as mockOAS from '../../__tests__/fixtures/mockOAS.json'
import * as mockPostman from '../../__tests__/fixtures/mockPostman.json'
import { IntegrationTestWriter, VariationWriter } from '../application'
import { TestSuite } from '../application/TestSuite'
import * as configLoader from '../lib/getConfig'
import { OpenApiParser } from '../oas'
import { Portman } from '../Portman'
import { PostmanParser } from '../postman'
import { OpenApiToPostmanService } from '../services'
import { PortmanOptions } from '../types'
import { omitKeys } from '../utils'
import * as validateJsonSchema from '../utils/PortmanConfig.validator'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const portmanResult = (collection: CollectionDefinition): any => {
return JSON.parse(JSON.stringify(collection))
}
const mockPostmanSync = jest.fn(() =>
Promise.resolve(JSON.stringify({ status: 'success', data: {} }))
)
jest.mock('../services/PostmanSyncService', () => {
return {
PostmanSyncService: jest.fn().mockImplementation(() => {
return {
sync: mockPostmanSync
}
})
}
})
afterAll(() => {
jest.restoreAllMocks()
})
describe('Portman', () => {
let options: PortmanOptions = {}
beforeEach(async () => {
/* eslint-disable @typescript-eslint/no-empty-function */
jest.spyOn(global.console, 'log').mockImplementation(() => {})
/* eslint-disable @typescript-eslint/no-empty-function */
jest.spyOn(global.console, 'error').mockImplementation(() => {})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
jest.spyOn(process, 'exit').mockImplementation(() => {
return undefined as never
})
// mock conversions
jest
.spyOn(SwaggerParser.prototype, 'dereference')
.mockImplementation(() => Promise.resolve(mockOAS as OpenAPI.Document))
jest
.spyOn(SwaggerParser.prototype, 'bundle')
.mockImplementation(() => Promise.resolve(mockOAS as OpenAPI.Document))
jest
.spyOn(OpenApiToPostmanService.prototype, 'convert')
.mockImplementation(() => Promise.resolve(mockPostman))
options = {
postmanUid: 'eb1ffad6-eece-456b-ad32-3f2a3f605537',
oaLocal: './__tests__/fixtures/crm.yml',
postmanConfigFile: './__tests__/fixtures/postman-config.json',
portmanConfigFile: './__tests__/fixtures/portman.crm.json',
portmanConfigPath: './__tests__/fixtures/portman.crm.json',
envFile: './__tests__/fixtures/.crm.env',
baseUrl: 'http://localhost:3050',
syncPostman: false,
includeTests: true,
runNewman: false
}
})
afterAll(() => {
jest.restoreAllMocks()
})
afterEach(() => {
jest.clearAllMocks()
})
it('should be a function', () => {
expect(Portman).toBeInstanceOf(Function)
})
it('should return a Portman instance', () => {
const portman = new Portman(options)
expect(portman).toBeInstanceOf(Portman)
})
it('should return a Portman instance with default options', () => {
const portman = new Portman({})
expect(portman).toBeInstanceOf(Portman)
})
describe('Portman.run()', () => {
it('should be a function', () => {
const portman = new Portman(options)
expect(portman.run).toBeInstanceOf(Function)
})
})
describe('Portman.before()', () => {
it('should be a function', () => {
const portman = new Portman(options)
expect(portman.before).toBeInstanceOf(Function)
})
it('should get called when calling run', async () => {
const portman = new Portman(options)
const spy = jest.spyOn(portman, 'before')
await portman.run()
expect(spy).toHaveBeenCalled()
})
it('should load config file with getConfig()', async () => {
const portman = new Portman(options)
const spy = jest.spyOn(configLoader, 'getConfig')
await portman.run()
expect(spy).toHaveBeenCalled()
})
it('should validate config file with validateJsonSchema()', async () => {
const portman = new Portman(options)
const spy = jest.spyOn(validateJsonSchema, 'validate')
await portman.run()
expect(spy).toHaveBeenCalled()
})
it('should exit with code 1 on invalid config', async () => {
const portman = new Portman(options)
const getConfigSpy = jest
.spyOn(configLoader, 'getConfig')
// return a successful config load so validation is reached
.mockReturnValueOnce(
Promise.resolve(Either.right({} as unknown as import('../types').PortmanConfig))
)
const validateSpy = jest
.spyOn(validateJsonSchema, 'validate')
// force validation to fail for this test only
.mockReturnValueOnce(Either.left([] as ValidationError[]))
await portman.run()
expect(validateSpy).toHaveBeenCalled()
expect(process.exit).toHaveBeenCalledWith(1)
// cleanup to avoid affecting subsequent tests
getConfigSpy.mockRestore()
validateSpy.mockRestore()
})
})
describe('Portman.parseOpenApiSpec()', () => {
it('should be a function', () => {
const portman = new Portman(options)
expect(portman.parseOpenApiSpec).toBeInstanceOf(Function)
})
it('should throw an error when no spec is passed', async () => {
const portman = new Portman({
portmanConfigFile: './__tests__/fixtures/portman.crm.json'
})
await expect(portman.parseOpenApiSpec()).rejects.toThrow('Error initializing OpenApi Spec.')
})
it('should throw an error when spec path does not exist', async () => {
const portman = new Portman({
oaLocal: './__tests__/fixtures/not_a_spec.yml'
})
await expect(portman.parseOpenApiSpec()).rejects.toThrow('Error initializing OpenApi Spec.')
})
it('should parse OpenAPI spec and define an OpenApiParser', async () => {
const portman = new Portman(options)
const spy = jest.spyOn(portman, 'parseOpenApiSpec')
await portman.run()
expect(spy).toHaveBeenCalled()
expect(portman.oasParser).toBeDefined()
expect(portman.oasParser).toBeInstanceOf(OpenApiParser)
})
})
describe('Portman.convertToPostmanCollection()', () => {
it('should be a function', () => {
const portman = new Portman(options)
expect(portman.convertToPostmanCollection).toBeInstanceOf(Function)
})
it('should convert OpenAPI spec to Postman collection', async () => {
const portman = new Portman(options)
const spy = jest.spyOn(portman, 'convertToPostmanCollection')
await portman.run()
expect(spy).toHaveBeenCalled()
expect(portman.postmanParser).toBeDefined()
expect(portman.postmanParser).toBeInstanceOf(PostmanParser)
})
it('should call Portman.runPortmanOverrides', async () => {
const portman = new Portman(options)
const spy = jest.spyOn(portman, 'runPortmanOverrides')
await portman.run()
expect(spy).toHaveBeenCalled()
expect(portman.portmanCollection).toBeDefined()
})
})
describe('Portman.injectTestSuite()', () => {
it('should be a function', () => {
const portman = new Portman(options)
expect(portman.injectTestSuite).toBeInstanceOf(Function)
})
it('should inject test suite into Postman collection', async () => {
const portman = new Portman(options)
const spy = jest.spyOn(portman, 'injectTestSuite')
await portman.run()
expect(spy).toHaveBeenCalled()
expect(portman.testSuite).toBeDefined()
expect(portman.testSuite).toBeInstanceOf(TestSuite)
expect(omitKeys(portman.testSuite.requestTestTypes, ['postmanItemId'])).toMatchSnapshot()
})
})
describe('Portman.injectVariationTests()', () => {
it('should be a function', () => {
const portman = new Portman(options)
expect(portman.injectVariationTests).toBeInstanceOf(Function)
})
it('should inject variation tests into Postman collection', async () => {
const portman = new Portman(options)
const spy = jest.spyOn(portman, 'injectVariationTests')
await portman.run()
expect(spy).toHaveBeenCalled()
expect(portman.variationWriter).toBeDefined()
expect(portman.variationWriter).toBeInstanceOf(VariationWriter)
expect(portman.variationWriter.variationFolder.name).toMatchSnapshot()
expect(
portmanResult(portman.portmanCollection).item.find(
item => item.name === 'Integration Tests'
)
).toBeDefined()
})
})
describe('Portman.injectVariationOverwrites()', () => {
it('should be a function', () => {
const portman = new Portman(options)
expect(portman.injectVariationOverwrites).toBeInstanceOf(Function)
})
it('should inject variation overwrites into Postman collection', async () => {
const portman = new Portman(options)
const spy = jest.spyOn(portman, 'injectVariationOverwrites')
await portman.run()
expect(spy).toHaveBeenCalled()
expect(portman.integrationTestWriter).toBeDefined()
expect(portman.integrationTestWriter).toBeInstanceOf(IntegrationTestWriter)
expect(portman.integrationTestWriter.integrationTestFolder.name).toMatchSnapshot()
expect(
portmanResult(portman.portmanCollection).item.find(
item => item.name === 'Integration Tests'
)
).toBeDefined()
})
})
describe('Portman.runPortmanOverrides()', () => {
it('should be a function', () => {
const portman = new Portman(options)
expect(portman.runPortmanOverrides).toBeInstanceOf(Function)
})
it('should run portman overrides', async () => {
const portman = new Portman(options)
const spy = jest.spyOn(portman, 'runPortmanOverrides')
await portman.run()
expect(spy).toHaveBeenCalled()
})
})
describe('Portman.moveContractTestsToFolder()', () => {
it('should be a function', () => {
const portman = new Portman(options)
expect(portman.moveContractTestsToFolder).toBeInstanceOf(Function)
})
it('should be called when bundleContractTests true', async () => {
const portman = new Portman({
...options,
bundleContractTests: true
})
const spy = jest.spyOn(portman, 'moveContractTestsToFolder')
await portman.run()
expect(spy).toHaveBeenCalled()
expect(
portmanResult(portman.portmanCollection).item.find(item => item.name === 'Contract Tests')
).toBeDefined()
})
})
describe('Portman.writePortmanCollectionToFile()', () => {
const jestCollectionFile = './tmp/jest-collection.json'
it('should be a function', () => {
const portman = new Portman(options)
expect(portman.writePortmanCollectionToFile).toBeInstanceOf(Function)
})
it('should write portman collection to file', async () => {
try {
await fs.rm(jestCollectionFile)
} catch (e) {
// ignore
}
expect(await fs.pathExists(jestCollectionFile)).toBe(false)
const portman = new Portman({
...options,
output: jestCollectionFile
})
const spy = jest.spyOn(portman, 'writePortmanCollectionToFile')
await portman.run()
expect(spy).toHaveBeenCalled()
expect(portman.portmanCollection).toBeDefined()
expect(portman.collectionFile).toBe(jestCollectionFile)
expect(await fs.pathExists(jestCollectionFile)).toBe(true)
})
})
describe('Portman.runNewmanSuite()', () => {
it('should be a function', () => {
const portman = new Portman(options)
expect(portman.runNewmanSuite).toBeInstanceOf(Function)
})
it('should run newman suite', async () => {
const portman = new Portman(options)
const spy = jest.spyOn(portman, 'runNewmanSuite')
await portman.run()
expect(spy).toHaveBeenCalled()
})
})
describe('Portman.syncCollectionToPostman()', () => {
it('should be a function', () => {
const portman = new Portman(options)
expect(portman.syncCollectionToPostman).toBeInstanceOf(Function)
})
it('should sync collection to postman', async () => {
const portman = new Portman(options)
const spy = jest.spyOn(portman, 'syncCollectionToPostman')
await portman.run()
expect(spy).toHaveBeenCalled()
})
it('should call PostmanSyncService.sync when syncPostman is true', async () => {
const portman = new Portman({
...options,
syncPostman: true
})
await portman.run()
expect(portman.postmanSyncService).toBeDefined()
expect(mockPostmanSync).toHaveBeenCalled()
})
})
describe('Portman.after()', () => {
it('should be a function', () => {
const portman = new Portman(options)
expect(portman.after).toBeInstanceOf(Function)
})
it('should run after hook', async () => {
const portman = new Portman(options)
const spy = jest.spyOn(portman, 'after')
await portman.run()
expect(spy).toHaveBeenCalled()
})
})
})