I was trying to build a test on a method of my controller, and when I tried to throw a customer exception that I created to use when the app don't find the user, I got this error: AssertionError [ERR_ASSERTION]: Local storage does not seem to have been initialized or in context, please make sure you use local storage middleware.
Edit: I tried to mock local storage, but i got the same error.
The test:
import { cpfValidator } from '../src/v1/helpers/validators';
import { ResourceNotFoundException } from "../src/v1/exception/error-exception";
describe('Testar as rotas', () => {
it('Informar todos os produtios relacionados a um usuário', async () => {
const resposta = cpfValidator('CPF38751468831', '')
expect(resposta).toThrow(TypeError);
}, 120000);
});
The cpf validator
export async function cpfValidator(userId: string, xTokenInfo?: string): Promise<string> {
if (userId !== 'me' && new RegExp(/^CPF[0-9]{11}$/).test(userId) && cpf.isValid(userId))
return userId.substr(3);
else if (userId === 'me' && new RegExp(/^sub:CPF[0-9]{11}$/).test(xTokenInfo) && cpf.isValid(xTokenInfo.substr(4)))
return xTokenInfo.substr(7);
else {
const logger = new Logger();
logger.error('Erro ao validar documento. Número de CPF Inválido.', { "user-id": userId });
throw new ResourceNotFoundException();
}
}
The class of error:
export class ResourceNotFoundException extends GenericException {
constructor() {
// consider ERRORS as a constant
super(ERRORS[404].CODE, ERRORS[404].MESSAGE, ERRORS[404].HTTP_CODE);
}
}
The class extended
import { HttpException } from '@nestjs/common';
export declare class GenericException extends HttpException {
constructor(code?: string, message?: string, statusCode?: number);
}
PS: This was my first question and I'm new with Jest, so sorry for my mistakes.