Skip to content

Commit

Permalink
Write unit tests for user registration and login
Browse files Browse the repository at this point in the history
Fixes DEVRhylme-Foundation#4

Add unit tests for user registration and login functionalities.

* **Tests**: Add `tests/auth.test.js` to test `POST /auth/register` and `POST /auth/login` endpoints using Jest.
* **Dependencies**: Add `jest` and `supertest` as dev dependencies in `package.json`. Add a test script to run Jest.
* **Configuration**: Add `jest.config.js` to configure Jest for the Node.js environment.
* **CI**: Modify `.github/workflows/build.yaml` to include steps for setting up Node.js, installing dependencies, and running Jest tests.
  • Loading branch information
yashksaini-coder committed Feb 16, 2025
1 parent 7e57b5d commit 9c86ceb
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,14 @@ jobs:

- name: Test
run: go test -v ./...

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install dependencies
run: npm install

- name: Run Jest tests
run: npm test
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
testEnvironment: 'node',
verbose: true,
collectCoverage: true,
coverageDirectory: 'coverage',
testMatch: ['**/tests/**/*.test.js']
};
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "expressify",
"version": "1.0.0",
"description": "A CLI tool to generate a production-grade scaffold for Express applications.",
"main": "index.js",
"scripts": {
"test": "jest"
},
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"jest": "^27.0.6",
"supertest": "^6.1.3"
},
"author": "DEVRhylme-Foundation",
"license": "MIT"
}
63 changes: 63 additions & 0 deletions tests/auth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const request = require('supertest');
const app = require('../src/app'); // Adjust the path as necessary

describe('User Registration and Login', () => {
describe('POST /auth/register', () => {
it('should register a user with valid data', async () => {
const response = await request(app)
.post('/auth/register')
.send({
username: 'testuser',
password: 'password123',
email: '[email protected]'
});
expect(response.status).toBe(201);
expect(response.body).toHaveProperty('id');
expect(response.body).toHaveProperty('username', 'testuser');
});

it('should reject registration with invalid data', async () => {
const response = await request(app)
.post('/auth/register')
.send({
username: '',
password: 'short',
email: 'invalidemail'
});
expect(response.status).toBe(400);
});
});

describe('POST /auth/login', () => {
it('should return a token for valid credentials', async () => {
// First, register a user
await request(app)
.post('/auth/register')
.send({
username: 'testuser',
password: 'password123',
email: '[email protected]'
});

// Then, login with the same user
const response = await request(app)
.post('/auth/login')
.send({
username: 'testuser',
password: 'password123'
});
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('token');
});

it('should reject login with invalid credentials', async () => {
const response = await request(app)
.post('/auth/login')
.send({
username: 'nonexistentuser',
password: 'wrongpassword'
});
expect(response.status).toBe(401);
});
});
});

0 comments on commit 9c86ceb

Please sign in to comment.