Last active
December 3, 2019 03:53
-
-
Save sivasankars/9c4c4dc6784632a5460530ae62c3b2cb to your computer and use it in GitHub Desktop.
Creating and Running Test Cases Using Jest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const server = require('./server.js'); // Import Server/Application | |
// Start application before running the test case | |
beforeAll((done) => { | |
server.events.on('start', () => { | |
done(); | |
}); | |
}); | |
// Stop application after running the test case | |
afterAll((done) => { | |
server.events.on('stop', () => { | |
done(); | |
}); | |
server.stop(); | |
}); | |
test('should success with server connection', async function () { | |
const options = { | |
method: 'GET', | |
url: '/' | |
}; | |
const data = await server.inject(options); | |
expect(data.statusCode).toBe(200); | |
}); | |
test('should fail in adding user due to no payload', async function () { | |
const options = { | |
method: 'POST', | |
url: '/user', | |
}; | |
const data = await server.inject(options); | |
expect(data.statusCode).toBe(422); | |
expect(data.result).toBe('Invalid user'); | |
}); | |
test('should fail in adding user, where user already exists', async function () { | |
const options = { | |
method: 'POST', | |
url: '/user', | |
payload: JSON.stringify({ name: 'Siva' }) | |
}; | |
const data = await server.inject(options); | |
expect(data.statusCode).toBe(422); | |
expect(data.result).toBe('User already exists'); | |
}); | |
test('should add user successfully', async function () { | |
const options = { | |
method: 'POST', | |
url: '/user', | |
payload: JSON.stringify({ name: 'Test' }) | |
}; | |
const data = await server.inject(options); | |
expect(data.statusCode).toBe(200); | |
expect(data.result).toBe('User added successfully'); | |
}); | |
test('should get user successfully', async function () { | |
const data = await server.inject('/user/3'); | |
expect(data.statusCode).toBe(200); | |
expect(data.result.id).toBe(3); | |
expect(data.result.name).toBe('Niralar'); | |
}); | |
test('should fail in getting user', async function () { | |
const data = await server.inject('/user/5'); | |
expect(data.statusCode).toBe(422); | |
expect(data.result).toBe('User not exists'); | |
}); | |
test('should list user successfully', async function () { | |
const data = await server.inject('/user/list'); | |
expect(data.statusCode).toBe(200); | |
expect(data.result.length).toBe(3); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment