Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add jest test for runTest #1461

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions test/run-test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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.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.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',
);
});
});