I'm trying to test a service in my Nest.js app. In some of my methods of this service I have some cases where I throw new HttpException depending of the context.
I'm trying to write unit tests of these methods and I don't know how to test cases where I throw HttpException.
I've tried this code:
it('Shound return error for a non existing id provided', async () => {
await expect(service.getUser('false Id')).rejects.toThrow(new HttpException('This user does not exist', 404));
});
But I received:
Expected the function to throw an error matching:
[Error: This user does not exist]
Instead, it threw:
Error: This user does not exist
Does anyone already encounter this use case ?
Duc answer works but if you want some prettier way you might like this,
it('should return null when update', async () => {
await expect(controller.updateById({ id: 'some id' }, {}))
.rejects.toThrowError(NotFoundException);
});
Simply do this:
it('should return null when update', async (done) => {
jest.spyOn(service, 'updateById').mockResolvedValue(null)
try {
await controller.updateById({ id: 'some id' }, {})
} catch (error) {
expect(error).toBeInstanceOf(NotFoundException)
done()
}
})
Related
I am attempting to implement unit testing in NestJS. However, I am new to both NestJS and unit testing in general.
So, what I need to do is to pass a test that gets all projects from a gitlab profile.
#Get()
getProjects(#Headers() headers: any): Observable<Project[]> {
if (!headers.token) {
throw new HttpException('Missing User Token', HttpStatus.BAD_REQUEST);
}
return this.projectsService.listProjects(headers.token);
}
This is the code in the project.controller.ts file. It works well, and returns an observable with the list of projects. However, it requires a header to be sent through the HTTP request(Which is received using the #Headers decorator), as the gitlab is locked behind an authentication that requires a token to be passed through the header of the request.
I have attempted to mock the header to no success, and I was wondering if anyone has any idea how to proceed with the unit testing for that get request.
You don't need anything special here. In your unit test, you just need to pass an object with a token property to your method call.
describe('SomeController', () => {
let controller: SomeController;
let service: jest.Mocked<SomeService>;
beforeAll(async () => {
const modRef = await Test.createTestingModule({
controllers: [SomeController],
providers: [{
provide: SomeService,
useValue: {
method: jest.fn(() => of('value'))
}
}]
}).compile();
controller = modRef.get(SomeController)
service = modRef.get<jest.Mocked<SomeService>>(SomeService)
});
it('getProjects', (done) => {
controller.getProejcts({ token: 'hey look! a token'}).subscribe({
next: (val) => expect(val).toBe('value'),
error: (err) => { throw err; }
complete: () => done()
});
});
})
I have a backend server using Node and Express.js and I'm using Jest as my test framework. I had a problem that I couldn't figure out. Let me share my code first:
// user.model.test.ts
describe('User model test', () => {
let connection: Connection;
beforeEach(async () => {
connection = await createConnection(ormConfig);
});
afterEach(async () => {
connection.close();
});
it('should not create a user with the same email', async () => {
await User.create({
username: 'bar',
email: 'foo#gmail.com',
password: 'password'
}).save();
const user2 = User.create({
username: 'foo',
email: 'foo#gmail.com',
password: 'password'
});
await expect(user2.save()).rejects.toThrowError();
});
}
As you can see here I'm creating a separate connection from my development database here the hooks are attached to test_db.
I have another test on controllers.
user.controller.test.ts
describe('Get all users', () => {
let connection: Connection;
beforeEach(async () => {
connection = await createConnection(ormConfig);
});
afterEach(async () => {
connection.close();
});
it('should return status code 200', async (done) => {
User.create({
username: 'test',
email: 'foobar#gmail.com',
password: 'password'
});
const res = await rekwest.get('/api/v1/users');
expect(res.status).toBe(200);
// console.log(res.st);
done();
});
});
Same thing what I'm doing here. I'm creating a test and attaching to test_db and expecting a different connection pool.
Now the error on the controller is weird User table already exists. And on the model I have an error of test_db doesn't exists. Now if I removed the hooks in the controller test the errors goes away but I can't test the controller.
So I found a link on how to test a MongoDB using Jest we have the same idea but I don't have the runInBand. So I added it jest --runInBand and for some reason, it fixes my errors. Can someone explain to me what happened? In the Jest documentation basically it's saying that your test will run 50% faster but I don't need that or do I?
I use axios for the api calls in my react redux app. I added this interceptor to intercept the response for all the api calls and redirect user to login if the status code is 401.
axios.interceptors.response.use((response) => {
if (response.status === 401) {
browserHistory.push('/login');
}
return response;
});
I am unit testing this and asserting on browserHistory spy to get called but I am not sure why my test keeps failing. I'm kind of stuck after loads of tries and am not able to sort out the issue. I am using moxios for mocking response.
Test
let sandbox;
beforeEach(() => {
moxios.install();
sandbox = sinon.sandbox.create();
});
afterEach(() => {
moxios.uninstall();
sandbox.restore();
});
describe('interceptors', () => {
it('should redirect to login if response status code is 401', (done) => {
moxios.withMock(() => {
sandbox.spy(browserHistory, 'push');
axios.get('/test');
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request.respondWith({
status: 401,
response: {}
}).then(() => {
expect(browserHistory.push).to.have.been.called; // Fails
done();
}).catch(done);
});
});
});
});
Any sort of resources or guide would be really appreciated. I am pretty sure I am missing something here.
I'm trying to test a service that I've created that makes an API call with vue-resource. The service should make the call and update the component with the new data, however in my tests it doesn't register the updated value. I'm using the same setup as the vue-cli webpack example and have based my auth service off this repo (which unfortunately doesn't have any tests)
my service:
export default {
login(context, creds){
context.$http.post(LOGIN_URL, creds)
.then((response) => {
//do something else here
}, (response) => {
context.error = response.data.error
}
}
}
my test:
import Vue from 'vue'
import VueResource from 'vue-resource'
Vue.use(VueResource)
import Auth from 'src/auth'
describe('Auth', () => {
it('should throw an error on unsuccessful login', (done) => {
//intercept the api call and force an invalid response
Vue.http.interceptors.unshift((request, next) => {
next(request.respondWidth({error: 'some error'}, {status: 401}));
});
const vm = new Vue({
data: {
error: ''
}
}).$mount()
Auth.login(vm, {email: 'test#test.com', pass: 'test'} )
//this always fails
expect(vm.error).to.equal('some error')
//ive also tried:
vm.$nextTick(() => {
expect(vm.error).to.equal('some error')
//done()
});
//undo our interceptor
Vue.http.interceptors.shift()
}
}
When I run the test it fails because it's expecting '' to equal 'some error'.
My suspicions are around the fact that vue-resource is using promises.
After reading through some of the Vue.js tests I found my answer. Instead of using vm.$nextTick I did the following:
setTimeout(function(){
expect(vm.error).to.equal('something')
done()
}, 0)
I keep getting an error in the save() method when I run the test.
var User = require('../../models/user')
, should = require('should');
describe('User', function(){
describe('#save()', function(){
it('should save without error', function(done){
var user = new User({
username : 'User1'
, email : 'user1#example.com'
, password : 'foo'
});
user.save(function(err, user){
if (err) throw err;
it('should have a username', function(done){
user.should.have.property('username', 'User1');
done();
});
});
})
})
})
here is the error:
$ mocha test/unit/user.js
․
✖ 1 of 1 test failed:
1) User #save() should save without error:
Error: timeout of 2000ms exceeded
at Object.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:1
61:14)
at Timer.list.ontimeout (timers.js:101:19)
You can nest describes but not it tests. Each test is meant to be stand alone so when you are looking through your results you can see where it is failing - on the save or not having the username property. For example in your code above there is no way for it to fail the 'should save without error' test as there is no done(). Which is also the reason the code above is timing out: mocha cannot find the done() for the 'should save without error' test.
Being able to nest describes is very powerful though. Within each describe you can have a before, beforeEach, after and afterEach statement. With these you can achieve the nesting that you are attempting above. See the mocha docs for more information if you want to read up on these statements.
The way I would write what you are trying to achieve is as follows:
var User = require('../../models/user')
, should = require('should')
// this allows you to access fakeUser from each test
, fakeUser;
describe('User', function(){
beforeEach(function(done){
fakeUser = {
username : 'User1'
, email : 'user1#example.com'
, password : 'foo'
}
// this cleans out your database before each test
User.remove(done);
});
describe('#save()', function(){
var user;
// you can use beforeEach in each nested describe
beforeEach(function(done){
user = new User(fakeUser);
done();
}
// you are testing for errors when checking for properties
// no need for explicit save test
it('should have username property', function(done){
user.save(function(err, user){
// dont do this: if (err) throw err; - use a test
should.not.exist(err);
user.should.have.property('username', 'User1');
done();
});
});
// now try a negative test
it('should not save if username is not present', function(done){
user.username = '';
user.save(function(err, user){
should.exist(err);
should.not.exist(user.username);
done();
});
});
});
});