Skip to content

Commit

Permalink
Create GitHub Action to check test for backend (aerabi#20)
Browse files Browse the repository at this point in the history
* Add ESLint to Frontend Package
* Add Eslint to Backend
* Add run test github action

Co-authored-by: himanshukandpal-28 <[email protected]>

Fixes aerabi#12
  • Loading branch information
himanshukandpal-28 authored Oct 17, 2022
1 parent ff685d4 commit bf6cec9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 41 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
with:
node-version: ${{ matrix.node }}

- name: Start MongoDB
uses: supercharge/[email protected]

- name: Cache node_modules
uses: actions/cache@v2
with:
Expand All @@ -32,3 +35,6 @@ jobs:

- name: Run Build
run: npm run build

- name: Run Test
run: npm run test
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"start": "node dist/index.js",
"dev": "concurrently \"npx tsc --watch\" \"nodemon -q dist/index.js\"",
"test": "cross-env NODE_ENV=test mocha --exit dist/test/**.js",
"lint": "eslint 'src/**/*.{js,ts,tsx}' --fix",
"lint": "eslint 'src/**/*.{js,ts,tsx}' 'test/**/*.{js,ts,tsx}' --fix",
"lint:test": "eslint \"{src,apps,libs,test}/**/*.ts\""
},
"author": "",
Expand Down
84 changes: 44 additions & 40 deletions backend/test/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,62 @@ import dotenv from 'dotenv';
import { after, before, describe, it } from 'mocha';
import * as request from 'request';
import { APIServer } from '../src/APIServer';
import { EventModel } from "../src/model/events";
import { EventModel } from '../src/model/events';

const expect = chai.expect;
dotenv.config();

const port = process.env.PORT || 8000;
const eventRequest: request.RequestAPI<request.Request, request.CoreOptions, request.RequiredUriUrl>
= request.defaults({ baseUrl: `http://localhost:${port}` });
const eventRequest: request.RequestAPI<request.Request, request.CoreOptions, request.RequiredUriUrl> = request.defaults(
{ baseUrl: `http://localhost:${port}` },
);

describe('Event controller Tests', () => {
before(() => {
return APIServer.start();
});

before(() => {
return APIServer.start();
});
after(() => {
return APIServer.stop();
});

after(() => {
return APIServer.stop();
});
const testEvent: EventModel = {
name: 'test event' + Math.random(),
link: 'https://example.com',
date: new Date(),
organizer: 'test orga',
};

let testEvent: EventModel = {
name: "test event" + Math.random(),
link: "https://example.com",
date: new Date(),
organizer: "test orga",
}

describe('The event api', () => {
it('should should create an event', (done) => {
eventRequest.post({
body: testEvent,
json: true,
url: '/api/events'
}, (error: any, response, body) => {
expect(response.statusCode).to.eq(200);
expectIdenticalEvents(JSON.parse(JSON.stringify(testEvent)), response.body);
done();
});
});

it('should list all events', (done) => {
eventRequest.get("/api/events", { json: true }, (error, response, body) => {
expectIdenticalEvents(JSON.parse(JSON.stringify(testEvent)), response.body.find((event: any) => event.name == testEvent.name));
done();
});
});
describe('The event api', () => {
it('should should create an event', done => {
eventRequest.post(
{
body: testEvent,
json: true,
url: '/api/events',
},
(error: any, response, body) => {
expect(response.statusCode).to.eq(200);
expectIdenticalEvents(JSON.parse(JSON.stringify(testEvent)), response.body);
done();
},
);
});

it('should list all events', done => {
eventRequest.get('/api/events', { json: true }, (error, response, body) => {
expectIdenticalEvents(
JSON.parse(JSON.stringify(testEvent)),
response.body.find((event: any) => event.name == testEvent.name),
);
done();
});
});
});
});


const expectIdenticalEvents = (expected: EventModel, actual: any) => {
for (const [key, value] of Object.entries(expected)) {
expect(actual[key].toString()).to.eq(value);
}
}
for (const [key, value] of Object.entries(expected)) {
expect(actual[key].toString()).to.eq(value);
}
};

0 comments on commit bf6cec9

Please sign in to comment.