mardi 18 août 2020

Mocha/Chai login method unit test nodejs

I've ran my tests for user CRUD and register user and everything went well. But for the login test i get an undefined response, even with the debugger I couldn't tell what am I missing. It looks like it doesn't save the newly created user. Also tried to create a user using the post method, and then check it for login method. In postman the routes work perfectly fine.

        it("should login an existing user and return a token", (done) => {
            let user = new User({
            username: "userTest112", 
            firstName: "User", 
            lastName: "Test",
            email: "userLogin@test.com", 
            password: "password", 
            userType: "participant",
            job: {
                employStatus: "employed",
                studentStatus: false
            }
            })
            user.save(() => {
                chai.request(server)
                .post("api/auth/login")
                .send ({
                    email: "userLogin@test.com",
                    password: "password"
                })
                .end((err, res) => {
                    console.log(err);
                    res.should.have.status(200);
                    res.body.should.be.a("object");
                    res.body.token.should.not.be.empty;
                    done();

                })
            })
        })
    }) ```
Error: connect ECONNREFUSED 127.0.0.1:80
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1129:14) {
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 80,
  response: undefined
}

Aucun commentaire:

Enregistrer un commentaire