Olá, encontrei um empecilho ao tentar executar o comando npm run test:auth:service
no terminal. Realizei todos os passos comforme mostrado pelo instrutor, mas acabou ocorrendo esse problema. Segue os códigos dos arquivos envolvidos:
packge.json:
{
"name": "nodejs-testes",
"version": "1.0.0",
"description": "Api de livraria com nodejs",
"author": "Raphael Lucas Girao de Lima",
"main": "index.js",
"engines": {
"node": "16.14.2"
},
"scripts": {
"dev": "nodemon server.js",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test:watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watchAll",
"test:coverage": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watchAll --coverage",
"test:auth:service": "node --experimental-vm-modules node_modules/jest/bin/jest.js --testPathPattern=src/test/services/authService.test.js"
},
"keywords": [],
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"dotenv": "^16.3.1",
"express": "^4.19.2",
"jsonwebtoken": "^9.0.0",
"knex": "^2.5.1",
"multer": "^1.4.5-lts.1",
"nodemailer": "^6.9.2",
"sqlite": "4.1.1",
"sqlite3": "^5.1.7"
},
"type": "module",
"devDependencies": {
"@jest/globals": "28.1.2",
"eslint": "8.16.0",
"eslint-config-airbnb-base": "15.0.0",
"eslint-plugin-import": "2.26.0",
"jest": "28.1.2",
"nodemon": "^2.0.22",
"supertest": "6.2.4"
}
}
authService.test.js:
import { describe } from "@jest/globals";
import AuthService from '../../services/authService';
const authService = new AuthService();
describe('Teste do authService.cadastrarUsuario', () => {
it('O usuário deve possuir um nome, email e ssenha', async () => {
const userMock = {
nome: 'Raphael',
email: 'raphael@teste.com.br'
}
const usuarioSalvo = authService.cadastrarUsuario(userMock);
await expect(usuarioSalvo).rejects.toThrowError('A senha do usuário é obrigatória!')
});
});
authService.js
import bcryptjs from 'bcryptjs';
import jsonwebtoken from 'jsonwebtoken';
import Usuario from '../models/usuario.js';
import constants from '../config/constants.js';
class AuthService {
async login(data) {
//código oculto
}
async cadastrarUsuario(data) {
try {
if (!data.senha) {
throw new Error('A senha do usuário é obrigatória!')
}
data.senha = await bcryptjs.hash(data.senha, 8);
const usuario = new Usuario(data);
const resposta = await usuario.salvar(usuario);
return { message: 'usuario criado', content: resposta };
} catch (err) {
throw new Error(err.message);
}
}
}
export default AuthService;
Mensagem de erro do terminal:
> nodejs-testes@1.0.0 test:auth:service
> node --experimental-vm-modules node_modules/jest/bin/jest.js --testPathPattern=src/test/services/authService.test.js
(node:15320) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In C:\ (...path) \curso-nodejs-teste-main
40 files checked.
testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 3 matches
testPathIgnorePatterns: \\node_modules\\ - 40 matches
testRegex: - 0 matches
Pattern: src\\test\\services\\authService.test.js - 0 matches
Organização do projeto