Skip to content

Commit 1fec66e

Browse files
author
Jahed Ahmed
committedAug 12, 2021
test: use async/await instead of callbacks
1 parent 04d18cd commit 1fec66e

File tree

1 file changed

+48
-106
lines changed

1 file changed

+48
-106
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,62 @@
1-
import { exec } from 'child_process';
2-
import { sep, join } from 'path';
3-
import { readFileSync, unlinkSync } from 'fs';
4-
import { v4 as uuidv4 } from 'uuid';
51
import { fakeServer } from '../../acceptance/fake-server';
6-
import cli = require('../../../src/cli/commands');
2+
import { createProjectFromWorkspace } from '../util/createProject';
3+
import { runSnykCLI } from '../util/runSnykCLI';
74

8-
const main = './bin/snyk'.replace(/\//g, sep);
9-
const testTimeout = 50000;
5+
jest.setTimeout(1000 * 60);
106

117
describe('test --json-file-output ', () => {
12-
let oldkey;
13-
let oldendpoint;
14-
const apiKey = '123456789';
15-
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
8+
let server: ReturnType<typeof fakeServer>;
9+
let env: Record<string, string>;
10+
11+
beforeAll((done) => {
12+
const apiPath = '/api/v1';
13+
const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345';
14+
env = {
15+
...process.env,
16+
SNYK_API: 'http://localhost:' + apiPort + apiPath,
17+
SNYK_TOKEN: '123456789',
18+
};
19+
20+
server = fakeServer(apiPath, env.SNYK_TOKEN);
21+
server.listen(apiPort, () => done());
22+
});
1623

17-
const BASE_API = '/api/v1';
18-
const SNYK_API = 'http://localhost:' + port + BASE_API;
19-
const SNYK_HOST = 'http://localhost:' + port;
24+
afterAll((done) => {
25+
server.close(() => done());
26+
});
2027

21-
const server = fakeServer(BASE_API, apiKey);
28+
it('can save JSON output to file while sending human readable output to stdout', async () => {
29+
const project = await createProjectFromWorkspace('no-vulns');
30+
const outputPath = 'json-file-output.json';
2231

23-
const noVulnsProjectPath = join(
24-
__dirname,
25-
'../../acceptance',
26-
'workspaces',
27-
'no-vulns',
28-
);
29-
beforeAll(async () => {
30-
let key = await cli.config('get', 'api');
31-
oldkey = key;
32+
const { code, stdout } = await runSnykCLI(
33+
`test --json-file-output=${outputPath}`,
34+
{
35+
cwd: project.path(),
36+
env,
37+
},
38+
);
3239

33-
key = await cli.config('get', 'endpoint');
34-
oldendpoint = key;
40+
expect(code).toEqual(0);
41+
expect(stdout).toMatch('Organization:');
3542

36-
await new Promise((resolve) => {
37-
server.listen(port, resolve);
38-
});
43+
const jsonObj = JSON.parse(await project.read(outputPath));
44+
expect(jsonObj).toMatchObject({ ok: true });
3945
});
4046

41-
afterAll(async () => {
42-
delete process.env.SNYK_API;
43-
delete process.env.SNYK_HOST;
44-
delete process.env.SNYK_PORT;
47+
it('test --json-file-output produces same JSON output as normal JSON output to stdout', async () => {
48+
const project = await createProjectFromWorkspace('no-vulns');
49+
const outputPath = 'json-file-output.json';
4550

46-
await server.close();
47-
let key = 'set';
48-
let value = 'api=' + oldkey;
49-
if (!oldkey) {
50-
key = 'unset';
51-
value = 'api';
52-
}
53-
await cli.config(key, value);
54-
if (oldendpoint) {
55-
await cli.config('endpoint', oldendpoint);
56-
}
57-
});
58-
it(
59-
'`can save JSON output to file while sending human readable output to stdout`',
60-
(done) => {
61-
const jsonOutputFilename = `${uuidv4()}.json`;
62-
exec(
63-
`node ${main} test ${noVulnsProjectPath} --json-file-output=${jsonOutputFilename}`,
64-
{
65-
env: {
66-
PATH: process.env.PATH,
67-
SNYK_TOKEN: apiKey,
68-
SNYK_API,
69-
SNYK_HOST,
70-
},
71-
},
72-
(err, stdout) => {
73-
if (err) {
74-
throw err;
75-
}
51+
const { code, stdout } = await runSnykCLI(
52+
`test --json --json-file-output=${outputPath}`,
53+
{
54+
cwd: project.path(),
55+
env,
56+
},
57+
);
7658

77-
expect(stdout).toMatch('Organization:');
78-
const outputFileContents = readFileSync(jsonOutputFilename, 'utf-8');
79-
unlinkSync(`./${jsonOutputFilename}`);
80-
const jsonObj = JSON.parse(outputFileContents);
81-
const okValue = jsonObj.ok as boolean;
82-
expect(okValue).toBeTruthy();
83-
done();
84-
},
85-
);
86-
},
87-
testTimeout,
88-
);
89-
90-
it(
91-
'`test --json-file-output produces same JSON output as normal JSON output to stdout`',
92-
(done) => {
93-
const jsonOutputFilename = `${uuidv4()}.json`;
94-
exec(
95-
`node ${main} test ${noVulnsProjectPath} --json --json-file-output=${jsonOutputFilename}`,
96-
{
97-
env: {
98-
PATH: process.env.PATH,
99-
SNYK_TOKEN: apiKey,
100-
SNYK_API,
101-
SNYK_HOST,
102-
},
103-
},
104-
async (err, stdout) => {
105-
if (err) {
106-
throw err;
107-
}
108-
// give file a little time to be finished to be written
109-
await new Promise((r) => setTimeout(r, 3000));
110-
const stdoutJson = stdout;
111-
const outputFileContents = readFileSync(jsonOutputFilename, 'utf-8');
112-
unlinkSync(`./${jsonOutputFilename}`);
113-
expect(stdoutJson).toEqual(outputFileContents);
114-
done();
115-
},
116-
);
117-
},
118-
testTimeout,
119-
);
59+
expect(code).toEqual(0);
60+
expect(await project.read(outputPath)).toEqual(stdout + '\n');
61+
});
12062
});

0 commit comments

Comments
 (0)
Please sign in to comment.