All the answers should be written in proper English.
1) Why should we write tests?
2) How much code coverage is required? Comment.
3) What kind of test is this? Explain your choice of answer.
-
End to end (E2E)
-
Integration
-
Unit
var request = require('supertest'); var app = require('../server'); describe('API Tests', function() { var task = { name: 'test', }; describe('Get all tasks', function() { it('should get all tasks', function(done) { request(app) .get('/tasks') .end(function(err, res) { expect(res.statusCode).toBe(200); expect(Array.isArray(res.body)).toBe(true); done(); }); }); }); describe('Create task ', function() { it('should create a task', function(done) { request(app) .post('/tasks') .send(task) .end(function(err, res) { expect(res.statusCode).toBe(200); expect(res.body.name).toBe('test'); task = res.body; done(); }); }); }); });
4) Below are 2 snippets of code which achieve the same functionality. Which snippet of code is better testable and WHY?
// 1
function valIncrementer(val, disabled) {
var nextVal = val + 1;
function clipToUpperLimit() {
if (val >= 10) {
nextVal = 10;
secondStageSetter()
}
}
function clipToLowerLimit() {
if (val <= 0) {
nextVal = 0;
secondStageSetter()
}
}
function secondStageSetter() {
if (disabled) {
nextVal = val
}
}
function run() {
clipToUpperLimit()
clipToLowerLimit()
}
run()
return nextVal
}
// 2
const incrementedVal = (val) => val + 1;
function substituteVal(val, low, high) {
if (val >= high) return high
else if (val <= low) return low
else return val
}
function shouldIncrement(val, disabled) {
return val >= 0 && val < 10 && !disabled
}
function valIncrementer(val, disabled) {
if (shouldIncrement(val, disabled)) {
return incrementedVal(val)
}
return substituteVal(val, 0, 10)
}
5) Describe the difference between the two tests below. Which one is ideal and WHY?
// 1
it('should send the profile data to the server and update the profile view properly', function() {
expect(...)to(...);
expect(...)to(...);
});
// 2
it('should send the profile data to the server', function() {
expect(...)to(...);
});
it('should update the profile view properly', function() {
expect(...)to(...);
});