Skip to content

Commit

Permalink
test: runTest jest test, user-friendly error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Avishag Israeli committed Oct 18, 2020
1 parent 028d178 commit 5acc55e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib/snyk-test/run-test.ts
Expand Up @@ -159,7 +159,8 @@ export async function runTest(

const hasFailedToGetVulnerabilities =
error.code === 404 &&
error.name.includes('FailedToGetVulnerabilitiesError');
error.name.includes('FailedToGetVulnerabilitiesError') &&
!error.userMessage;

if (isFeatureNotAllowed) {
throw NoSupportedManifestsFoundError([root]);
Expand Down
70 changes: 70 additions & 0 deletions test/run-test.spec.ts
@@ -0,0 +1,70 @@
import { runTest } from '../src/lib/snyk-test/run-test';
import * as optionsValidator from '../src/lib/options-validator';
import { CustomError } from '../src/lib/errors/custom-error';
import { Options, TestOptions } from '../src/lib/types';

describe('CLI runTest - propagate correct user error', () => {
it('returns userMessage for a default error code', async () => {
jest.spyOn(optionsValidator, 'validateOptions').mockImplementation(() => {
const err = new CustomError('test');
err.userMessage = 'just a random error';
throw err;
});

// Dummy options in order to call to runTest
const options: Options & TestOptions = {
path: '',
traverseNodeModules: false,
interactive: false,
showVulnPaths: 'none',
};

await expect(runTest(undefined, '', options)).rejects.toThrow(
'just a random error',
);
});

it('returns userMessage for error code 404', async () => {
jest.spyOn(optionsValidator, 'validateOptions').mockImplementation(() => {
const err = new CustomError('FailedToGetVulnerabilitiesError');
err.name = 'FailedToGetVulnerabilitiesError';
err.code = 404;
err.userMessage = 'this is error 404';
throw err;
});

// Dummy options in order to call to runTest
const options: Options & TestOptions = {
path: '',
traverseNodeModules: false,
interactive: false,
showVulnPaths: 'none',
};

await expect(runTest(undefined, '', options)).rejects.toThrow(
'this is error 404',
);
});

it('returns right message for error code 403', async () => {
jest.spyOn(optionsValidator, 'validateOptions').mockImplementation(() => {
const err = new CustomError('Feature not allowed');
err.name = 'Feature not allowed';
err.code = 403;
err.userMessage = 'this is error 403';
throw err;
});

// Dummy options in order to call to runTest
const options: Options & TestOptions = {
path: '',
traverseNodeModules: false,
interactive: false,
showVulnPaths: 'none',
};

await expect(runTest(undefined, '', options)).rejects.toThrow(
'Could not detect supported target files',
);
});
});

0 comments on commit 5acc55e

Please sign in to comment.