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

refactor: split test suites #5083

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
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
47 changes: 0 additions & 47 deletions test/jest/acceptance/cli-json-file-output.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,53 +32,6 @@ describe('test --json-file-output', () => {
server.close(() => done());
});

it('test with --json returns without error and with JSON return type when no vulns found', async () => {
const project = await createProjectFromWorkspace('fail-on/no-vulns');
server.setCustomResponse(await project.readJSON('vulns-result.json'));

const { code, stdout } = await runSnykCLI(`test --json`, {
cwd: project.path(),
env,
});

expect(code).toEqual(0);

expect(server.getRequests().length).toBeGreaterThanOrEqual(1);
const outputObj = JSON.parse(stdout);
expect(outputObj).not.toBe('');
});

it('test without --json returns without error and with a string return type when no vulns found', async () => {
const project = await createProjectFromWorkspace('fail-on/no-vulns');
server.setCustomResponse(await project.readJSON('vulns-result.json'));

const { code, stdout } = await runSnykCLI(`test`, {
cwd: project.path(),
env,
});

expect(code).toEqual(0);

expect(server.getRequests().length).toBeGreaterThanOrEqual(1);
expect(stdout).not.toBe('');
expect(typeof stdout).toBe('string');
});

it('test with --json throws error and error contains json output with vulnerabilities when vulns found', async () => {
const project = await createProjectFromWorkspace('fail-on/no-fixable');
server.setCustomResponse(await project.readJSON('vulns-result.json'));

const { code, stdout } = await runSnykCLI(`test --json`, {
cwd: project.path(),
env,
});

const returnedJson = JSON.parse(stdout);
expect(returnedJson.vulnerabilities.length > 0).toBeTruthy();
expect(code).toEqual(1);
expect(stdout).not.toBe('');
});

it('can save JSON output to file while sending human readable output to stdout', async () => {
const project = await createProjectFromWorkspace('no-vulns');
const outputPath = 'json-file-output.json';
Expand Down
79 changes: 79 additions & 0 deletions test/jest/acceptance/cli-json-output.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { fakeServer } from '../../acceptance/fake-server';
import { createProjectFromWorkspace } from '../util/createProject';
import { runSnykCLI } from '../util/runSnykCLI';

jest.setTimeout(1000 * 60);

describe('test --json', () => {
let server: ReturnType<typeof fakeServer>;
let env: Record<string, string>;

beforeAll((done) => {
const apiPath = '/api/v1';
const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345';
env = {
...process.env,
SNYK_API: 'http://localhost:' + apiPort + apiPath,
SNYK_TOKEN: '123456789',
SNYK_DISABLE_ANALYTICS: '1',
};

server = fakeServer(apiPath, env.SNYK_TOKEN);
server.listen(apiPort, () => done());
});

afterEach(() => {
server.restore();
});

afterAll((done) => {
server.close(() => done());
});

it('test with --json returns without error and with JSON return type when no vulns found', async () => {
const project = await createProjectFromWorkspace('fail-on/no-vulns');
server.setCustomResponse(await project.readJSON('vulns-result.json'));

const { code, stdout } = await runSnykCLI(`test --json`, {
cwd: project.path(),
env,
});

expect(code).toEqual(0);

expect(server.getRequests().length).toBeGreaterThanOrEqual(1);
const outputObj = JSON.parse(stdout);
expect(outputObj).not.toBe('');
});

it('test without --json returns without error and with a string return type when no vulns found', async () => {
const project = await createProjectFromWorkspace('fail-on/no-vulns');
server.setCustomResponse(await project.readJSON('vulns-result.json'));

const { code, stdout } = await runSnykCLI(`test`, {
cwd: project.path(),
env,
});

expect(code).toEqual(0);

expect(server.getRequests().length).toBeGreaterThanOrEqual(1);
expect(stdout).not.toBe('');
expect(typeof stdout).toBe('string');
});

it('test with --json throws error and error contains json output with vulnerabilities when vulns found', async () => {
const project = await createProjectFromWorkspace('fail-on/no-fixable');
server.setCustomResponse(await project.readJSON('vulns-result.json'));

const { code, stdout } = await runSnykCLI(`test --json`, {
cwd: project.path(),
env,
});

const returnedJson = JSON.parse(stdout);
expect(returnedJson.vulnerabilities.length > 0).toBeTruthy();
expect(code).toEqual(1);
expect(stdout).not.toBe('');
});
});
Loading