|
| 1 | +const test = require('uvu').test; |
| 2 | +const assert = require('uvu/assert'); |
| 3 | +const pactum = require('pactum'); |
| 4 | +const reporter = pactum.reporter; |
| 5 | +const mock = pactum.mock; |
| 6 | +const request = pactum.request; |
| 7 | +const handler = pactum.handler; |
| 8 | + |
| 9 | +const psc = require('../src/index'); |
| 10 | + |
| 11 | +test.before(() => { |
| 12 | + psc.swaggerYamlPath = './tests/testObjects/openapi3.yaml'; |
| 13 | + psc.basePath = '/api/server/v2' |
| 14 | + psc.reportFile = 'report-openapi3.json' |
| 15 | + reporter.add(psc); |
| 16 | + request.setBaseUrl('http://localhost:9393'); |
| 17 | + handler.addInteractionHandler('get all ninjas', () => { |
| 18 | + return { |
| 19 | + request: { |
| 20 | + method: 'GET', |
| 21 | + path: '/api/server/v2/getallninjas' |
| 22 | + }, |
| 23 | + response: { |
| 24 | + status: 200 |
| 25 | + } |
| 26 | + } |
| 27 | + }); |
| 28 | + handler.addInteractionHandler('get ninjas by rank', (ctx) => { |
| 29 | + return { |
| 30 | + request: { |
| 31 | + method: 'GET', |
| 32 | + path: `/api/server/v2/getninjas/${ctx.data}` |
| 33 | + }, |
| 34 | + response: { |
| 35 | + status: 200 |
| 36 | + } |
| 37 | + } |
| 38 | + }); |
| 39 | + handler.addInteractionHandler('get ninja by rank and name', (ctx) => { |
| 40 | + return { |
| 41 | + request: { |
| 42 | + method: 'GET', |
| 43 | + path: `/api/server/v2/getninja/${ctx.data.rank}/${ctx.data.name}` |
| 44 | + }, |
| 45 | + response: { |
| 46 | + status: 200 |
| 47 | + } |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + handler.addInteractionHandler('get health', () => { |
| 52 | + return { |
| 53 | + request: { |
| 54 | + method: 'GET', |
| 55 | + path: `/api/server/v2/health` |
| 56 | + }, |
| 57 | + response: { |
| 58 | + status: 200 |
| 59 | + } |
| 60 | + } |
| 61 | + }); |
| 62 | + return mock.start(); |
| 63 | +}); |
| 64 | + |
| 65 | +test.after(() => { |
| 66 | + return mock.stop(); |
| 67 | +}); |
| 68 | + |
| 69 | +test('spec passed', async () => { |
| 70 | + await pactum.spec() |
| 71 | + .useInteraction('get all ninjas') |
| 72 | + .get('/api/server/v2/getallninjas') |
| 73 | + .expectStatus(200); |
| 74 | +}); |
| 75 | + |
| 76 | +test('spec passed - additional path params', async () => { |
| 77 | + await pactum.spec() |
| 78 | + .useInteraction('get ninjas by rank', "jounin") |
| 79 | + .get('/api/server/v2/getninjas/jounin') |
| 80 | + .expectStatus(200); |
| 81 | +}); |
| 82 | + |
| 83 | +test('spec passed - no path params', async () => { |
| 84 | + await pactum.spec() |
| 85 | + .useInteraction('get health') |
| 86 | + .get('/api/server/v2/health') |
| 87 | + .expectStatus(200); |
| 88 | +}); |
| 89 | + |
| 90 | +test('spec passed - different api path with path params', async () => { |
| 91 | + await pactum.spec() |
| 92 | + .useInteraction('get ninja by rank and name', {rank: "jounin", name: "kakashi"}) |
| 93 | + .get('/api/server/v2/getninja/jounin/kakashi') |
| 94 | + .expectStatus(200); |
| 95 | +}); |
| 96 | + |
| 97 | +test('spec failed', async () => { |
| 98 | + try { |
| 99 | + await pactum.spec() |
| 100 | + .get('/api/server/v2/getallninjas') |
| 101 | + .expectStatus(200); |
| 102 | + } catch (error) { |
| 103 | + console.log(error); |
| 104 | + } |
| 105 | +}); |
| 106 | + |
| 107 | +test('run reporter', async () => { |
| 108 | + await reporter.end(); |
| 109 | +}); |
| 110 | + |
| 111 | +test('validate json reporter', async () => { |
| 112 | + const report = require('../reports/report-openapi3.json'); |
| 113 | + console.log(JSON.stringify(report, null, 2)); |
| 114 | + assert.equal(Object.keys(report).length, 7); |
| 115 | + assert.equal(report.hasOwnProperty("basePath"), true) |
| 116 | + assert.equal(report.hasOwnProperty("coverage"), true) |
| 117 | + assert.equal(report.hasOwnProperty("coveredApiCount"), true) |
| 118 | + assert.equal(report.hasOwnProperty("missedApiCount"), true) |
| 119 | + assert.equal(report.hasOwnProperty("totalApiCount"), true) |
| 120 | + assert.equal(report.hasOwnProperty("coveredApiList"), true) |
| 121 | + assert.equal(report.hasOwnProperty("missedApiList"), true) |
| 122 | + assert.equal(report.coverage, 0.57); |
| 123 | + assert.equal(report.coveredApiCount, 4); |
| 124 | + assert.equal(report.missedApiCount, 3); |
| 125 | + assert.equal(report.totalApiCount, 7); |
| 126 | + assert.equal(report.coveredApiList.length, 4); |
| 127 | + assert.equal(report.missedApiList.length, 3); |
| 128 | +}); |
| 129 | + |
| 130 | +test.run(); |
0 commit comments