From 1fec66e8b656ae8b18933174ddb79ca1ad024563 Mon Sep 17 00:00:00 2001 From: Jahed Ahmed <jahed@snyk.io> Date: Wed, 11 Aug 2021 17:54:38 +0000 Subject: [PATCH] test: use async/await instead of callbacks --- .../acceptance/cli-json-file-output.spec.ts | 154 ++++++------------ 1 file changed, 48 insertions(+), 106 deletions(-) diff --git a/test/jest/acceptance/cli-json-file-output.spec.ts b/test/jest/acceptance/cli-json-file-output.spec.ts index 764422ea98..4352ea1778 100644 --- a/test/jest/acceptance/cli-json-file-output.spec.ts +++ b/test/jest/acceptance/cli-json-file-output.spec.ts @@ -1,120 +1,62 @@ -import { exec } from 'child_process'; -import { sep, join } from 'path'; -import { readFileSync, unlinkSync } from 'fs'; -import { v4 as uuidv4 } from 'uuid'; import { fakeServer } from '../../acceptance/fake-server'; -import cli = require('../../../src/cli/commands'); +import { createProjectFromWorkspace } from '../util/createProject'; +import { runSnykCLI } from '../util/runSnykCLI'; -const main = './bin/snyk'.replace(/\//g, sep); -const testTimeout = 50000; +jest.setTimeout(1000 * 60); describe('test --json-file-output ', () => { - let oldkey; - let oldendpoint; - const apiKey = '123456789'; - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + let server: ReturnType<typeof fakeServer>; + let env: Record<string, string>; + + beforeAll((done) => { + const apiPath = '/api/v1'; + const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345'; + env = { + ...process.env, + SNYK_API: 'http://localhost:' + apiPort + apiPath, + SNYK_TOKEN: '123456789', + }; + + server = fakeServer(apiPath, env.SNYK_TOKEN); + server.listen(apiPort, () => done()); + }); - const BASE_API = '/api/v1'; - const SNYK_API = 'http://localhost:' + port + BASE_API; - const SNYK_HOST = 'http://localhost:' + port; + afterAll((done) => { + server.close(() => done()); + }); - const server = fakeServer(BASE_API, apiKey); + it('can save JSON output to file while sending human readable output to stdout', async () => { + const project = await createProjectFromWorkspace('no-vulns'); + const outputPath = 'json-file-output.json'; - const noVulnsProjectPath = join( - __dirname, - '../../acceptance', - 'workspaces', - 'no-vulns', - ); - beforeAll(async () => { - let key = await cli.config('get', 'api'); - oldkey = key; + const { code, stdout } = await runSnykCLI( + `test --json-file-output=${outputPath}`, + { + cwd: project.path(), + env, + }, + ); - key = await cli.config('get', 'endpoint'); - oldendpoint = key; + expect(code).toEqual(0); + expect(stdout).toMatch('Organization:'); - await new Promise((resolve) => { - server.listen(port, resolve); - }); + const jsonObj = JSON.parse(await project.read(outputPath)); + expect(jsonObj).toMatchObject({ ok: true }); }); - afterAll(async () => { - delete process.env.SNYK_API; - delete process.env.SNYK_HOST; - delete process.env.SNYK_PORT; + it('test --json-file-output produces same JSON output as normal JSON output to stdout', async () => { + const project = await createProjectFromWorkspace('no-vulns'); + const outputPath = 'json-file-output.json'; - await server.close(); - let key = 'set'; - let value = 'api=' + oldkey; - if (!oldkey) { - key = 'unset'; - value = 'api'; - } - await cli.config(key, value); - if (oldendpoint) { - await cli.config('endpoint', oldendpoint); - } - }); - it( - '`can save JSON output to file while sending human readable output to stdout`', - (done) => { - const jsonOutputFilename = `${uuidv4()}.json`; - exec( - `node ${main} test ${noVulnsProjectPath} --json-file-output=${jsonOutputFilename}`, - { - env: { - PATH: process.env.PATH, - SNYK_TOKEN: apiKey, - SNYK_API, - SNYK_HOST, - }, - }, - (err, stdout) => { - if (err) { - throw err; - } + const { code, stdout } = await runSnykCLI( + `test --json --json-file-output=${outputPath}`, + { + cwd: project.path(), + env, + }, + ); - expect(stdout).toMatch('Organization:'); - const outputFileContents = readFileSync(jsonOutputFilename, 'utf-8'); - unlinkSync(`./${jsonOutputFilename}`); - const jsonObj = JSON.parse(outputFileContents); - const okValue = jsonObj.ok as boolean; - expect(okValue).toBeTruthy(); - done(); - }, - ); - }, - testTimeout, - ); - - it( - '`test --json-file-output produces same JSON output as normal JSON output to stdout`', - (done) => { - const jsonOutputFilename = `${uuidv4()}.json`; - exec( - `node ${main} test ${noVulnsProjectPath} --json --json-file-output=${jsonOutputFilename}`, - { - env: { - PATH: process.env.PATH, - SNYK_TOKEN: apiKey, - SNYK_API, - SNYK_HOST, - }, - }, - async (err, stdout) => { - if (err) { - throw err; - } - // give file a little time to be finished to be written - await new Promise((r) => setTimeout(r, 3000)); - const stdoutJson = stdout; - const outputFileContents = readFileSync(jsonOutputFilename, 'utf-8'); - unlinkSync(`./${jsonOutputFilename}`); - expect(stdoutJson).toEqual(outputFileContents); - done(); - }, - ); - }, - testTimeout, - ); + expect(code).toEqual(0); + expect(await project.read(outputPath)).toEqual(stdout + '\n'); + }); });