From d8728c7d889d8c1eb8d26514d28afa380f0e8a16 Mon Sep 17 00:00:00 2001 From: venkatesh21bit Date: Wed, 25 Dec 2024 22:42:58 +0530 Subject: [PATCH] tests --- bin/index.js | 10 ++++++---- package.json | 6 ++++-- templates/basic/basic_api.test.js | 9 +++++++++ templates/basic/server.js | 5 +++-- test/init.test.js | 8 ++++++++ 5 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 templates/basic/basic_api.test.js diff --git a/bin/index.js b/bin/index.js index 97568bc..ea3f679 100755 --- a/bin/index.js +++ b/bin/index.js @@ -77,8 +77,8 @@ program console.error(error); } }); - -function initCommand(options) { +//Changing sync to async to optimize further +async function initCommand(options) { const selectedTemplate = options.template || "basic"; // Default to 'basic' if no template is specified if (!templates[selectedTemplate]) { @@ -108,7 +108,9 @@ function initCommand(options) { const copySpinner = createSpinner("Creating server files...").start(); try { - fs.copySync(templatePath, destinationPath); + await fs.copy(templatePath, targetDir, { + filter: (src) => !src.includes("node_modules"), // Skiping node_modules to optimize + }); copySpinner.success({ text: "Created server files successfully." }); } catch (err) { @@ -133,7 +135,7 @@ function initCommand(options) { const addDependencies = createSpinner("Adding dependency packages...").start(); try { const packageJsonPath = path.join(targetDir, "package.json"); - const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8"); + const packageJsonContent = await fs.readJSON(packageJsonPath, "utf8"); const packageJson = JSON.parse(packageJsonContent); packageJson.dependencies = packageJson.dependencies || {}; diff --git a/package.json b/package.json index ef0a97e..68fbcab 100644 --- a/package.json +++ b/package.json @@ -14,11 +14,13 @@ "dependencies": { "chalk": "^5.3.0", "commander": "^12.1.0", + "express": "^4.21.2", "figlet": "^1.7.0", "fs-extra": "^11.2.0", - "nanospinner": "^1.1.0" + "nanospinner": "^1.1.0", + "supertest": "^7.0.0" }, "devDependencies": { "jest": "^29.7.0" } -} \ No newline at end of file +} diff --git a/templates/basic/basic_api.test.js b/templates/basic/basic_api.test.js new file mode 100644 index 0000000..96e2ad5 --- /dev/null +++ b/templates/basic/basic_api.test.js @@ -0,0 +1,9 @@ +const request = require("supertest"); +const app = require("./server.js"); +describe("API Endpoints", () => { + it("should return Hello World on GET /", async () => { + const res = await request(app).get("/"); + expect(res.statusCode).toEqual(200); + expect(res.text).toBe("Hello World!"); + }); +}); diff --git a/templates/basic/server.js b/templates/basic/server.js index d182777..be4248e 100644 --- a/templates/basic/server.js +++ b/templates/basic/server.js @@ -1,5 +1,4 @@ -import express from "express"; - +const express = require("express"); const app = express(); const port = 3000; @@ -10,3 +9,5 @@ app.get("/", (req, res) => { app.listen(port, () => { console.log(`Example app listening on port ${port}.`); }); + +module.exports = app; \ No newline at end of file diff --git a/test/init.test.js b/test/init.test.js index a5937e7..d58d734 100644 --- a/test/init.test.js +++ b/test/init.test.js @@ -2,6 +2,7 @@ const fs = require('fs'); const path = require('path'); const crypto = require('crypto'); const util = require('node:util'); +const { afterEach } = require('node:test'); const exec = util.promisify(require('node:child_process').exec); const tempDir = path.join(__dirname, 'temp'); @@ -66,9 +67,16 @@ describe('init', () => { }); test('no templates passed, should default to basic', async () => { + + console.time('Hash Calculation'); const originalHash = computeSHA256Hash(path.join(__dirname, '..', 'templates', 'basic')); + console.timeEnd('Hash Calculation'); + console.time('Command Execution'); await exec(`node ../../bin/index.js init`, { cwd: tempDir }); + console.timeEnd('Command Execution'); + console.time('Hash Verification'); const commandHash = computeSHA256Hash(tempDir); + console.timeEnd('Hash Verification'); expect(commandHash).toEqual(originalHash); })