I try to use chai to make a request to my API on the /login route, but it cannot complete, I can use the same structure to do it for another route but it is not working in the best way.
const sinon = require('sinon');
const chai = require('chai');
const chaiHttp = require('chai-http');
const sinonChai = require('sinon-chai');
const app = require('../../src/app');
const { User } = require('../../src/models');
const { userMock } = require('../mocks');
const { expect } = chai;
chai.use(sinonChai);
chai.use(chaiHttp);
describe('login test', function () {
describe('POST /login', function () {
beforeEach(function () { sinon.restore(); });
it('should return 200 and a token when the user is found', async function () {
// arrange
sinon.stub(User, 'findOne').resolves(userMock.users[0]);
const bodyReq = {
email: userMock.userId1.email,
password: userMock.userId1.password,
};
// act
const { status, body } = await chai.request(app)
.post('/login')
.send(bodyReq)
// assert
expect(status).to.equal(200);
expect(body).to.have.property('token');
});
});
});
❯ npm run test:mocha
> [email protected] test:mocha
> mocha
(node:90966) [SEQUELIZE0002] DeprecationWarning: The logging-option should be either a function or false. Default: console.log
(Use `node --trace-deprecation ...` to show where the warning was created)
login test
POST /login
1) should return 200 and a token when the user is found
0 passing (2s)
1 failing
1) login test
POST /login
should return 200 and a token when the user is found:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/julio/Trybe/modulo-back/2-periodo/sd-035-project-blogs-api/tests/integrations/Login.test.js)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7)
the warning is because of the configuration of the .mocharc.josn file:
{
"spec": ["tests/**/*.test.js", "tests/*.test.js"],
"exit": true
}
I already removed it "tests/*.test.js"
.
The project folders look like this:
/src/
--congig
--controllers
--middlewares
--migrations
--models
--routes
--seeds
--services
--utils
--app.js
--server.js
/tests/
--integrations/
----Login.test.js
--mocks/
If you want to investigate by looking at the code in more depth, here is the pullrequest:
https://github.com/juliomatiastrybe/blogs-api-sequelize/pull/4/commits/7501936be512f06600d970fc8ced895f69a0acf7
I used debug to try to see the error but I couldn’t, and even when making a request for another route, the middleware doesn’t work as it should, for example on routes that have token validation, I can’t validate it, even though the API is functional and can do the same process via thunderClient.
3
Answers
I did another test for another route but it doesn't work either, I passed a valid token, tested via ThunderClient, but it's as if the middleware didn't work correctly to validate the data.
folder and file structure:
terminal return:
It could be that there is a file with tests that Mocha is trying to use which it cannot find that lives in the folder called tests and is called SOMETHING dot (.) test dot js hence why it says there is no file with matching the pattern:
So can you show us:
It could be that a (THE) file has been renamed to a DIFFERENT pattern or simply does not exist anymore.
I managed to resolve it.
There were several issues to be resolved.
The first thing is that when creating the JWT token, there was no default for secret when not provided an environment variable, so I needed to add it.
With the auth.js file looking like this:
In the loginSevice file, I would get both the properties returned from User sequelize and also the dataValues.
However, in my mock I was not passing this behavior, returning only the user properties, without the dataValues, so in the mock I left it like this:
And the request was not complete precisely because of these errors that were not captured, this reveals the impotence of always worrying about capturing these error.