diff --git a/test/cli-json-file-output.spec.ts b/test/cli-json-file-output.spec.ts index c43f0925a7..28e93b4d1b 100644 --- a/test/cli-json-file-output.spec.ts +++ b/test/cli-json-file-output.spec.ts @@ -6,7 +6,7 @@ import { v4 as uuidv4 } from 'uuid'; const osName = require('os-name'); const main = './dist/cli/index.js'.replace(/\//g, sep); -const testTimeout = 40000; +const testTimeout = 50000; const isWindows = osName() .toLowerCase() @@ -28,7 +28,7 @@ describe('test --json-file-output ', () => { throw err; } // give file a little time to be finished to be written - await new Promise((r) => setTimeout(r, 3000)); + await new Promise((r) => setTimeout(r, 5000)); expect(stdout).toMatch('Organization:'); const outputFileContents = readFileSync( 'snyk-direct-json-test-output.json', @@ -88,7 +88,7 @@ describe('test --json-file-output ', () => { throw err; } // give file a little time to be finished to be written - await new Promise((r) => setTimeout(r, 3000)); + await new Promise((r) => setTimeout(r, 5000)); const stdoutJson = stdout; const outputFileContents = readFileSync(outputPath, 'utf-8'); unlinkSync(outputPath); @@ -123,7 +123,7 @@ describe('test --json-file-output ', () => { throw err; } // give file a little time to be finished to be written - await new Promise((r) => setTimeout(r, 3000)); + await new Promise((r) => setTimeout(r, 5000)); const stdoutJson = stdout; const outputFileContents = readFileSync(outputPath, 'utf-8'); unlinkSync(outputPath); diff --git a/test/ecosystems-test-cpp.spec.ts b/test/ecosystems-test-cpp.spec.ts index 700deff07e..5de2f3f3e2 100644 --- a/test/ecosystems-test-cpp.spec.ts +++ b/test/ecosystems-test-cpp.spec.ts @@ -13,6 +13,7 @@ const isWindows = .toLowerCase() .indexOf('windows') === 0; +const testTimeout = 1000; describe('testEcosystem - cpp', () => { const fixturePath = path.join(__dirname, 'fixtures', 'cpp-project'); const cwd = process.cwd(); @@ -51,157 +52,177 @@ describe('testEcosystem - cpp', () => { process.chdir(cwd); }); - it('should return human readable result when no json option given', async () => { - const makeRequestSpy = jest - .spyOn(request, 'makeRequest') - .mockResolvedValue({ result: testResult }); - const expected = TestCommandResult.createHumanReadableTestCommandResult( - displayTxt, - stringifyTestResults, - ); - const actual = await ecosystems.testEcosystem('cpp', ['.'], { - path: '', - }); - expect(makeRequestSpy.mock.calls[0][0]).toEqual({ - body: { - scanResult: { - facts: [ - { - type: 'cpp-fingerprints', - data: [ - { - filePath: 'add.cpp', - hash: '52d1b046047db9ea0c581cafd4c68fe5', - }, - { - filePath: 'add.h', - hash: 'aeca71a6e39f99a24ecf4c088eee9cb8', - }, - { - filePath: 'main.cpp', - hash: 'ad3365b3370ef6b1c3e778f875055f19', - }, - ], + it( + 'should return human readable result when no json option given', + async () => { + const makeRequestSpy = jest + .spyOn(request, 'makeRequest') + .mockResolvedValue({ result: testResult }); + const expected = TestCommandResult.createHumanReadableTestCommandResult( + displayTxt, + stringifyTestResults, + ); + const actual = await ecosystems.testEcosystem('cpp', ['.'], { + path: '', + }); + expect(makeRequestSpy.mock.calls[0][0]).toEqual({ + body: { + scanResult: { + facts: [ + { + type: 'cpp-fingerprints', + data: [ + { + filePath: 'add.cpp', + hash: '52d1b046047db9ea0c581cafd4c68fe5', + }, + { + filePath: 'add.h', + hash: 'aeca71a6e39f99a24ecf4c088eee9cb8', + }, + { + filePath: 'main.cpp', + hash: 'ad3365b3370ef6b1c3e778f875055f19', + }, + ], + }, + ], + identity: { + type: 'cpp', + }, + name: expect.any(String), + target: { + branch: expect.any(String), + remoteUrl: expect.any(String), }, - ], - identity: { - type: 'cpp', - }, - name: expect.any(String), - target: { - branch: expect.any(String), - remoteUrl: expect.any(String), }, }, - }, - headers: { - authorization: expect.stringContaining('token'), - 'x-is-ci': expect.any(Boolean), - }, - json: true, - method: 'POST', - url: expect.stringContaining('/test-dependencies'), - qs: expect.any(Object), - }); - expect(actual).toEqual(expected); - }); - - it('should return json result when json option', async () => { - const makeRequestSpy = jest - .spyOn(request, 'makeRequest') - .mockResolvedValue({ result: testResult }); - const expected = TestCommandResult.createJsonTestCommandResult( - stringifyTestResults, - ); - const actual = await ecosystems.testEcosystem('cpp', ['.'], { - path: '', - json: true, - }); - expect(makeRequestSpy.mock.calls[0][0]).toEqual({ - body: { - scanResult: { - facts: [ - { - type: 'cpp-fingerprints', - data: [ - { - filePath: 'add.cpp', - hash: '52d1b046047db9ea0c581cafd4c68fe5', - }, - { - filePath: 'add.h', - hash: 'aeca71a6e39f99a24ecf4c088eee9cb8', - }, - { - filePath: 'main.cpp', - hash: 'ad3365b3370ef6b1c3e778f875055f19', - }, - ], + headers: { + authorization: expect.stringContaining('token'), + 'x-is-ci': expect.any(Boolean), + }, + json: true, + method: 'POST', + url: expect.stringContaining('/test-dependencies'), + qs: expect.any(Object), + }); + expect(actual).toEqual(expected); + }, + testTimeout, + ); + + it( + 'should return json result when json option', + async () => { + const makeRequestSpy = jest + .spyOn(request, 'makeRequest') + .mockResolvedValue({ result: testResult }); + const expected = TestCommandResult.createJsonTestCommandResult( + stringifyTestResults, + ); + const actual = await ecosystems.testEcosystem('cpp', ['.'], { + path: '', + json: true, + }); + expect(makeRequestSpy.mock.calls[0][0]).toEqual({ + body: { + scanResult: { + facts: [ + { + type: 'cpp-fingerprints', + data: [ + { + filePath: 'add.cpp', + hash: '52d1b046047db9ea0c581cafd4c68fe5', + }, + { + filePath: 'add.h', + hash: 'aeca71a6e39f99a24ecf4c088eee9cb8', + }, + { + filePath: 'main.cpp', + hash: 'ad3365b3370ef6b1c3e778f875055f19', + }, + ], + }, + ], + identity: { + type: 'cpp', + }, + name: expect.any(String), + target: { + branch: expect.any(String), + remoteUrl: expect.any(String), }, - ], - identity: { - type: 'cpp', - }, - name: expect.any(String), - target: { - branch: expect.any(String), - remoteUrl: expect.any(String), }, }, - }, - headers: { - authorization: expect.stringContaining('token'), - 'x-is-ci': expect.any(Boolean), - }, - json: true, - method: 'POST', - url: expect.stringContaining('/test-dependencies'), - qs: expect.any(Object), - }); - expect(actual).toEqual(expected); - }); - - it('should return fingerprints when debug option is set', async () => { - const mock = jest - .spyOn(request, 'makeRequest') - .mockResolvedValue({ result: testResult }); - const expected = TestCommandResult.createHumanReadableTestCommandResult( - debugDisplayTxt, - stringifyTestResults, - ); - const actual = await ecosystems.testEcosystem('cpp', ['.'], { - path: '', - debug: true, - }); - expect(mock).toHaveBeenCalled(); - expect(actual).toEqual(expected); - }); - - it('should throw error when response code is not 200', async () => { - const error = { code: 401, message: 'Invalid auth token' }; - jest.spyOn(request, 'makeRequest').mockRejectedValue(error); - const expected = new Error(error.message); - expect.assertions(1); - try { - await ecosystems.testEcosystem('cpp', ['.'], { + headers: { + authorization: expect.stringContaining('token'), + 'x-is-ci': expect.any(Boolean), + }, + json: true, + method: 'POST', + url: expect.stringContaining('/test-dependencies'), + qs: expect.any(Object), + }); + expect(actual).toEqual(expected); + }, + testTimeout, + ); + + it( + 'should return fingerprints when debug option is set', + async () => { + const mock = jest + .spyOn(request, 'makeRequest') + .mockResolvedValue({ result: testResult }); + const expected = TestCommandResult.createHumanReadableTestCommandResult( + debugDisplayTxt, + stringifyTestResults, + ); + const actual = await ecosystems.testEcosystem('cpp', ['.'], { path: '', + debug: true, }); - } catch (error) { - expect(error).toEqual(expected); - } - }); - - it('should return error when there was a problem testing dependencies', async () => { - jest - .spyOn(request, 'makeRequest') - .mockRejectedValue('Something went wrong'); - const expected = TestCommandResult.createHumanReadableTestCommandResult( - errorTxt, - '[]', - ); - const actual = await ecosystems.testEcosystem('cpp', ['.'], { - path: '', - }); - expect(actual).toEqual(expected); - }); + expect(mock).toHaveBeenCalled(); + expect(actual).toEqual(expected); + }, + testTimeout, + ); + + it( + 'should throw error when response code is not 200', + async () => { + const error = { code: 401, message: 'Invalid auth token' }; + jest.spyOn(request, 'makeRequest').mockRejectedValue(error); + const expected = new Error(error.message); + expect.assertions(1); + try { + await ecosystems.testEcosystem('cpp', ['.'], { + path: '', + }); + } catch (error) { + expect(error).toEqual(expected); + } + }, + testTimeout, + ); + + it( + 'should return error when there was a problem testing dependencies', + async () => { + jest + .spyOn(request, 'makeRequest') + .mockRejectedValue('Something went wrong'); + const expected = TestCommandResult.createHumanReadableTestCommandResult( + errorTxt, + '[]', + ); + const actual = await ecosystems.testEcosystem('cpp', ['.'], { + path: '', + }); + expect(actual).toEqual(expected); + }, + testTimeout, + ); });