Skip to content

Commit

Permalink
Merge pull request #1662 from snyk/test/add-longer-timeouts
Browse files Browse the repository at this point in the history
test: add longer timeoutsfor flaky tests
  • Loading branch information
lili2311 authored Feb 26, 2021
2 parents 3a2a30e + bc9c0f2 commit 8cd9fbf
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 149 deletions.
8 changes: 4 additions & 4 deletions test/cli-json-file-output.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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',
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
311 changes: 166 additions & 145 deletions test/ecosystems-test-cpp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
);
});

0 comments on commit 8cd9fbf

Please sign in to comment.