Skip to content

Commit

Permalink
Merge pull request #1 from venkatesh21bit/test_optimization
Browse files Browse the repository at this point in the history
tests
  • Loading branch information
venkatesh21bit authored Dec 25, 2024
2 parents da5a32d + d8728c7 commit f2a5649
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
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
});

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
Original file line number Diff line number Diff line change
@@ -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!");
});
});
5 changes: 3 additions & 2 deletions templates/basic/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import express from "express";

const express = require("express");
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');
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);
})

Expand Down

0 comments on commit f2a5649

Please sign in to comment.