Skip to content

Commit

Permalink
Merge pull request #2157 from snyk/test/analytics-fix
Browse files Browse the repository at this point in the history
test: increase analytics test timeout
  • Loading branch information
Jahed Ahmed committed Aug 12, 2021
2 parents 104b07d + 8e296a6 commit 01e348d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
4 changes: 4 additions & 0 deletions test/acceptance/fake-server.ts
Expand Up @@ -8,6 +8,7 @@ interface FakeServer extends restify.Server {
popRequests: (num: number) => restify.Request[];
setNextResponse: (r: any) => void;
setNextStatusCodeAndResponse: (c: number, r: any) => void;
clearRequests: () => void;
}

export function fakeServer(root, apikey) {
Expand All @@ -23,6 +24,9 @@ export function fakeServer(root, apikey) {
server.popRequests = (num: number) => {
return server._reqLog.splice(server._reqLog.length - num, num);
};
server.clearRequests = () => {
server._reqLog = [];
};
server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser({ mapParams: true }));
server.use(restify.plugins.bodyParser({ mapParams: true }));
Expand Down
33 changes: 17 additions & 16 deletions test/jest/acceptance/analytics.spec.ts
@@ -1,8 +1,11 @@
import { fakeServer } from '../../acceptance/fake-server';
import { createProject } from '../util/createProject';
import {
createProjectFromFixture,
createProjectFromWorkspace,
} from '../util/createProject';
import { runSnykCLI } from '../util/runSnykCLI';
import * as request from '../../../src/lib/request';
import * as fs from 'fs';

jest.setTimeout(1000 * 30);

describe('analytics module', () => {
let server;
Expand All @@ -27,7 +30,7 @@ describe('analytics module', () => {
});

afterEach(() => {
jest.restoreAllMocks();
server.clearRequests();
});

afterAll((done) => {
Expand All @@ -37,7 +40,7 @@ describe('analytics module', () => {
});

it('sends analytics for `snyk test` with no vulns found', async () => {
const project = await createProject('../acceptance/workspaces/npm-package');
const project = await createProjectFromWorkspace('npm-package');
const { code } = await runSnykCLI('test', {
cwd: project.path(),
env,
Expand Down Expand Up @@ -101,14 +104,11 @@ describe('analytics module', () => {
});

it('sends analytics for `snyk test` with vulns found', async () => {
const testDepGraphResult = JSON.parse(
fs.readFileSync(
'test/fixtures/npm/with-vulnerable-lodash-dep/test-dep-graph-result.json',
'utf-8',
),
const project = await createProjectFromFixture(
'npm/with-vulnerable-lodash-dep',
);
server.setNextResponse(testDepGraphResult);
const project = await createProject('npm/with-vulnerable-lodash-dep');

server.setNextResponse(await project.read('test-dep-graph-result.json'));

const { code } = await runSnykCLI('test', {
cwd: project.path(),
Expand Down Expand Up @@ -183,7 +183,7 @@ describe('analytics module', () => {
});

it('sends correct analytics data a bad command', async () => {
const project = await createProject('../acceptance/workspaces/npm-package');
const project = await createProjectFromWorkspace('npm-package');
const { code } = await runSnykCLI('random-nonsense-command --some-option', {
cwd: project.path(),
env,
Expand Down Expand Up @@ -246,7 +246,7 @@ describe('analytics module', () => {
});

it('sends analytics data a bad command', async () => {
const project = await createProject('../acceptance/workspaces/npm-package');
const project = await createProjectFromWorkspace('npm-package');
const { code } = await runSnykCLI('', {
cwd: project.path(),
env,
Expand Down Expand Up @@ -301,14 +301,15 @@ describe('analytics module', () => {
});

it("won't send analytics if disable analytics is set", async () => {
const requestSpy = jest.spyOn(request, 'makeRequest');
const { code } = await runSnykCLI(`version`, {
env: {
...env,
SNYK_DISABLE_ANALYTICS: '1',
},
});
expect(code).toBe(0);
expect(requestSpy).not.toBeCalled();

const lastRequest = server.popRequest();
expect(lastRequest).toBeUndefined();
});
});

0 comments on commit 01e348d

Please sign in to comment.