Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized test case execution time && Tests to verify the api endpoint. #58

Merged
merged 11 commits into from
Dec 30, 2024
10 changes: 6 additions & 4 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down Expand Up @@ -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
});
venkatesh21bit marked this conversation as resolved.
Show resolved Hide resolved

copySpinner.success({ text: "Created server files successfully." });
} catch (err) {
Expand All @@ -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 || {};

Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
9 changes: 9 additions & 0 deletions templates/basic/basic_api.test.js
venkatesh21bit marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const request = require("supertest");
const app = require("./server.js");
Ashrockzzz2003 marked this conversation as resolved.
Show resolved Hide resolved
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!");
});
});
5 changes: 3 additions & 2 deletions templates/basic/server.js
Ashrockzzz2003 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import express from "express";

const express = require("express");
Ashrockzzz2003 marked this conversation as resolved.
Show resolved Hide resolved
const app = express();
const port = 3000;

Expand All @@ -10,3 +9,5 @@ app.get("/", (req, res) => {
app.listen(port, () => {
console.log(`Example app listening on port ${port}.`);
});

module.exports = app;
8 changes: 8 additions & 0 deletions test/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
venkatesh21bit marked this conversation as resolved.
Show resolved Hide resolved
const exec = util.promisify(require('node:child_process').exec);
const tempDir = path.join(__dirname, 'temp');

Expand Down Expand Up @@ -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);
Ashrockzzz2003 marked this conversation as resolved.
Show resolved Hide resolved
})

Expand Down