Skip to content

Commit

Permalink
test: mock analysis report tests with nock at the API level
Browse files Browse the repository at this point in the history
  • Loading branch information
novalex committed Jul 14, 2023
1 parent bea4ee8 commit c1799f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"jest": "^26.4.2",
"jest-extended": "^0.8.0",
"jsonschema": "^1.2.11",
"nock": "^13.3.2",
"npm-run-all": "^4.1.5",
"prettier": "^2.1.1",
"ts-jest": "^26.3.0",
Expand Down
32 changes: 22 additions & 10 deletions tests/analysis.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import nock from 'nock';
import jsonschema from 'jsonschema';

import { analyzeFolders, extendAnalysis, analyzeBundle, analyzeScmProject } from '../src/analysis';
Expand Down Expand Up @@ -349,11 +350,14 @@ describe('Functional test of analysis', () => {
);
});

it('should successfully analyze folder with the report option enabled', async () => {
const mockReportBundle = jest.spyOn(report, 'reportBundle');
mockReportBundle.mockResolvedValueOnce(getReportReturn);
it('should successfully analyze folder and report results with the report option enabled', async () => {
nock(baseURL, { allowUnmocked: true })
.post('/report')
.reply(200, { reportId: 'report-id' })
.get('/report/report-id')
.reply(200, getReportReturn);

const bundle = await analyzeFolders({
const result = await analyzeFolders({
connection: { baseURL, sessionToken, source },
analysisOptions: { severity: AnalysisSeverity.info },
fileOptions: {
Expand All @@ -366,8 +370,12 @@ describe('Functional test of analysis', () => {
},
});

expect(mockReportBundle).toHaveBeenCalledTimes(1);
expect(bundle).toBeTruthy();
nock.cleanAll();

expect(result).not.toBeNull();
expect(result).toHaveProperty('fileBundle');
expect(result).toHaveProperty('analysisResults');
expect(result).toHaveProperty('reportResults');
});

it('should successfully analyze folder but not report with the report option disabled', async () => {
Expand All @@ -388,9 +396,12 @@ describe('Functional test of analysis', () => {
});

describe('analyzeScmProject', () => {
it('should successfully analyze SCM project', async () => {
const mockReportScm = jest.spyOn(report, 'reportScm');
mockReportScm.mockResolvedValueOnce(getReportReturn);
it('should successfully analyze SCM project and report results', async () => {
nock(baseURL, { allowUnmocked: true })
.post('/test')
.reply(200, { testId: 'test-id' })
.get('/test/test-id')
.reply(200, getReportReturn);

const result = await analyzeScmProject({
connection: { baseURL, sessionToken, source },
Expand All @@ -401,7 +412,8 @@ describe('Functional test of analysis', () => {
},
});

expect(mockReportScm).toHaveBeenCalledTimes(1);
nock.cleanAll();

expect(result).not.toBeNull();
expect(result).toHaveProperty('analysisResults');
expect(result).toHaveProperty('reportResults');
Expand Down

0 comments on commit c1799f1

Please sign in to comment.