diff --git a/test/jest/acceptance/cli-json-file-output.spec.ts b/test/jest/acceptance/cli-json-file-output.spec.ts index 5e611d9635..922c33efac 100644 --- a/test/jest/acceptance/cli-json-file-output.spec.ts +++ b/test/jest/acceptance/cli-json-file-output.spec.ts @@ -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'; diff --git a/test/jest/acceptance/cli-json-output.spec.ts b/test/jest/acceptance/cli-json-output.spec.ts new file mode 100644 index 0000000000..b877e2abfd --- /dev/null +++ b/test/jest/acceptance/cli-json-output.spec.ts @@ -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; + let env: Record; + + 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(''); + }); +});