Search code examples
node.jsmocha.jschai

Use JWT token in multiple test cases mocha node-js


How I create a reusable function which gives me a JWT token, so I can execute my test cases in which token is required without calling the login function again and again in each test case file

account.js

describe("Account", () => {
var token;

describe("/POST Login", () => {
    it("it should gives the token", (done) => {
        chai.request(server)
            .post('api/v1/account')
            .set('Accept', 'application/json')
            .send({ "email": "[email protected]", "password": "123456" })
            .end((err, res) => {
                res.should.have.status(200);
                res.body.should.have.property("token");
                token = res.body.token //----------------TOKEN SET
                
                done();
            });
    });
});


describe("/GET account", () => {
    it("it should get the user account", (done) => {
        chai.request(server)
            .get('api/v1/account')
            .set('x-auth-token', token)
            .end((err, res) => {
                res.should.have.status(200);
                done();
            });
    });
});
});

category

describe("Category", () => {
var token;

//Login function duplicate in both the files

describe("/POST Login", () => {
    it("it should gives the token", (done) => {
        chai.request(server)
            .post('api/v1/account')
            .set('Accept', 'application/json')
            .send({ "email": "[email protected]", "password": "123456" })
            .end((err, res) => {
                res.should.have.status(200);
                res.body.should.have.property("token");
                token = res.body.token //----------------TOKEN SET
                
                done();
            });
    });
});


describe("/GET category", () => {
    it("it should get the user account", (done) => {
        chai.request(server)
            .get('api/v1/account')
            .set('x-auth-token', token)
            .end((err, res) => {
                res.should.have.status(200);
                done();
            });
    });
});
});

I want to get the token from other file and used in different cases. What is the best approach todo this?


Solution

  • I'm basing my response on the fact that you have mentioned unit tests. Usually, with a unit test, you're testing a small piece of functionality. This means, that you want to test a small piece of logic within a bigger component/piece of logic and you are not interested in testing other components (like for example in your case testing your API. What you usually want to test is how your logic should behave in the case you will receive a 200 success response from the API or what happens with your logic if you receive a 400 or 500. I would recommend to mock your API calls for your testing by using a library like nock: https://www.npmjs.com/package/nock

    The way that you are trying to implement it it might be a bit more complicated. If you want to do this kind of testing I wouldn't pick jest/mocha as test runners. I would prepare a postman collection (probably you already have that) and then I would utilise newman to run my collection and actually do the integration testing that you want. You can read further here: Running collections on the command line with Newman

    There are different approaches as well, but the one above can be a good one.