visit
Correction: testing on NodeJs is way faster than testing with PHPUnit, because Jest runs your tests in parallel, and in the world of CI/CD, this means something very important. Fast deployment time! 🙌🏽
// Get User Test
test('get user', async () => {
const response = await request
.get('/v1/user/1')
.set('Authorization', `Bearer sample-token`)
.send();
expect(response.status).toBe(200);
});
// Delete User Test
test('delete user', async () => {
const response = await request
.delete('/v1/user/1')
.set('Authorization', `Bearer sample-token`)
.send();
expect(response.status).toBe(200);
});
// Get User Test
test('get user', async () => {
// Create a new user
const user = User.create({name: "Sample user 1"});
// Get the user
const response = await request
.get(`/v1/user/${user.id}`)
.set('Authorization', `Bearer sample-token`)
.send();
expect(response.status).toBe(200);
});
// Delete User Test
test('delete user', async () => {
// Create a new user
const user = User.create({name: "Sample user 2"});
// Delete the user
const response = await request
.delete(`/v1/user/${user.id}`)
.set('Authorization', `Bearer sample-token`)
.send();
expect(response.status).toBe(200);
});
Promises
const user = User.findByPk(1); // no await
expect(user).not.toBeNull();
This will always be true as it will be testing on the returned Promise
object which will not be null.
const user = await User.findByPk(1); // await
expect(user).not.toBeNull();
console.log
Debuggers allow you to literally go into the function and see what happens step by step and view the real content of each variable at any point, while console.log
only shows you the string representation of the variable you log which could be hiding that extra piece of information you need to figure the bug out.
Additionally, console.log
codes can easily find their way to production and you find yourself unknowingly logging sensitive information which could be dangerous.
const getSignedUrl = (key, bucket = null) => {
if (process.env.NODE_ENV === 'test') {
return `//s3.eu-west-2.amazonaws.com/sample/${key}`;
}
return s3.getSignedUrl('getObject', {
Bucket: bucket,
Key: key,
Expires: 60,
});
};
Also published on //log.victoranuebunwa.com/how-i-survive-testing-on-nodejs-and-jest.