Skip to content

Commit

Permalink
Fix up yarn audit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
as3richa committed Jan 27, 2019
1 parent d85920b commit 6737b90
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions __tests__/commands/audit.js
Expand Up @@ -68,6 +68,7 @@ test.concurrent('sends correct dependency map to audit api for single dependency
'brace-expansion': '^1.0.0',
},
dependencies: {},
dev: false,
},
'brace-expansion': {
version: '1.1.11',
Expand All @@ -77,21 +78,25 @@ test.concurrent('sends correct dependency map to audit api for single dependency
'concat-map': '0.0.1',
},
dependencies: {},
dev: false,
},
'balanced-match': {
version: '1.0.0',
integrity: 'sha1-ibTRmasr7kneFk6gK4nORi1xt2c=',
requires: {},
dependencies: {},
dev: false,
},
'concat-map': {
version: '0.0.1',
integrity: 'sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=',
requires: {},
dependencies: {},
dev: false,
},
},
version: '0.0.0',
dev: false,
};

return runAudit([], {}, 'single-vulnerable-dep-installed', async config => {
Expand Down Expand Up @@ -141,8 +146,10 @@ test.concurrent('sends correct dependency map to audit api for private package.'
integrity: 'sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA==',
requires: {},
dependencies: {},
dev: false,
},
},
dev: false,
};

return runAudit([], {}, 'private-package', async config => {
Expand All @@ -167,10 +174,13 @@ test('calls reporter auditSummary with correct data for private package', () =>
});

describe('returns semantic exit codes', () => {
let lockfileSpy;
let installSpy;

beforeAll(() => {
// mock unrelated stuff
jest.spyOn(lockfileModule.default, 'fromDirectory').mockImplementation(jest.fn());
jest.spyOn(installModule, 'Install').mockImplementation(() => {
lockfileSpy = jest.spyOn(lockfileModule.default, 'fromDirectory').mockImplementation(jest.fn());
installSpy = jest.spyOn(installModule, 'Install').mockImplementation(() => {
return {
fetchRequestFromCwd: jest.fn(() => {
return {};
Expand All @@ -185,6 +195,11 @@ describe('returns semantic exit codes', () => {
});
});

afterAll(() => {
lockfileSpy.mockRestore();
installSpy.mockRestore();
});

const exitCodeTestCases = [
[0, {}, 'zero when no vulnerabilities'],
[1, {info: 77}, '1 for info'],
Expand All @@ -196,13 +211,16 @@ describe('returns semantic exit codes', () => {
];
exitCodeTestCases.forEach(([expectedExitCode, foundVulnerabilities, description]) => {
test(description, async () => {
jest.spyOn(auditModule.default.prototype, 'performAudit').mockImplementation(() => {
const spy = jest.spyOn(auditModule.default.prototype, 'performAudit').mockImplementation(() => {
return foundVulnerabilities;
});

const configMock: any = {};
const reporterMock: any = {};
const exitCode = await audit(configMock, reporterMock, {}, []);
expect(exitCode).toEqual(expectedExitCode);

spy.mockRestore();
});
});
});
Expand All @@ -212,12 +230,14 @@ test.concurrent('sends correct dependency map to audit api for workspaces.', ()
dependencies: {
'balanced-match': {
dependencies: {},
dev: false,
integrity: 'sha1-ibTRmasr7kneFk6gK4nORi1xt2c=',
requires: {},
version: '1.0.0',
},
'brace-expansion': {
dependencies: {},
dev: false,
integrity: 'sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==',
requires: {
'balanced-match': '^1.0.0',
Expand All @@ -227,12 +247,14 @@ test.concurrent('sends correct dependency map to audit api for workspaces.', ()
},
'concat-map': {
dependencies: {},
dev: false,
integrity: 'sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=',
requires: {},
version: '0.0.1',
},
minimatch: {
dependencies: {},
dev: false,
integrity: 'sha1-UjYVelHk8ATBd/s8Un/33Xjw74M=',
requires: {
'brace-expansion': '^1.0.0',
Expand All @@ -241,6 +263,7 @@ test.concurrent('sends correct dependency map to audit api for workspaces.', ()
},
prj1: {
dependencies: {},
dev: false,
integrity: '',
requires: {
minimatch: '3.0.0',
Expand All @@ -256,6 +279,7 @@ test.concurrent('sends correct dependency map to audit api for workspaces.', ()
prj1: '0.0.0',
},
version: '1.0.0',
dev: false,
};

return runAudit([], {}, 'workspace', async config => {
Expand Down

0 comments on commit 6737b90

Please sign in to comment.