I am trying to develop an application using NestJs as the backend framework. Currently I am writing some integration tests for the controllers.
This is my first project using typescript, I usually use Java/Spring but I wanted to learn and give nestJs a try.
I use different guards to access rest endpoints. In this case I have an AuthGuard and RolesGuard
To make the rest endpoint work I just add something like this in the TestingModuleBuilder:
.overrideGuard(AuthGuard())
.useValue({ canActivate: () => true })
The point is, is it possible to define or override this guards for each test to check that the request should fail if no guard or not allowed guard is defined?
My code for the test is the following one:
describe('AuthController integration tests', () => {
let userRepository: Repository<User>
let roleRepository: Repository<Role>
let app: INestApplication
beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [
AuthModule,
TypeOrmModule.forRoot(typeOrmConfigTest),
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.register({
secret: jwtConfig.secret,
signOptions: {
expiresIn: jwtConfig.expiresIn
}
})
]
})
.overrideGuard(AuthGuard())
.useValue({ canActivate: () => true })
.overrideGuard(RolesGuard)
.useValue({ canActivate: () => true })
.compile()
app = module.createNestApplication()
await app.init()
userRepository = module.get('UserRepository')
roleRepository = module.get('RoleRepository')
const initializeDb = async () => {
const roles = roleRepository.create([
{ name: RoleName.ADMIN },
{ name: RoleName.TEACHER },
{ name: RoleName.STUDENT }
])
await roleRepository.save(roles)
}
await initializeDb()
})
afterAll(async () => {
await roleRepository.query(`DELETE FROM roles;`)
await app.close()
})
afterEach(async () => {
await userRepository.query(`DELETE FROM users;`)
})
describe('users/roles (GET)', () => {
it('should retrieve all available roles', async () => {
const { body } = await request(app.getHttpServer())
.get('/users/roles')
.set('accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
expect(body).toEqual(
expect.arrayContaining([
{
id: expect.any(String),
name: RoleName.STUDENT
},
{
id: expect.any(String),
name: RoleName.TEACHER
},
{
id: expect.any(String),
name: RoleName.ADMIN
}
])
)
})
})
It's not immediately possibly with the current implementation, but if you save the guard mock as a jest mock it should be possible. Something like this
describe('Controller Integration Testing', () => {
let app: INestApplication;
const canActivate = jest.fn(() => true);
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
})
.overrideGuard(TestGuard)
.useValue({ canActivate })
.compile();
app = moduleFixture.createNestApplication();
await app.init();
});
it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!');
});
it('/ (GET) Fail guard', () => {
canActivate.mockReturnValueOnce(false);
return request(app.getHttpServer())
.get('/')
.expect(403);
});
});
Related
do I need to write a test for the below code?
#Post()
update(#Body() updateUserDto: UpdateUserDto, #Request() req) {
return this.userService.update(req.user.user, updateUserDto);
}
I'm going to make some assumptions about injected properties of the controller class. For full setups, you can view my testing example repo here. This should be essentially what you're looking for though
describe('UserController (Unit)', () => {
let controller: UserController;
let service: MockedClass<UserService>;
beforeAll(async () => {
const modRef = await Test.createTestingModule({
controllers: [UserController],
providers: [
{
provide: UserService,
useValue: {
update: jest.fn().mockReturnValue({ hello: 'world' }),
},
}
],
}).compile();
controller = modRef.get(UserController);
service = modRef.get(UserService);
});
it('should call update and return the value', () => {
const bodyVal = objectThatMatchesUpdateUserDto;
const reqVal = {
user: {
user: valueThatMatchesReqUserUser
}
};
const res = controller.update(bodyVal, reqVal);
expect(res).toEqual({ hello: 'world' });
expect(service.update).toBeCalledWith(reqVal.user.user, bodyVal)
});
});
i am writing test functions in nestjs and i am new to this task.
I mostly had problems with typeorm. but my current problem is that I do not call the paginate function in the PaginateLib class inside the function I wrote in the service. I tested the userList function directly before, but it gave paginate undefined error. Now I used mock and paginate function in test. it still gives undefined error.
Here's my admin.service code
public async userList(page, limit): Promise<any> {
const opt = {
relations: ['userInfo'],
};
const userData = await this.paginateLib.paginate(
getRepository(Users),
opt,
page,
limit,
);
return userData;
}
Here's Paginate function code in PaginateLib class. I'm cutting this short as I don't think it's very necessary.
export class PaginateLib {
async paginate(repo, opt, page, limit) {
try {
page = Number(page);
limit = Number(limit);
const items = (Number(page) - 1) * Number(limit);
const [data, count] = await repo.findAndCount({
...opt,
skip: Number(items),
take: Number(limit),
});
if (count <= items) {
return [];
}
and here's my test code
class PaginateMock {
paginate(repo: any, opt: any, page: number, limit: number) {
return [];
}
}
describe('AdminService', () => {
let service: AdminService;
let connection: Connection;
let module: TestingModule;
let services: PaginateLib;
beforeAll(async () => {
const ApiServiceProvider = {
provide: PaginateLib,
useClass: PaginateMock,
},
module = await Test.createTestingModule({
imports: [
TypeOrmModule.forRoot({
type: '****',
host: '****',
port: ****,
username: '****',
password: '****',
database: '****',
entities: [__dirname + '/../../**/*.entity.ts'],
synchronize: true,
}),
],
providers: [
AdminService,
ApiServiceProvider,
],
}).compile();
service = module.get<AdminService>(AdminService);
services = module.get<PaginateLib>(PaginateLib);
});
// afterAll(async () => {
// await module.close();
// });
describe('User Info', () => {
it('should be get user list', async () => {
const paginateSpy = jest.spyOn(services, 'paginate');
expect(paginateSpy).toHaveBeenCalled();
expect(true).toBe(service.userList(1, 1));
});
});
});
I tried different things many times. but the error I get is undefined. what should I do? Is there anyone have an idea?thank you
Try this:
describe('User Info', () => {
it('should be get user list', async () => {
const paginateSpy = jest.spyOn(services, 'paginate');
const userData = await service.userList(1, 1);
expect(paginateSpy).toHaveBeenCalled();
expect(userData).toBe(); // Don't know what userData actually looks like. So this is up to you
});
});
I want to mock stripe apis but unable to figure out how to do it. I'm creating the session using below code-
const stripe = require('stripe')('key');
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [
{
price_data: {
currency: 'usd',
product_data: {
name: 'abs',
images: ['url'],
},
unit_amount: 100,
},
quantity: 1,
},
],
mode: 'payment',
success_url: `${YOUR_DOMAIN}?success=true&session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${YOUR_DOMAIN}?back=true&order_id=${req.clientReferenceId}`,
metadata: req.metadata,
client_reference_id: req.clientReferenceId,
customer_email: req.customerEmail,
billing_address_collection: 'required',
});
I cannot import stripe in test file as it will require API key. How to do this?
PS- I tried by following this - Jest to mock Stripe but it is not working for me.
stripe exported as a function, when you want to mock it you can use jest.doMock helper.
Imagine that you have an index.js like this:
const stripe = require('stripe')('key'); // You use require('stripe') as a function
exports.CreateSession = async (req) => {
const session = await stripe.checkout.sessions.create({
// information to create session
});
return session;
}
Then, testing file will be like this:
describe('Create session', () => {
beforeEach(() => {
jest.resetModules(); // importance line
});
it('creates the session', async () => {
const req = {
unitAmount: 100,
imageUrl: '',
metadata: {
userId: '1'
}
};
jest.doMock('stripe', () => {
// instead of mocking return an object, let’s return a function
return jest.fn(() => ({ // when the function be called, it return an object like this
checkout: {
sessions: {
create: jest.fn(() => Promise.resolve({
sessionId: '123' // sessionId instead of id , right?
})),
},
},
}));
});
const { CreateSession } = require('./index'); // import function what you need to test
const resp = await CreateSession(req); // execute with a parameter
expect(resp.sessionId).toBe('123');
});
});
You need to create the stripe mock before use it:
jest.mock("stripe", ()=> ({
checkout: {
sessions: {
create: jest.fn(()=> Promise.resolve()) //--> resolve data you are expecting
}
}
}));
Also you can use the Manual Mocks
__mocks__/stripe.js
const stripe = {
checkout: {
sessions: {
create: jest.fn(()=> Promise.resolve()) //--> resolve data you are expecting
}
}
};
export default stripe;
In this project, it uses NestJS along with TypeORM. For real API requests, CRUD operation is being operated on MySQL(which is using AWS RDS).
Now I am trying to use SQLite(In-Memory) to test API results.
I successfully implemented this in Unit Test, as the code below.
First, below is create-memory-db.ts, which returns a connection to in-memory SQLite database.
type Entity = Function | string | EntitySchema<any>;
export async function createMemoryDB(entities: Entity[]) {
return createConnection({
type: 'sqlite',
database: ':memory:',
entities,
logging: false,
synchronize: true,
});
}
And by using the exported function above, I successfully ran Unit test, like below.
describe('UserService Logic Test', () => {
let userService: UserService;
let connection: Connection;
let userRepository: Repository<User>;
beforeAll(async () => {
connection = await createMemoryDB([User]);
userRepository = await connection.getRepository(User);
userService = new UserService(userRepository);
});
afterAll(async () => {
await connection.close();
});
afterEach(async () => {
await userRepository.query('DELETE FROM users');
});
// testing codes.
});
I am trying to do the same thing on e2e tests. I tried below code.
// user.e2e-spec.ts
describe('UserController (e2e)', () => {
let userController: UserController;
let userService: UserService;
let userRepository: Repository<User>;
let connection: Connection;
let app: INestApplication;
const NAME = 'NAME';
const EMAIL = 'test#test.com';
const PASSWORD = '12345asbcd';
beforeAll(async () => {
connection = await createMemoryDB([User]);
userRepository = await connection.getRepository(User);
userService = new UserService(userRepository);
userController = new UserController(userService);
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [],
controllers: [UserController],
providers: [UserService],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
afterAll(async () => {
await connection.close();
});
afterEach(async () => {
// await userRepository.query('DELETE FROM users');
});
it('[POST] /user : Response is OK if conditions are right', () => {
const dto = new UserCreateDto();
dto.name = NAME;
dto.email = EMAIL;
dto.password = PASSWORD;
return request(app.getHttpServer())
.post('/user')
.send(JSON.stringify(dto))
.expect(HttpStatus.CREATED);
});
});
I cannot create UserModule since it doesn't have a constructor with Connection parameter.
The code itself has no compile error, but gets results below when e2e test is executed.
Nest can't resolve dependencies of the UserService (?). Please make sure that the argument UserRepository at index[0] is available in the RootTestModule context.
Potential solutions:
- If UserRepository is a provider, is it part of the current RootTestModule?
- If UserRepository is exported from a seperate #Module, is that module imported within RootTestModule?
#Module({
imports: [/* The module containing UserRepository */]
})
TypeError: Cannot read property 'getHttpServer' of undefined.
Any help would be greatly appreciated. Thanks :)
UPDATE : New error occured after trying below.
describe('UserController (e2e)', () => {
let userService: UserService;
let userRepository: Repository<User>;
let connection: Connection;
let app: INestApplication;
const NAME = 'NAME';
const EMAIL = 'test#test.com';
const PASSWORD = '12345asbcd';
beforeAll(async () => {
connection = await createMemoryDB([User]);
userRepository = await connection.getRepository(User);
userService = new UserService(userRepository);
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [UserModule],
})
.overrideProvider(UserService)
.useClass(userService)
.compile();
app = moduleFixture.createNestApplication();
await app.init();
});
afterAll(async () => {
await connection.close();
});
afterEach(async () => {
await userRepository.query('DELETE FROM users');
});
it('[POST] /user : Response is OK if conditions are right', async () => {
const dto = new UserCreateDto();
dto.name = NAME;
dto.email = EMAIL;
dto.password = PASSWORD;
const result = await request(app.getHttpServer())
.post('/user')
.send(JSON.stringify(dto))
.expect({ status: HttpStatus.CREATED });
});
});
I checked if query is working, and was able to see that it is using SQLite database as I wanted. But new error appeared in console.
TypeError: metatype is not a constructor.
TypeError: Cannot read property 'getHttpServer' of undefined.
Okay, I solved this issue by using TypeOrm.forRoot() inside the imports field of Test.createTestingModule. Below is how I did it.
describe('UserController (e2e)', () => {
let userService: UserService;
let userRepository: Repository<User>;
let app: INestApplication;
const NAME = 'NAME';
const EMAIL = 'test#test.com';
const PASSWORD = '12345asbcd';
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [
UserModule,
TypeOrmModule.forRoot({
type: 'sqlite',
database: ':memory:',
entities: [User],
logging: true,
synchronize: true,
}),
],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
userRepository = moduleFixture.get('UserRepository');
userService = new UserService(userRepository);
});
afterAll(async () => {
await app.close();
});
afterEach(async () => {
await userRepository.query('DELETE FROM users');
});
});
For those looking for setup e2e tests that hit endpoints and assert the response body, you can do something like this:
// app.module.ts
#Module({
imports: [
TypeOrmModule.forRootAsync({
useFactory: async (configService: ConfigService) => {
if (process.env.APPLICATION_ENV === 'test') {
return {
type: 'sqlite',
database: ':memory:',
entities: [Entity],
synchronize: true,
}
}
return {
// your default options
};
},
}),
]
})
I would like to be able to test my Nest service against an actual database. I understand that most unit tests should use a mock object, but it also, at times, makes sense to test against the database itself.
I have searched through SO and the GH issues for Nest, and am starting to reach the transitive closure of all answers. :-)
I am trying to work from https://github.com/nestjs/nest/issues/363#issuecomment-360105413. Following is my Unit test, which uses a custom provider to pass the repository to my service class.
describe("DepartmentService", () => {
const token = getRepositoryToken(Department);
let service: DepartmentService;
let repo: Repository<Department>;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
DepartmentService,
{
provide: token,
useClass: Repository
}
]
}).compile();
service = module.get<DepartmentService>(DepartmentService);
repo = module.get(token);
});
Everything compiles properly, TypeScript seems happy. However, when I try to execute create or save on my Repository instance, the underlying Repository appears to be undefined. Here's the stack backtrace:
TypeError: Cannot read property 'create' of undefined
at Repository.Object.<anonymous>.Repository.create (repository/Repository.ts:99:29)
at DepartmentService.<anonymous> (relational/department/department.service.ts:46:53)
at relational/department/department.service.ts:19:71
at Object.<anonymous>.__awaiter (relational/department/department.service.ts:15:12)
at DepartmentService.addDepartment (relational/department/department.service.ts:56:16)
at Object.<anonymous> (relational/department/test/department.service.spec.ts:46:35)
at relational/department/test/department.service.spec.ts:7:71
It appears that the EntityManager instance with the TypeORM Repository class is not being initialized; it is the undefined reference that this backtrace is complaining about.
How do I get the Repository and EntityManager to initialize properly?
thanks,
tom.
To initialize typeorm properly, you should just be able to import the TypeOrmModule in your test:
Test.createTestingModule({
imports: [
TypeOrmModule.forRoot({
type: 'mysql',
// ...
}),
TypeOrmModule.forFeature([Department])
]
I prefer not using #nestjs/testing for the sake of simplicity.
First of all, create a reusable helper:
/* src/utils/testing-helpers/createMemDB.js */
import { createConnection, EntitySchema } from 'typeorm'
type Entity = Function | string | EntitySchema<any>
export async function createMemDB(entities: Entity[]) {
return createConnection({
// name, // let TypeORM manage the connections
type: 'sqlite',
database: ':memory:',
entities,
dropSchema: true,
synchronize: true,
logging: false
})
}
Then, write test:
/* src/user/user.service.spec.ts */
import { Connection, Repository } from 'typeorm'
import { createMemDB } from '../utils/testing-helpers/createMemDB'
import UserService from './user.service'
import User from './user.entity'
describe('User Service', () => {
let db: Connection
let userService: UserService
let userRepository: Repository<User>
beforeAll(async () => {
db = await createMemDB([User])
userRepository = await db.getRepository(User)
userService = new UserService(userRepository) // <--- manually inject
})
afterAll(() => db.close())
it('should create a new user', async () => {
const username = 'HelloWorld'
const password = 'password'
const newUser = await userService.createUser({ username, password })
expect(newUser.id).toBeDefined()
const newUserInDB = await userRepository.findOne(newUser.id)
expect(newUserInDB.username).toBe(username)
})
})
Refer to https://github.com/typeorm/typeorm/issues/1267#issuecomment-483775861
Here's an update to the test that employs Kim Kern's suggestion.
describe("DepartmentService", () => {
let service: DepartmentService;
let repo: Repository<Department>;
let module: TestingModule;
beforeAll(async () => {
module = await Test.createTestingModule({
imports: [
TypeOrmModule.forRoot(),
TypeOrmModule.forFeature([Department])
],
providers: [DepartmentService]
}).compile();
service = module.get<DepartmentService>(DepartmentService);
repo = module.get<Repository<Department>>(getRepositoryToken(Department));
});
afterAll(async () => {
module.close();
});
it("should be defined", () => {
expect(service).toBeDefined();
});
// ...
}
I created a test orm configuration
// ../test/db.ts
import { TypeOrmModuleOptions } from '#nestjs/typeorm';
import { EntitySchema } from 'typeorm';
type Entity = Function | string | EntitySchema<any>;
export const createTestConfiguration = (
entities: Entity[],
): TypeOrmModuleOptions => ({
type: 'sqlite',
database: ':memory:',
entities,
dropSchema: true,
synchronize: true,
logging: false,
});
which I then utilize when setting up the tests
// books.service.test.ts
import { Test, TestingModule } from '#nestjs/testing';
import { HttpModule, HttpService } from '#nestjs/common';
import { TypeOrmModule, getRepositoryToken } from '#nestjs/typeorm';
import { Repository } from 'typeorm';
import { BooksService } from './books.service';
import { Book } from './book.entity';
import { createTestConfiguration } from '../../test/db';
describe('BooksService', () => {
let module: TestingModule;
let service: BooksService;
let httpService: HttpService;
let repository: Repository<Book>;
beforeAll(async () => {
module = await Test.createTestingModule({
imports: [
HttpModule,
TypeOrmModule.forRoot(createTestConfiguration([Book])),
TypeOrmModule.forFeature([Book]),
],
providers: [BooksService],
}).compile();
httpService = module.get<HttpService>(HttpService);
service = module.get<BooksService>(BooksService);
repository = module.get<Repository<Book>>(getRepositoryToken(Book));
});
afterAll(() => {
module.close();
});
it('should be defined', () => {
expect(service).toBeDefined();
});
This allows to query the repository after the tests and ensure that the correct data was inserted.
I usually import AppModule for database connection, and finally after tests are executed I close the connection:
let service: SampleService;
let connection: Connection;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [AppModule, TypeOrmModule.forFeature([SampleEntity])],
providers: [SampleService],
}).compile();
service = module.get<SampleService>(SampleService);
connection = await module.get(getConnectionToken());
});
afterEach(async () => {
await connection.close();
});
I used existing AppModule for testing:
import { INestApplication } from '#nestjs/common';
import { NestFactory } from '#nestjs/core';
import { AppModule } from '../../../app.module';
import { AuthService } from '../auth.service';
describe('AuthService', () => {
let app: INestApplication;
let service: AuthService;
beforeAll(async () => {
app = await NestFactory.create(AppModule);
service = app.get(AuthService);
});
afterAll(async () => {
await app.close();
});
it('AuthService should be defined', () => {
expect(service).toBeDefined();
});
describe('login', () => {
it('should login user', async () => {
const user = await service.login({ email: 'xxxzei#mail.ru', password: '12345678' });
expect(user.id).toBeDefined();
});
});
});
Also to add env variables in your configuration file or AppModule:
import { config as testConfig } from 'dotenv';
if (process.env.NODE_ENV === 'test') {
testConfig({ path: resolve('./.env') });
}