0

I have a route that records user data and gives a unique identifier.

router.post('/', (req, res) => {
    const user_email = req.body.user_email;
    const username = req.body.username;
    const user_phone = req.body.user_phone;
    const service_sid = serviceSidVerification(user_phone);
    const entry_id = uuidv4();
    const user = new User(
        username,
        user_email,
        user_password,
        user_phone,
        entry_id,
        service_sid,
    );
    await db.query(user.setUser());
    return res.status(200).json({
        entry_id: entry_id,
    });
});

I want to write a test for this route. I need to replace the return value from the serviceSidVerification() function with my own. For example, 12345. How can this be done with a test?

This is what my test looks like.

describe(`Users Test`, () => {
    describe('POST /api/users/', () => {
        it(`Should create user`, (done) => {
            let stub = sinon.createStubInstance(server);
            stub.getEvents.returns('12345')
            chai
                .request(server)
                .post('/api/users/')
                .send({ 'username': faker.internet.userName() })
                .send({ 'user_email': faker.internet.email() })
                .send({ 'user_password': faker.internet.password() })
                .send({ 'user_phone': faker.phone.phoneNumber() })
                .end((err, res) => {
                    expect(res).have.status(200);
                    expect(res.body).have.property('entry_id');
                    expect(res.body.entry_id).to.be.an('string');
                    done();
                });
            });
        });
    });
});

...

function ServiceSidVerification(phone) {
    const client = new twilio(config.smsVerify.accountSid, config.smsVerify.authToken);
    const service_sid = (await client.verify.services
        .create({
            friendlyName: config.smsVerify.title,
            codeLength: config.smsVerify.codeLength,
        })
    ).sid;
    const sid = (await client.verify.services(service_sid).verifications
        .create({
            to: phone,
            channel: 'sms'
        })
    ).sid;
    return { service_sid, sid };
}

How do I test my route?

1 Answer 1

1

To change the behavior of serviceSidVerification() you can use sinon. If you decide to go this way, you'll have to require sinon in the tests file + the module, where serviceSidVerification resides. Then, before executing the test you'll have to do something like this:

const stubServiceSidVerification = sinon.stub(yourModule, 'serviceSidVerification')
    .returns('yourValue');

Please note that if you use sinon, serviceSidVerification should be exported as the property of an object and not as a standalone function because of this issue.

Another option is to use proxyquire.

Sign up to request clarification or add additional context in comments.

6 Comments

I get an error Cannot stub non-existent own property serviceSidVerification
Can you share the code where serviceSidVerification is defined and exported and the code where you try to stub it?
Added. The function is in the same file.
What sinon can do is stub(replace) the specified function when a test calls the API that uses that function. In your case it could change the behavior of serviceSidVerification when the test hits the endpoint. In order to do that, that function must be required and stubbed where the test is executed. You've said that serviceSidVerification is defined in the 'same file' - what file do you mean? Also, I can't see where it gets stubbed. I see sinon.createStubInstance(server) but I'm not sure that this is what is needed in this case.
I cannot figure out how to test this route. Is there an example or explanation?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.