Skip to content

Commit 4cf24ba

Browse files
committed
fixed dependencies issue of jsonwebtoken and the dynamic import for controller
1 parent 70f5985 commit 4cf24ba

File tree

6 files changed

+116
-4
lines changed

6 files changed

+116
-4
lines changed

.github/workflows/test.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Test BackTool CLI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
16+
- name: Setup Node.js
17+
uses: actions/setup-node@v3
18+
with:
19+
node-version: '18'
20+
21+
- name: Install dependencies
22+
run: npm install
23+
24+
- name: Run tests
25+
run: npm test
26+
27+
- name: Test MongoDB setup
28+
run: npx backtool --project test-mongo --database MongoDB
29+
30+
- name: Test PostgreSQL setup
31+
run: npx backtool --project test-postgres --database PostgreSQL
32+
33+
- name: Test MySQL setup
34+
run: npx backtool --project test-mysql --database MySQL
35+
36+
- name: Test SQLite setup
37+
run: npx backtool --project test-sqlite --database SQLite

backtool_folder/controllers/auth.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import jwt from 'jsonwebtoken';
2-
import User from '../models/user.${database}.js';
2+
import config from '../config.js';
3+
import User from `../models/user.${config.database}.js`;
34

45
const generateToken = (user) => {
56
return jwt.sign(

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ async function generateBackendStructure(projectName, databaseChoice) {
8383
});
8484
8585
mongoose.set('strictQuery', true);`;
86+
87+
8688
break;
8789
case 'PostgreSQL':
8890
packagesToInstall.push('pg', 'bcryptjs');
@@ -163,6 +165,7 @@ async function generateBackendStructure(projectName, databaseChoice) {
163165
express: '^4.18.2',
164166
dotenv: '^16.3.1',
165167
cors: '^2.8.5',
168+
jsonwebtoken: '^9.0.2',
166169
...(databaseChoice === 'MongoDB' && { mongoose: '^8.0.0', bcryptjs: '^2.4.3' }),
167170
...(databaseChoice === 'PostgreSQL' && { pg: '^8.11.3', bcryptjs: '^2.4.3' }),
168171
...(databaseChoice === 'MySQL' && { mysql2: '^3.6.2', bcryptjs: '^2.4.3' }),

package.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
{
22
"name": "backtool",
3-
"version": "1.0.3",
3+
"version": "1.0.5",
44
"description": "This tool cli creates a backend setup with express js mongodb or postgresql",
55
"main": "index.js",
66
"type": "module",
77
"bin": {
88
"backtool": "index.js"
99
},
1010
"scripts": {
11-
"test": "echo \"Error: no test specified\" && exit 1"
11+
"test": "jest",
12+
"test:watch": "jest --watch",
13+
"test:coverage": "jest --coverage",
14+
"test:ci": "jest --ci --runInBand --coverage"
15+
},
16+
"jest": {
17+
"testEnvironment": "node",
18+
"coveragePathIgnorePatterns": [
19+
"/node_modules/"
20+
]
1221
},
1322
"author": "Amanpreet",
1423
"license": "ISC",
24+
"devDependencies": {
25+
"jest": "^29.7.0",
26+
"supertest": "^6.3.4"
27+
},
1528
"dependencies": {
1629
"bcrypt": "^5.1.1",
1730
"bcryptjs": "^3.0.2",
@@ -27,4 +40,4 @@
2740
"npm-programmatic": "^0.0.12",
2841
"ora": "^8.2.0"
2942
}
30-
}
43+
}

tests/cli.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, it, expect } from '@jest/globals';
2+
import { exec } from 'child_process';
3+
import path from 'path';
4+
import { fileURLToPath } from 'url';
5+
6+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
7+
const cliPath = path.join(__dirname, '..', 'index.js');
8+
9+
describe('CLI functionality', () => {
10+
it('should run without errors with --help flag', (done) => {
11+
exec(`node ${cliPath} --help`, (error, stdout, stderr) => {
12+
expect(error).toBeNull();
13+
expect(stdout).toContain('Usage');
14+
expect(stderr).toBe('');
15+
done();
16+
});
17+
});
18+
19+
it('should show version with -v flag', (done) => {
20+
exec(`node ${cliPath} -v`, (error, stdout, stderr) => {
21+
expect(error).toBeNull();
22+
expect(stdout).toContain('1.0.3');
23+
expect(stderr).toBe('');
24+
done();
25+
});
26+
});
27+
});

tests/db.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { describe, it, expect } from '@jest/globals';
2+
import mongoose from 'mongoose';
3+
import { MongoMemoryServer } from 'mongodb-memory-server';
4+
5+
let mongoServer;
6+
7+
beforeAll(async () => {
8+
mongoServer = await MongoMemoryServer.create();
9+
const uri = mongoServer.getUri();
10+
await mongoose.connect(uri);
11+
});
12+
13+
afterAll(async () => {
14+
await mongoose.disconnect();
15+
await mongoServer.stop();
16+
});
17+
18+
describe('Database operations', () => {
19+
it('should connect to in-memory MongoDB', () => {
20+
expect(mongoose.connection.readyState).toBe(1);
21+
});
22+
23+
it('should create and save a document', async () => {
24+
const TestModel = mongoose.model('Test', new mongoose.Schema({ name: String }));
25+
const doc = new TestModel({ name: 'test' });
26+
await doc.save();
27+
28+
const found = await TestModel.findOne({ name: 'test' });
29+
expect(found.name).toBe('test');
30+
});
31+
});

0 commit comments

Comments
 (0)