Skip to content

Commit

Permalink
Merge pull request #1462 from snyk/test/runTest-test
Browse files Browse the repository at this point in the history
feat: better error message propagated if available
  • Loading branch information
Avishagp committed Oct 19, 2020
2 parents ac87ce0 + e1822cf commit c1b420b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/lib/snyk-test/run-test.ts
Expand Up @@ -309,7 +309,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
4 changes: 2 additions & 2 deletions test/acceptance/cli-test/cli-test.generic.spec.ts
Expand Up @@ -40,7 +40,7 @@ export const GenericTests: AcceptanceTests = {
} catch (err) {
t.equal(
err.userMessage,
"We couldn't test npm-package. Please check the version and package name and try running `snyk test` again.\nFor additional assistance, run `snyk help` or check out our docs \n(link to: https://support.snyk.io/hc/en-us/articles/360003851277#UUID-ba99a73f-110d-1f1d-9e7a-1bad66bf0996).",
'cli error message',
'got correct err message',
);
}
Expand All @@ -58,7 +58,7 @@ export const GenericTests: AcceptanceTests = {
} catch (err) {
t.equal(
err.userMessage,
"We couldn't test ruby-app. Please check the version and package name and try running `snyk test` again.\nFor additional assistance, run `snyk help` or check out our docs \n(link to: https://support.snyk.io/hc/en-us/articles/360003851277#UUID-ba99a73f-110d-1f1d-9e7a-1bad66bf0996).",
'cli error message',
'got correct err message',
);
}
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 c1b420b

Please sign in to comment.