Skip to content

Commit

Permalink
feat(user-Test): implementing sign up using jest
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuyisenge2 committed Apr 22, 2024
1 parent fe1b880 commit 1531571
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 112 deletions.
43 changes: 30 additions & 13 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
# This workflow will install node dependencies, run tests and report coverage
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: build

on:
push:
branches:
- feat-test-CI-implementation-187419042
- "*"
- develop

- "*"
pull_request:
branches:
- develop
- "*"

jobs:
test:
Expand All @@ -22,6 +20,11 @@ jobs:
DB_TEST_URL: ${{ secrets.DB_TEST_URL }}
DEV_MODE: ${{ secrets.DEV_MODE }}
DB_HOSTED_MODE: ${{ secrets.DB_HOSTED_MODE }}
ACCESS_TOKEN_SECRET: ${{ secrets.ACCESS_TOKEN_SECRET }}
SESSION_SECRET: ${{ secrets.SESSION_SECRET }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}

strategy:
matrix:
Expand All @@ -37,15 +40,29 @@ jobs:
- name: Install dependencies
run: npm install

- name: Run tests
run: npm run test

- name: Run tests and build test coverage
run: npm run test:ci

- name: Test & publish code climate coverage
uses: paambaati/[email protected]
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
with:
coverageCommand: npm run test:ci
debug: true
coverageLocations: |
${{github.workspace}}/*.lcov:lcov
- name: Setup Code Climate test-reporter
run: |
# Download test reporter as a static binary
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build
- name: Run tests
run: npm run test

- name: Store coverage report
if: always()
run: mkdir -p coverage


- name: Send coverage report to Code Climate
if: always()
run: ./cc-test-reporter after-build -t lcov -p coverage

- name: coveralls
run: npx coveralls < coverage/lcov.info
23 changes: 0 additions & 23 deletions src/__test__/login.test.ts

This file was deleted.

31 changes: 0 additions & 31 deletions src/__test__/product.test.ts

This file was deleted.

29 changes: 0 additions & 29 deletions src/__test__/users.test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,13 @@
import app from "../app";
import request from "supertest";
// import { connectionToDatabase } from "../database/config/db.config";

jest.setTimeout(30000);

describe("USER API TEST", () => {
// beforeAll(async () => {
// await connectionToDatabase();
// });

it("Should return 200 and list of all users", async () => {
// Your test implementation goes here
});

it("should create a new user", async () => {
// Your test implementation goes here
});

it("should return validation errors when required fields are missing", async () => {
// Your test implementation goes here
});

it("should return an error if the email is already in use", async () => {
// Your test implementation goes here
});
});

import { connectionToDatabase } from "../database/config/db.config";
import { deleteTableData } from "../utils/database.utils";
import { User } from "../database/models/User";
import {
NewUser,
user_bad_request,
User_without_email,
exist_user,
} from "../mock/static";
import { createUser } from "../services/user.services";
Expand Down Expand Up @@ -62,7 +37,6 @@ describe("USER API TEST", () => {
/**
* ----------------------------register new user --------------------------------------------
*/
describe("FAILED SIGN UP", () => {
it("it should return a user not found and status 400", async () => {
const { body } = await Jest_request.post("/api/v1/users/register")
.send(user_bad_request)
Expand All @@ -76,15 +50,12 @@ describe("USER API TEST", () => {
expect(body.status).toStrictEqual("CONFLICT");
expect(body.message).toStrictEqual("User already exist!");
});
});
describe("SUCCCESSFULL SIGN UP WITH TOKEN CREATED", () => {
it("it should register a user and return 201", async () => {
const { body } = await Jest_request.post("/api/v1/users/register")
.send(NewUser)
.expect(201);
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Account Created successfully!");
expect(body.token).toBeDefined();
});
});
});
2 changes: 1 addition & 1 deletion src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const registerUser = async (
} catch (error) {
res
.status(500)
.json(new HttpException("SERVER FAIL", "Something went wrong!"));
.json(new HttpException("SERVER FAILS", "Something went wrong!"));
}
};

Expand Down
21 changes: 10 additions & 11 deletions src/database/config/db.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ export const sequelizeConnection: Sequelize = new Sequelize(db_uri, {
idle: 10000,
},
});

export const connectionToDatabase = () =>
sequelizeConnection
.authenticate()
.then(() => {
console.log("Database connected successfully.", db_uri);
})
.catch((error) => {
console.error("Unable to connect to the database:", error);
process.exit(1);
});
export const connectionToDatabase = async () => {
try {
await sequelizeConnection.authenticate();
await sequelizeConnection.sync();
console.log("Database connected successfully.", db_uri);
} catch (error) {
console.log("Unable to connect to the database:", error);
process.exit(1);
}
}
4 changes: 0 additions & 4 deletions src/utils/database.utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import {
connectionToDatabase,
sequelizeConnection,
} from "../database/config/db.config";

export const deleteTableData = async (Model: any, tableName: string) => {
try {
Expand Down

0 comments on commit 1531571

Please sign in to comment.