I am not sure I how to write unit test case file for guard in nestjs. I have below Role.guard.ts file. I have to create Role.guard.spec.ts file. can somebody help please?
import { Injectable, CanActivate, ExecutionContext, Logger } from '#nestjs/common';
import { Reflector } from '#nestjs/core';
import { ROLES_KEY } from './roles.decorator';
import { Role } from './role.enum';
#Injectable()
export class RolesGuard implements CanActivate {
constructor(private reflector: Reflector) {}
canActivate(context: ExecutionContext): boolean {
const requiredRoles = this.reflector.getAllAndOverride<string[]>(ROLES_KEY, [
context.getHandler(),
context.getClass(),
]);
const { user } = context.switchToHttp().getRequest();
if (!user.role) {
Logger.error('User does not have a role set');
return false;
}
if (user.role === Role.Admin) {
return true;
}
if (!Array.isArray(requiredRoles) || !requiredRoles.length) {
// No #Roles() decorator set, deny access as not admin
return false;
}
if (requiredRoles.includes(Role.All)) {
return true;
}
return requiredRoles.includes(user.role);
}
}
I wrote below code but coverage issue is coming.
import { createMock } from '#golevelup/ts-jest';
import { ExecutionContext } from "#nestjs/common";
import { Reflector } from "#nestjs/core";
import { RolesGuard } from "./roles.guard";
describe('RolesGuard', () => {
let guard: RolesGuard;
let reflector: Reflector;
beforeEach(() => {
reflector = new Reflector();
guard = new RolesGuard(reflector);
});
it('should be defined', () => {
expect(guard).toBeDefined();
});
it('should return false if user does not exist', () => {
reflector.getAllAndOverride = jest.fn().mockReturnValue(true);
const context = createMock<ExecutionContext>();
const canActivate = guard.canActivate(context);
expect(canActivate).toBe(false);
})
})
below lines are not getting covered.
if (user.role === Role.Admin) {
return true;
}
if (!Array.isArray(requiredRoles) || !requiredRoles.length) {
// No #Roles() decorator set, deny access as not admin
return false;
}
if (requiredRoles.includes(Role.All)) {
return true;
}
return requiredRoles.includes(user.role);
Edit 1:-
Below test cases are covering my some part of code.
it('should return true if user exist', () => {
reflector.getAllAndOverride = jest.fn().mockReturnValue(true);
const context = createMock<ExecutionContext>({
switchToHttp: () => ({
getRequest: () => ({
user: {
role:'admin'
}
}),
}),
});
const canActivate = guard.canActivate(context);
expect(canActivate).toBe(true);
})
it('should return false if user does not exist', () => {
reflector.getAllAndOverride = jest.fn().mockReturnValue(true);
const context = createMock<ExecutionContext>({
switchToHttp: () => ({
getRequest: () => ({
user: {
role:'user'
}
}),
}),
});
const canActivate = guard.canActivate(context);
expect(canActivate).toBe(false);
})
But below code still not getting covered.
if (requiredRoles.includes(Role.All)) {
return true;
}
return requiredRoles.includes(user.role);
}
Can sombody help me on the same?
Looks like you need to be able to craft the payload appropriately such that your code is hit. There are a variety of ways to do this but, in a "Nest"-y way, we can try something like what the docs tell us. Notice in that link that the provided testing utilities make this a lot easier to mock.
import { createMock } from '#golevelup/ts-jest';
import { ExecutionContext } from "#nestjs/common";
import { Reflector } from "#nestjs/core";
import { RolesGuard } from "./roles.guard";
describe('RolesGuard', () => {
let guard: RolesGuard;
let reflector: Reflector
beforeEach(async () => {
reflector = new Reflector();
guard = new RolesGuard(reflector);
});
it('should be defined', () => {
expect(guard).toBeDefined();
});
it('should return false if user does not exist', () => {
reflector.getAllAndOverride = jest.fn().mockReturnValue(true);
const context = createMock<ExecutionContext>();
const canActivate = guard.canActivate(context);
expect(canActivate).toBe(false);
})
it('should return false if user does exist', () => {
reflector.getAllAndOverride = jest.fn().mockReturnValue(true);
// Mock the "class" type of this so we can get what we want.
// We want to tell it to return an object where role is defined.
const context = createMock<ExecutionContext>({
switchToHttp: () => ({
getRequest: () => ({
user: { role: { /* enter your data here */ }
}),
}),
});
const canActivate = guard.canActivate(context);
expect(canActivate).toBe(false);
})
})
From here your context is successfully hydrated with whatever you want and the second test should start showing up as covering your other code branches. You can now edit the role attribute to look however you want. Notice also in the above beforeEach call you should be able to switch to using testing modules instead. This is not exhaustive though, you'll likely need to add additional test cases to cover your other branches. If you follow as I've done here, that should be relatively trivial.
Does what I've done here make sense? By crafting a custom payload to the object, the roles attribute is present, allowing the test to evaluate other cases. I got my override from the createMock function directly from the docs for golevelup.
Related
I want to write a unit test for a controller in NestJS which uses the service. Service uses an entity and typeorm to getting data from postgres.
controller.spec.ts
import { Test, TestingModule } from '#nestjs/testing';
import { EmissionsWeldingController } from '../EmissionsWelding.controller';
describe('EmissionsWeldingController', () => {
let controller: EmissionsWeldingController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [EmissionsWeldingController],
}).compile();
controller = module.get<EmissionsWeldingController>(EmissionsWeldingController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});
It failed with an error: Nest can't resolve dependencies of the EmissionsWeldingController (?). Please make sure that the argument EmissionsWeldingService at index [0] is available in the RootTestModule context.
When I define my service and entity
import { Test, TestingModule } from '#nestjs/testing';
import { TypeOrmModule } from '#nestjs/typeorm';
import { EmissionsWeldingController } from '../EmissionsWelding.controller';
import { Mark, Substance, WorkType, WorkSpecification, InputField, SummaryValue } from '../EmissionsWelding.entity';
import { EmissionsWeldingService } from '../EmissionsWelding.service';
describe('EmissionsWeldingController', () => {
let controller: EmissionsWeldingController;
let service: EmissionsWeldingService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [TypeOrmModule.forFeature([Mark, Substance, WorkType, WorkSpecification, InputField, SummaryValue])],
controllers: [EmissionsWeldingController],
providers: [EmissionsWeldingService],
}).compile();
controller = module.get<EmissionsWeldingController>(EmissionsWeldingController);
service = module.get<EmissionsWeldingService>(EmissionsWeldingService);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});
It's failing with an error: Nest can't resolve dependencies of the MarkRepository (?). Please make sure that the argument Connection at index [0] is available in the TypeOrmModule context.
How should I define providers and entities without getting above error?
service.ts
import { Injectable } from '#nestjs/common';
import { InjectRepository } from '#nestjs/typeorm';
import { Repository } from 'typeorm';
import { GetResultDto } from './dto/GetResult.dto';
import { InputField, Mark, Substance, SummaryValue, WorkSpecification, WorkType } from './EmissionsWelding.entity';
#Injectable()
export class EmissionsWeldingService {
constructor(
#InjectRepository(Mark)
private markRepository: Repository<Mark>,
#InjectRepository(Substance)
private substanceRepository: Repository<Substance>,
#InjectRepository(WorkSpecification)
private workSpecificationRepository: Repository<WorkSpecification>,
#InjectRepository(WorkType)
private workTypeRepository: Repository<WorkType>,
#InjectRepository(InputField)
private inputFieldRepository: Repository<InputField>,
#InjectRepository(SummaryValue)
private summaryValueRepository: Repository<SummaryValue>,
) {}
async getMarks(work_type_id: number, work_specification_id: number): Promise<Mark[]> {
return await this.markRepository.find({ where: { work_type_id, work_specification_id } });
}
async getSubstances(): Promise<Substance[]> {
return await this.substanceRepository.find();
}
async getWorkSpecifications(): Promise<WorkSpecification[]> {
return await this.workSpecificationRepository.find();
}
async getWorkTypes(): Promise<WorkType[]> {
return await this.workTypeRepository.find();
}
async getInputFields(): Promise<WorkType[]> {
return await this.inputFieldRepository.find();
}
async getSummaryValues(mark_id: number, substance_id: number): Promise<SummaryValue[]> {
return await this.summaryValueRepository.find({ where: { mark_id, substance_id } });
}
async getResult(body: GetResultDto): Promise<GetResultDto[]> {
const result = [];
const { mark_id, input_fields_values } = body;
const substances = await this.getSubstances();
let currentSummaryValue;
for (let i = 0; i <= substances.length - 1; i++) {
currentSummaryValue = await this.getSummaryValues(mark_id, i + 1);
result.push({
code: substances[i].code,
name: substances[i].name,
year:
((input_fields_values.year * currentSummaryValue[0].value) / 10 ** 6) *
(1 - (input_fields_values.clean ? input_fields_values.clean : 0)),
second:
((currentSummaryValue[0].value * input_fields_values.hour) / 3600) *
(1 - (input_fields_values.clean ? input_fields_values.clean : 0)),
});
}
return result;
}
}
controller.ts
import { Body, Controller, Get, Post, Query } from '#nestjs/common';
import { ApiTags } from '#nestjs/swagger';
import { GetResultDto } from './dto/GetResult.dto';
import { EmissionsWeldingService } from './EmissionsWelding.service';
#ApiTags('EmissionsWelding')
#Controller('EmissionsWelding')
export class EmissionsWeldingController {
constructor(private emissionsWeldingService: EmissionsWeldingService) {}
#Get('getMarks')
getMarks(#Query('work_type_id') work_type_id: number, #Query('work_specification_id') work_specification_id: number) {
return this.emissionsWeldingService.getMarks(work_type_id, work_specification_id);
}
#Get('getSubstances')
getSubstances() {
return this.emissionsWeldingService.getSubstances();
}
#Get('getWorkSpecifications')
getWorkSpecifications() {
return this.emissionsWeldingService.getWorkSpecifications();
}
#Get('getWorkTypes')
getWorkTypes() {
return this.emissionsWeldingService.getWorkTypes();
}
#Get('getInputFields')
getInputFields() {
return this.emissionsWeldingService.getInputFields();
}
#Post('getResult')
getResult(#Body() body: GetResultDto) {
return this.emissionsWeldingService.getResult(body);
}
}
You need to provide a mock for every repository that is injected into your service.
This answer provides the necessary detail: https://stackoverflow.com/a/55366343/588734
describe('EmissionsWeldingController', () => {
let controller: EmissionsWeldingController;
let service: EmissionsWeldingService;
let markRepositoryMock: MockType<Repository<Mark>>;
// repeat for every repository let __RepositoryMock: MockType<Repository<__>>;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [EmissionsWeldingController],
providers: [
EmissionsWeldingService,
{
provide: getRepositoryToken(Mark),
useValue: jest.fn(() => ({
findOne: jest.fn(entity => entity),
// provide a mock for each function you are calling
}))
},
// repeat for every mock
],
}).compile();
controller = module.get<EmissionsWeldingController>(EmissionsWeldingController);
service = module.get<EmissionsWeldingService>(EmissionsWeldingService);
markRepositoryMock = module.get(getRepositoryToken(Mark));
// repeat for every mock repository
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});
Friends, I have a problem!
I'm testing my API with NestJS.
I'm using Jest for the test. Unfortunately, I am encountering the following error:
TypeError: Cannot read properties of undefined (reading 'close')
This error is very explicit but I don't see where it could come from.
Would you have an idea?
My current code :
import * as pactum from 'pactum';
import { Test } from '#nestjs/testing';
import { AppModule } from '../src/app.module';
import { INestApplication, ValidationPipe } from '#nestjs/common';
import { PrismaService } from '../src/prisma/prisma.service';
import { AuthDto } from '../src/auth/dto';
describe('App e2e', () => {
let app: INestApplication;
let prisma: PrismaService;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile();
const app = moduleRef.createNestApplication();
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
}),
);
await app.init();
await app.listen(3334);
prisma = app.get(PrismaService);
await prisma.cleanDatabase();
});
afterAll(async () => {
console.log('Closing server');
await app.close(); // <------------- THE PROBLEM ARISES HERE.
});
describe('Auth', () => {
describe('Signup', () => {
it('should signup a user', () => {
const dto: AuthDto = {
email: 'darrel.doe#mail.com',
password: '1234',
};
return pactum
.spec()
.post('http://localhost:3333/auth/signup')
.withBody(dto)
.expectStatus(201);
});
});
describe('Signin', () => {
it.todo('should signin a user');
});
});
});
You are mixing two variables in different scopes.
let app: INestApplication; is upper scope one which you are actually using, since it does not have any value assigned it is undefined. The inner one is different because you are defining inside another scope.
A solution is very simple, just remove const from const app = moduleRef.createNestApplication();
I have never used Vuex with Nuxt.js, so I have met the problem. Here is my store/index.js file:
export const state = () => ({
wrongCredentials: false,
logged: false,
uxd: null
})
export const mutations = {
setWrongCredentials(state, value) {
state.wrongCredentials = value
},
setLogged(state, value) {
state.logged = value
},
setUxd(state, value) {
state.uxd = value
},
}
As you can see there is state and mutations. In my other file, where I check user's JWT token and, depending on resultM I want set values in store:
import jwt from 'jsonwebtoken'
import cert from '../jwt/public'
export default {
verify (token) {
jwt.verify(token, cert, async (err, decoded) => {
if (err) {
this.$store.state.wrongCredentials = true
this.$store.state.logged = false
this.$store.state.uxd = null
} else {
this.$store.state.wrongCredentials = false
this.$store.state.logged = true
this.$store.state.uxd = decoded.uxd
}
})
}
}
The code you see doesn't work correctly, it just doesn't set values, so, I have created mutations and did something like this:
await this.$store.dispatch('setWrongCredentials', true)
Also doesn't work. The problem is, I don't know how to work with Vuex store not in .vue files, so, how can I set values in store?
Unfortunately, I have not found solution of this problem like I wanted in .js file, so, here is another solution.
First of all, you need to set no only state and mutations, but also actions:
export const actions = {
fetchWrongCredentials(ctx, value) {
ctx.commit('setWrongCredentials', value)
},
fetchLogged(ctx, value) {
ctx.commit('setLogged', value)
},
fetchUxd(ctx, value) {
ctx.commit('setUxd', value)
}
}
And in your Vue component you need to set values in state using actions, just like that:
methods: {
...mapActions(['fetchWrongCredentials', 'fetchLogged', 'fetchUxd']),
async login() {
await login({
password: this.userPassword,
login: this.userLogin,
twoFactor: this.twoFactor
}).then(result => {
const jwtRes = jwt.verify(result.token)
this.fetchLogged(true)
this.fetchWrongCredentials(false)
this.fetchUxd(jwtRes.token)
}).catch(() => {
this.fetchLogged(false)
this.fetchWrongCredentials(true)
this.fetchUxd(null)
this.error = true
setTimeout(() => {
this.error = false
}, 1000)
})
}
}
In my case, I had to modify that .js file:
import jwt from 'jsonwebtoken'
import cert from '../jwt/public'
export default {
verify (token) {
return jwt.verify(token, cert, (err, decoded) => {
if (err) {
return false
} else {
return { token: decoded.uxd }
}
})
}
}
I am trying to test an axios request, and I need to use an auth token in order to access the endpoint, however my test fails because I am getting "Bearer null" and inputting this into my headers.Authorization. Here is my actual code below
File I'm testing:
this.$axios.get(url, { headers: { Authorization: `Bearer ${localStorage.getItem("access-token")}` } })
.then((response) => {
this.loading = true;
// Get latest barcode created and default it to our "from" input
this.barcodeFrom = response.data.data[response.data.data.length - 1]['i_end_uid'] + 1;
this.barcodeTo = this.barcodeFrom + 1;
this.barcodeRanges = response.data.data;
// Here we add to the data array to make printed barcodes more obvious for the user
this.barcodeRanges.map(item => item['range'] = `${item['i_start_uid']} - ${item['i_end_uid']}`);
// Make newest barcodes appear at the top
this.barcodeRanges.sort((a, b) => new Date(b['created_at']) - new Date(a['created_at']));
})
.catch((error) => {
console.log('Barcode retrieval error:', error);
this.barcodeFrom === 0 ? null : this.snackbarError = true;
})
.finally(() => {
// Edge case when there's no barcode records
this.barcodeFrom === 0 ? this.barcodeTo = 1 : null;
this.loading = false
});
console.log('bcr', this.barcodeRanges);
Test file:
import Vuetify from "vuetify";
import Vuex from "vuex";
import { createLocalVue, shallowMount } from "#vue/test-utils";
import VueMobileDetection from "vue-mobile-detection";
import axios from 'axios';
import index from "#/pages/barcode_logs/index";
describe('/pages/barcode_logs/index.vue', () => {
// Initialize our 3rd party stuff
const localVue = createLocalVue();
localVue.use(Vuetify);
localVue.use(Vuex);
localVue.use(axios);
localVue.use(VueMobileDetection);
// Initialize store
let store;
// Create store
store = new Vuex.Store({
modules: {
core: {
state: {
labgroup:{
current: {
id: 1
}
}
}
}
}
});
// Set-up wrapper options
const wrapperOptions = {
localVue,
store,
mocks: {
$axios: {
get: jest.fn(() => Promise.resolve({ data: {} }))
}
}
};
// Prep spies for our component methods we want to validate
const spycreateBarcodes = jest.spyOn(index.methods, 'createBarcodes');
const createdHook = jest.spyOn(index, 'created');
// Mount the component we're testing
const wrapper = shallowMount(index, wrapperOptions);
test('if barcode logs were retrieved', () => {
expect(createdHook).toHaveBeenCalled();
expect(wrapper.vm.barcodeRanges).toHaveLength(11);
});
});
How do I mock or get the actual auth token in to work in my test?
const setItem = jest.spyOn(Storage.prototype, 'setItem')
const getItem = jest.spyOn(Storage.prototype, 'getItem')
expect(setItem).toHaveBeenCalled()
expect(getItem).toHaveBeenCalled()
You can try to mock localStorage before creating instance of a wrapper like this:
global.localStorage = {
state: {
'access-token': 'superHashedString'
},
setItem (key, item) {
this.state[key] = item
},
getItem (key) {
return this.state[key]
}
}
You can also spy on localStorage functions to check what arguments they were called with:
jest.spyOn(global.localStorage, 'setItem')
jest.spyOn(global.localStorage, 'getItem')
OR
You can delete localVue.use(axios) to let your $axios mock work correctly.
This
mocks: {
$axios: {
get: jest.fn(() => Promise.resolve({ data: {} }))
}
}
is not working because of that
localVue.use(axios)
I have been following these testing guidelines to test my vuex store.
But when I touched upon the actions part, I felt there is a lot going on that I couldn't understand.
The first part goes like:
// actions.js
import shop from '../api/shop'
export const getAllProducts = ({ commit }) => {
commit('REQUEST_PRODUCTS')
shop.getProducts(products => {
commit('RECEIVE_PRODUCTS', products)
})
}
// actions.spec.js
// use require syntax for inline loaders.
// with inject-loader, this returns a module factory
// that allows us to inject mocked dependencies.
import { expect } from 'chai'
const actionsInjector = require('inject!./actions')
// create the module with our mocks
const actions = actionsInjector({
'../api/shop': {
getProducts (cb) {
setTimeout(() => {
cb([ /* mocked response */ ])
}, 100)
}
}
})
I infer that this is to mock the service inside the action.
The part which follows is:
// helper for testing action with expected mutations
const testAction = (action, payload, state, expectedMutations, done) => {
let count = 0
// mock commit
const commit = (type, payload) => {
const mutation = expectedMutations[count]
expect(mutation.type).to.equal(type)
if (payload) {
expect(mutation.payload).to.deep.equal(payload)
}
count++
if (count >= expectedMutations.length) {
done()
}
}
// call the action with mocked store and arguments
action({ commit, state }, payload)
// check if no mutations should have been dispatched
if (expectedMutations.length === 0) {
expect(count).to.equal(0)
done()
}
}
describe('actions', () => {
it('getAllProducts', done => {
testAction(actions.getAllProducts, null, {}, [
{ type: 'REQUEST_PRODUCTS' },
{ type: 'RECEIVE_PRODUCTS', payload: { /* mocked response */ } }
], done)
})
})
This is where it I find it difficult to follow.
My store looks like:
import * as NameSpace from '../NameSpace'
import { ParseService } from '../../Services/parse'
const state = {
[NameSpace.AUTH_STATE]: {
auth: {},
error: null
}
}
const getters = {
[NameSpace.AUTH_GETTER]: state => {
return state[NameSpace.AUTH_STATE]
}
}
const mutations = {
[NameSpace.AUTH_MUTATION]: (state, payload) => {
state[NameSpace.AUTH_STATE] = payload
}
}
const actions = {
[NameSpace.ASYNC_AUTH_ACTION]: ({ commit }, payload) => {
ParseService.login(payload.username, payload.password)
.then((user) => {
commit(NameSpace.AUTH_MUTATION, {auth: user, error: null})
})
.catch((error) => {
commit(NameSpace.AUTH_MUTATION, {auth: [], error: error})
})
}
}
export default {
state,
getters,
mutations,
actions
}
And This is how I am trying to test:
import * as NameSpace from 'src/store/NameSpace'
import AuthStore from 'src/store/modules/authorization'
const actionsInjector = require('inject!../../../../../src/store/modules/authorization')
// This file is present at: test/unit/specs/store/modules/authorization.spec.js
// src and test are siblings
describe('AuthStore Actions', () => {
const injectedAction = actionsInjector({
'../../Services/parse': {
login (username, password) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (Math.random() > 0.5) {
resolve({})
} else {
reject({})
}
}, 300)
})
}
}
})
it('Gets the user profile if the username and password matches', () => {
const testAction = (action, payload, state, mutations, done) => {
const commit = (payload) => {
if (payload) {
expect(mutations.payload).to.deep.equal(payload)
}
}
action({ commit, state }, payload)
.then(result => {
expect(state).to.deep.equal({auth: result, error: null})
})
.catch(error => {
expect(state).to.deep.equal({auth: [], error: error})
})
}
testAction(injectedAction.login, null, {}, [])
})
})
If I try to do this, I get:
"Gets the user profile if the username and password matches"
undefined is not a constructor (evaluating 'action({ commit: commit, state: state }, payload)')
"testAction#webpack:///test/unit/specs/store/modules/authorization.spec.js:96:13 <- index.js:26198:14
webpack:///test/unit/specs/store/modules/authorization.spec.js:104:15 <- index.js:26204:16"
I need help understanding what am I supposed to do to test such actions.
I know it's been awhile but I came across this question because I was having a similar problem. If you were to console.log injectedActions right before you make the testAction call you'd see that the injectedAction object actually looks like:
Object{default: Object{FUNC_NAME: function FUNC_NAME(_ref) { ... }}}
So the main solution here would be changing the testAction call to:
testAction(injectedAction.default.login, null, {}, [], done)
because you are exporting your action as defaults in your store.
A few other issues that are unrelated to your particular error... You do not need to manipulate the testAction boilerplate code. It will work as expected so long as you pass in the proper parameters. Also, be sure to pass done to testAction or your test will timeout. Hope this helps somebody else who comes across this!