Skip to content

Commit 34e7c7b

Browse files
committedJul 29, 2021
test: add acceptance test for analytics
1 parent 24370cd commit 34e7c7b

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
 

‎test/acceptance/fake-server.ts

+6
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,12 @@ export function fakeServer(root, apikey) {
344344
return next();
345345
});
346346

347+
server.post(root + '/analytics/cli', (req, res, next) => {
348+
res.status(200);
349+
res.send({});
350+
return next();
351+
});
352+
347353
server.setNextResponse = (response) => {
348354
server._nextResponse = response;
349355
};
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { fakeServer } from '../../acceptance/fake-server';
2+
import { runSnykCLI } from '../util/runSnykCLI';
3+
4+
describe('analytics module', () => {
5+
let server;
6+
let env: Record<string, string>;
7+
8+
beforeAll((done) => {
9+
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
10+
const baseApi = '/api/v1';
11+
env = {
12+
...process.env,
13+
SNYK_API: 'http://localhost:' + port + baseApi,
14+
SNYK_HOST: 'http://localhost:' + port,
15+
SNYK_TOKEN: '123456789',
16+
};
17+
18+
server = fakeServer(baseApi, env.SNYK_TOKEN);
19+
server.listen(port, () => {
20+
done();
21+
});
22+
});
23+
24+
afterAll((done) => {
25+
server.close(() => {
26+
done();
27+
});
28+
});
29+
30+
test('validate actual analytics call', async () => {
31+
server.setNextResponse({});
32+
const { code } = await runSnykCLI(`version --org=fooOrg --all-projects`, {
33+
env,
34+
});
35+
expect(code).toBe(0);
36+
37+
const lastRequest = server.popRequest();
38+
expect(lastRequest).toMatchObject({
39+
headers: {
40+
host: 'localhost:12345',
41+
accept: 'application/json',
42+
authorization: 'token 123456789',
43+
'content-type': 'application/json; charset=utf-8',
44+
'x-snyk-cli-version': '1.0.0-monorepo',
45+
},
46+
query: {
47+
org: 'fooOrg',
48+
},
49+
body: {
50+
data: {
51+
args: [
52+
{
53+
org: 'fooOrg',
54+
allProjects: true,
55+
},
56+
],
57+
ci: expect.any(Boolean),
58+
command: 'version',
59+
durationMs: expect.any(Number),
60+
environment: {
61+
npmVersion: expect.any(String),
62+
},
63+
id: expect.any(String),
64+
integrationEnvironment: '',
65+
integrationEnvironmentVersion: '',
66+
integrationName: '',
67+
integrationVersion: '',
68+
// prettier-ignore
69+
metrics: {
70+
'network_time': {
71+
type: 'timer',
72+
values: [],
73+
total: expect.any(Number),
74+
},
75+
'cpu_time': {
76+
type: 'synthetic',
77+
values: [expect.any(Number)],
78+
total: expect.any(Number),
79+
},
80+
},
81+
nodeVersion: process.version,
82+
org: 'fooOrg',
83+
os: expect.any(String),
84+
standalone: false,
85+
version: '1.0.0-monorepo',
86+
},
87+
},
88+
});
89+
});
90+
});

0 commit comments

Comments
 (0)
Please sign in to comment.