Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: increase analytics test timeout #2157

Merged
3 commits merged into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions test/acceptance/fake-server.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main thing to avoid test timeouts.


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

afterEach(() => {
jest.restoreAllMocks();
server.clearRequests();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this as we should typically clear state between tests so we aren't asserting on invalid state.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

});

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();
Comment on lines +311 to +313
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spying won't work as spies are bound to the current Node context; which is Jest. We are launching Snyk CLI in its own process where the spy does not exist.

To test this correctly, we can ensure fakeServer didn't receive a request.

});
});