Skip to content

Commit

Permalink
test: Update Jest configuration for end-to-end tests and remove unuse…
Browse files Browse the repository at this point in the history
…d test files
  • Loading branch information
the-sabra committed Jul 25, 2024
1 parent a312d8a commit dbfcc11
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 16 deletions.
15 changes: 14 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
{
"a-file-icon-vscode.activeIconPacks": ["nest", "rails"]
"a-file-icon-vscode.activeIconPacks": [
"angular",
"ngrx",
"nest",
"react",
"redux",
"recoil",
"vue",
"vuex",
"rails",
"phalcon",
"volt",
"none"
]
}
12 changes: 12 additions & 0 deletions jest-e2e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": "./",
"testRegex": ".*\\.e2e-spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"moduleNameMapper": {
"^src/(.*)$": "<rootDir>/src/$1"
},
"testEnvironment": "node"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"test:e2e": "jest --config ./jest-e2e.json --detectOpenHandles",
"typeorm": "ts-node --require tsconfig-paths/register ./node_modules/typeorm/cli",
"typeorm:generate-migration": "npm run typeorm -- -d ./typeorm.config.ts migration:generate ./migrations/$npm_config_name",
"typeorm:migrate": "npm run typeorm -- -d ./typeorm.config.ts migration:run"
Expand Down
16 changes: 12 additions & 4 deletions test/app.e2e-spec.ts → test/audio.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import { isURL } from 'class-validator';

describe('AppController (e2e)', () => {
describe('Audio E2E TESTS', () => {
let app: INestApplication;

beforeEach(async () => {
Expand All @@ -15,10 +16,17 @@ describe('AppController (e2e)', () => {
await app.init();
});

it('/ (GET)', () => {
it('should get reciter surah audio', () => {
const surah_id = 1;
const reciter_id = 1;

return request(app.getHttpServer())
.get('/')
.get(`/audio?surah_id=${surah_id}&reciter_id=${reciter_id}`)
.expect(200)
.expect('Hello World!');
.expect((res) => {
expect(res.body.surah_id).toBe(surah_id);
expect(res.body.reciter_id).toBe(reciter_id);
expect(isURL(res.body.url)).toBe(true);
});
});
});
9 changes: 0 additions & 9 deletions test/jest-e2e.json

This file was deleted.

45 changes: 45 additions & 0 deletions test/reciter.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';

describe('Reciter E2E TESTS', () => {
let app: INestApplication;

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
await app.init();
});

it('should get all reciters', () => {
return request(app.getHttpServer()).get('/reciter').expect(200);
});

it('should get reciter by id', () => {
return request(app.getHttpServer()).get('/reciter/1').expect(200);
});

it('should return 404 if reciter not found by id', () => {
return request(app.getHttpServer()).get('/reciter/99999').expect(404);
});

describe('adding image section', () => {
it('should add an image to a reciter', () => {
return request(app.getHttpServer())
.patch('/reciter/1')
.send({ image: 'image-url' })
.expect(200);
});

it('should return 404 if reciter not found by id', () => {
return request(app.getHttpServer())
.patch('/reciter/99999')
.send({ image: 'image-url' })
.expect(404);
});
});
});
70 changes: 70 additions & 0 deletions test/surah.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';

describe('Surah E2E TESTS', () => {
let app: INestApplication;

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
await app.init();
});

it('should get list of all surah', () => {
return request(app.getHttpServer())
.get('/surah')
.expect(200)
.expect((res) => {
expect(res.body).toHaveLength(114);
expect(res.body[0]).toMatchObject({
id: 1,
name_arabic: 'الفاتحة',
name_complex: 'Al-Fatihah',
revelation_place: 'meccan',
verses_count: 7,
image: 'image-url',
});
});
});

it('should get a surah by id', () => {
return request(app.getHttpServer())
.get('/surah/1')
.expect(200)
.expect((res) => {
expect(res.body).toMatchObject({
id: 1,
name_arabic: 'الفاتحة',
name_complex: 'Al-Fatihah',
revelation_place: 'meccan',
verses_count: 7,
image: 'image-url',
});
});
});

describe('adding image section', () => {
it('should add an image to a surah', () => {
return request(app.getHttpServer())
.patch('/surah/1')
.send({ image: 'image-url' })
.expect(200);
});

it('should return 404 if surah not found by id', () => {
return request(app.getHttpServer())
.patch('/surah/150')
.send({ image: 'image-url' })
.expect(404);
});
});

it('should return 404 if surah not found by id', () => {
return request(app.getHttpServer()).get('/surah/150').expect(404);
});
});
33 changes: 33 additions & 0 deletions test/verse.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';

describe('Verse E2E TESTS', () => {
let app: INestApplication;

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
await app.init();
});

it('should get surah verses', () => {
return request(app.getHttpServer())
.get('/verse/surah?surah_id=1')
.expect(200)
.expect((res) => {
expect(res.body.verses).toHaveLength(7);
expect(res.body.verses[0]).toMatchObject({
id: 1,
vers: 'بِسۡمِ ٱللَّهِ ٱلرَّحۡمَٰنِ ٱلرَّحِيمِ',
verse_number: 1,
vers_lang: 'ar',
surah_id: 1,
});
});
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
},
"include": ["src/**/*", "test", "seed.ts","typeorm.config.ts"]
"include": ["src/**/*", "test", "seed.ts"]
}

0 comments on commit dbfcc11

Please sign in to comment.