-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
89 changed files
with
679 additions
and
515 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Testing Nestjs applications | ||
|
||
In the previous posts, I have write a lot of testing codes to verify if our application is working as expected. | ||
|
||
Nestjs provides integration with with [Jest](https://github.com/facebook/jest) and [Supertest](https://github.com/visionmedia/supertest) out-of-the-box, and testing harness for unit testing and end-to-end (e2e) test. | ||
|
||
## Nestjs test harness | ||
|
||
Like the Angular 's `TestBed`, Nestjs provide a similar `Test` facilities to assemble the Nestjs components for your testing codes. | ||
|
||
```typescript | ||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
... | ||
], | ||
}).compile(); | ||
|
||
service = module.get<UserService>(UserService); | ||
}); | ||
|
||
``` | ||
|
||
|
||
## End-to-end testing | ||
|
||
Nestjs integrates supertest to send a request to the server side. | ||
|
||
Use `beforeAll` and `afterAll` to start and stop the application, use `request` to send a http request to the server and assert the response result. | ||
The `APP_URL` is the url of the server, it is defined in the `fixtures/constant.ts` file. This is the base url. Modify it to your own url. | ||
Also since we are not using app module, we need to separately run the server in one terminal and issue the test command in another terminal. | ||
To run the e2e test, use `make test-e2e` command. | ||
|
||
|
||
```typescript | ||
import * as request from 'supertest'; | ||
import { APP_URL } from "../fixtures/constant"; | ||
//... | ||
|
||
describe('API endpoints testing (e2e)', () => { | ||
|
||
const app = APP_URL; | ||
|
||
beforeAll(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile(); | ||
}); | ||
|
||
|
||
// an example of using supertest request. | ||
it("should get a list of all user /posts (GET)", () => { | ||
return request(app) | ||
.get("/posts") | ||
.auth(adminJwtToken, { type: "bearer" }) | ||
.expect(({ body }) => { | ||
expect(body.meta).toBeDefined(); | ||
expect(body.items).toBeDefined(); | ||
}) | ||
.expect(200); | ||
}); | ||
} | ||
``` | ||
More details for the complete e2e tests, check Nestjs 's [test folder](https://github.com/hantsy/nestjs-sample/tree/master/test). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,30 @@ | ||
import { PageOptionsDto } from "@common/classes/pagination"; | ||
import { Order, Roles } from "@common/types/enums"; | ||
|
||
|
||
export const mockedUser = { | ||
idx: 'idx', | ||
username:'username', | ||
password:'password', | ||
firstName:'firstName', | ||
lastName:'lastName', | ||
email:'email', | ||
avatar:'avatar', | ||
roles:[Roles.ADMIN], | ||
mobileNumber:'mobileNumber', | ||
} | ||
|
||
|
||
idx: "idx", | ||
username: "username", | ||
password: "password", | ||
firstName: "firstName", | ||
lastName: "lastName", | ||
email: "email", | ||
avatar: "avatar", | ||
roles: [Roles.ADMIN], | ||
mobileNumber: "mobileNumber", | ||
}; | ||
|
||
export const mockedPost = { | ||
slug: 'slug', | ||
title: 'title', | ||
description: 'description', | ||
content: 'content', | ||
tags: ['tag1', 'tag2'], | ||
} | ||
slug: "slug", | ||
title: "title", | ||
description: "description", | ||
content: "content", | ||
tags: ["tag1", "tag2"], | ||
}; | ||
|
||
export const query: PageOptionsDto = { | ||
page: 1, | ||
limit: 10, | ||
offset: 5, | ||
sort: 'createdAt', | ||
order: Order.DESC | ||
page: 1, | ||
limit: 10, | ||
offset: 5, | ||
sort: "createdAt", | ||
order: Order.DESC, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,4 @@ export class Comment extends BaseEntity { | |
super(); | ||
Object.assign(this, partial); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
// all entities should be exported from this barrel file | ||
|
||
export { ActivityLog } from "./activity-log.entity"; | ||
export { Comment } from "./comment.entity"; | ||
export { OtpLog } from "./otp-log.entity"; | ||
export { RefreshToken } from "./refresh-token.entity"; | ||
export { Post } from "./post.entity"; | ||
export { Protocol } from "./protocol.entity"; | ||
export { RefreshToken } from "./refresh-token.entity"; | ||
export { User } from "./user.entity"; | ||
export { Post } from "./post.entity"; | ||
export { Comment } from "./comment.entity"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,4 @@ export class Post extends BaseEntity { | |
Object.assign(this, partial); | ||
this.slug = slugify(partial.title); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { EntityRepository } from "@mikro-orm/postgresql"; | ||
|
||
import { User } from "./user.entity"; | ||
|
||
export class UserRepository extends EntityRepository<User> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.