From b6967fba65ac1154dc31fa28a522aeddba03b571 Mon Sep 17 00:00:00 2001 From: Tue Nguyen Date: Mon, 14 Feb 2022 10:49:33 -0500 Subject: [PATCH] Create test for dependency-discovery service --- src/api/dependency-discovery/jest.config.js | 9 ++++++ src/api/dependency-discovery/jest.setup.js | 1 + src/api/dependency-discovery/package.json | 1 - .../src/dependency-list.js | 2 +- src/api/dependency-discovery/src/router.js | 4 +-- .../test/dependency.test.js | 29 +++++++++++++++++++ 6 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 src/api/dependency-discovery/jest.config.js create mode 100644 src/api/dependency-discovery/jest.setup.js create mode 100644 src/api/dependency-discovery/test/dependency.test.js diff --git a/src/api/dependency-discovery/jest.config.js b/src/api/dependency-discovery/jest.config.js new file mode 100644 index 0000000000..ba7a2aa4a1 --- /dev/null +++ b/src/api/dependency-discovery/jest.config.js @@ -0,0 +1,9 @@ +const baseConfig = require('../../../jest.config.base'); + +module.exports = { + ...baseConfig, + setupFiles: ['/src/api/dependency-discovery/jest.setup.js'], + rootDir: '../../..', + testMatch: ['/src/api/dependency-discovery/test/*.test.js'], + collectCoverageFrom: ['/src.api/dependency-discovery/src/**/*.js'], +}; diff --git a/src/api/dependency-discovery/jest.setup.js b/src/api/dependency-discovery/jest.setup.js new file mode 100644 index 0000000000..6a26f57bb8 --- /dev/null +++ b/src/api/dependency-discovery/jest.setup.js @@ -0,0 +1 @@ +process.env = { ...process.env }; diff --git a/src/api/dependency-discovery/package.json b/src/api/dependency-discovery/package.json index 6eb5445ea1..11504bc2a4 100644 --- a/src/api/dependency-discovery/package.json +++ b/src/api/dependency-discovery/package.json @@ -4,7 +4,6 @@ "version": "0.1.0", "description": "A dependency graph compilation service for Telescope", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", "start": "node src/server.js", "dev": "env-cmd -f env.local nodemon src/server.js" }, diff --git a/src/api/dependency-discovery/src/dependency-list.js b/src/api/dependency-discovery/src/dependency-list.js index 239f73032b..0a97c5cac8 100644 --- a/src/api/dependency-discovery/src/dependency-list.js +++ b/src/api/dependency-discovery/src/dependency-list.js @@ -11,4 +11,4 @@ async function getDependencies() { return dependencies.split(/\r\n|\n|\r/).filter((line) => line !== ''); } -module.exports = getDependencies(); +module.exports = getDependencies; diff --git a/src/api/dependency-discovery/src/router.js b/src/api/dependency-discovery/src/router.js index cb5ae9cd31..7955855270 100644 --- a/src/api/dependency-discovery/src/router.js +++ b/src/api/dependency-discovery/src/router.js @@ -1,12 +1,12 @@ const { Router } = require('@senecacdot/satellite'); -const dependencyList = require('./dependency-list'); +const getDependencies = require('./dependency-list'); const router = Router(); router.get('/projects', async (req, res, next) => { try { res.set('Cache-Control', 'max-age=3600'); - res.status(200).json(await dependencyList); + res.status(200).json(await getDependencies()); } catch (err) { next(err); } diff --git a/src/api/dependency-discovery/test/dependency.test.js b/src/api/dependency-discovery/test/dependency.test.js new file mode 100644 index 0000000000..d17cc0fb90 --- /dev/null +++ b/src/api/dependency-discovery/test/dependency.test.js @@ -0,0 +1,29 @@ +const request = require('supertest'); +const { app } = require('../src'); + +const depsList = [ + '@apidevtools/json-schema-ref-parser', + '@babel/code-frame', + '@babel/compat-data', + '@babel/core", "@babel/generator', + '@babel/helper-annotate-as-pure', + '@babel/helper-builder-binary-assignment-operator-visitor', + '@babel/helper-compilation-targets', + '@babel/helper-create-class-features-plugin', + '@babel/helper-create-regexp-features-plugin', +]; + +jest.mock('../src/dependency-list', () => { + return jest.fn().mockImplementation(() => { + return Promise.resolve(depsList); + }); +}); + +describe('GET /projects', () => { + test('Should return 200 and an array of dependencies', async () => { + const res = await request(app).get('/projects'); + expect(res.status).toBe(200); + expect(typeof res.body).toEqual('object'); + expect(res.body.length).toBe(depsList.length); + }); +});