Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(mission-integration-bay): add tests - Danny Jung #145

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"git.enabled": false
"git.enabled": true
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"start": "./src/index.js",
"test": "jest --reporters=default --reporters=jest-junit",
"test:dev": "jest --watch --silent --runInBand",
"test:dev": "jest --watch --silent --maxWorkers=0",
"test:dev:migrate": "DO_MIGRATION='true' jest --watch --silent --runInBand",
"test:dev:verbose": "jest --watch --runInBand --verbose",
"lint": "eslint .",
Expand Down
41 changes: 7 additions & 34 deletions test/mission-basic-response-bay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const {
} = require('../src/entry-points/sensors-api');
const { getShortUnique, getSensorEvent } = require('./test-helper');
const sinon = require('sinon');
const SensorsRepository = require('../src/data-access/sensors-repository');
const { AppError } = require('../src/error-handling');

let expressApp;

Expand All @@ -34,7 +36,6 @@ afterEach(() => {
});

describe('Sensors test', () => {
// ✅ TASK: Run the testing and ensure the the next simplistic test pass
test('Just checking that testing works on your machine', () => {
expect('Me boosting my testing knowledge in the workshop').toBeTruthy();
// 💡 TIP: The the tests in watch mode: npm run test:dev
Expand All @@ -54,47 +55,19 @@ describe('Sensors test', () => {
category: 'Kids-Room',
// 💡 TIP: Consider explicitly specify that category is undefined by assigning 'undefined'
};
Math.random() < 0.5 ? eventToAdd.temperature = undefined : eventToAdd.category = undefined;

// Act

// 💡 TIP: use any http client lib like Axios OR supertest
// 💡 TIP: This is how it is done with Supertest -> await request(expressApp).post("/sensor-events").send(eventToAdd);
// 💡 TIP: This is how it is done with Supertest ->
const receivedResponse = await request(expressApp).post("/sensor-events").send(eventToAdd);

// Assert

// 💡 TIP: Check that the received response is indeed as stated in the test name
// 💡 TIP: Use this syntax for example: expect(receivedResponse.status).toBe(...);
});

// ✅ TASK: Test that when a new valid event is posted to /sensor-events route, we get back a valid response
// 💡 TIP: Consider checking both the HTTP status and the body
test('When inserting a valid event, should get successful response', async () => {
// Arrange
// Act
// 💡 TIP: use any http client lib like Axios OR supertest
// 💡 TIP: This is how it is done with Supertest -> await request(expressApp).post("/sensor-events").send(eventToAdd);
// Assert
// 💡 TIP: You may check the body and the status all together with the following syntax:
// expect(receivedResponse).toMatchObject({status: 200, body: {...}});
});

// ✅ TASK: Test that when a new valid event is posted to /sensor-events route, it's indeed retrievable from the DB
// 💡 TIP: In the assert phase, query to get the event that was added
// 💡 TIP: Whenever possible, use the public API for verification (not direct DB access)

// ✅ Keep the tests very short and readable, strive not to pass 7 statements per test
// 💡 TIP: If it gets too long, extract obvious parts into an external helper

// ✅🚀 TASK: Code the following test below
test('When an internal unknown error occurs during request, Then get back 500 error', async () => {
// Arrange
// 💡 TIP: Factor a valid event here, otherwise the request will get rejected on start and the failure won't happen
// 💡 TIP: Make some internal function fail, choose any class method
// 💡 TIP: Use the library sinon to alter the behaviour of existing function and make it throw error
// https://sinonjs.org/releases/latest/stubs/
// 💡 TIP: Here is the syntax: sinon.stub(someClass.prototype, 'methodName').rejects(new Error("Error explanation"));
// Act
// Assert
// 💡 TIP: Use this syntax for example:
expect(receivedResponse.status).toBe(400);
});

// ✅ Ensure that the webserver is closed when all the tests are completed
Expand Down
91 changes: 91 additions & 0 deletions test/mission-basic-response-bay2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 🏅 Your mission is to create your first integration tests here 💜
// 🏝 Response island
// ✅ Whenever you see this icon, there's a TASK for you
// ✅🚀 This symbol represents an advanced task
// 💡 - This is an ADVICE symbol, it will appear nearby most tasks and help you in fulfilling the tasks

const request = require('supertest');
const nock = require('nock');
const {
startWebServer,
stopWebServer,
} = require('../src/entry-points/sensors-api');
const { getShortUnique, getSensorEvent } = require('./test-helper');
const sinon = require('sinon');
const SensorsRepository = require('../src/data-access/sensors-repository');
const { AppError } = require('../src/error-handling');

let expressApp;

beforeAll(async () => {
expressApp = await startWebServer();
});

afterAll(async () => {
await stopWebServer();
});

beforeEach(() => {
nock('http://localhost').get('/notification').reply(200, {
success: true,
});
});

afterEach(() => {
sinon.restore();
});

describe('Sensors test', () => {
// ✅ TASK: Test that when a new valid event is posted to /sensor-events route, we get back a valid response
// 💡 TIP: Consider checking both the HTTP status and the body
test('When inserting a valid event, should get successful response', async () => {
// Arrange
const eventToAdd = {
temperature: 20,
color: 'Green',
weight: 80,
status: 'active',
category: 'Kids-Room',
};

// Act
// 💡 TIP: use any http client lib like Axios OR supertest
// 💡 TIP: This is how it is done with Supertest ->
const receivedResponse = await request(expressApp).post("/sensor-events").send(eventToAdd);

// Assert
// 💡 TIP: You may check the body and the status all together with the following syntax:
expect(receivedResponse).toMatchObject({ status: 200, body: eventToAdd });
});

// ✅ TASK: Test that when a new valid event is posted to /sensor-events route, it's indeed retrievable from the DB
// 💡 TIP: In the assert phase, query to get the event that was added
// 💡 TIP: Whenever possible, use the public API for verification (not direct DB access)
test('When inserting a valid event, should be retrievable', async () => {
// Arrange
const eventToAdd = {
temperature: 20,
color: 'Green',
weight: 80,
status: 'active',
category: 'Kids-Room',
};

// Act
const successResponse = await request(expressApp).post("/sensor-events").send(eventToAdd);
const receivedResponse = await request(expressApp).get(`/sensor-events/${successResponse.body.id}`).send();

// Assert
expect(receivedResponse).toMatchObject({ status: 200, body: eventToAdd });
});

// ✅ Keep the tests very short and readable, strive not to pass 7 statements per test
// 💡 TIP: If it gets too long, extract obvious parts into an external helper

// ✅ Ensure that the webserver is closed when all the tests are completed
// 💡 TIP: Use the right test hook to call the API and instruct it to close

// ✅🚀 Spread your tests across multiple files, let the test runner invoke tests in multiple processes - Ensure all pass
// 💡 TIP: You might face port collision where two APIs instances try to open the same port
// 💡 TIP: Use the flag 'jest --maxWorkers=<num>'. Assign zero for max value of some specific number greater than 1
});
152 changes: 152 additions & 0 deletions test/mission-basic-response-bay3.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// 🏅 Your mission is to create your first integration tests here 💜
// 🏝 Response island
// ✅ Whenever you see this icon, there's a TASK for you
// ✅🚀 This symbol represents an advanced task
// 💡 - This is an ADVICE symbol, it will appear nearby most tasks and help you in fulfilling the tasks

const request = require('supertest');
const nock = require('nock');
const {
startWebServer,
stopWebServer,
} = require('../src/entry-points/sensors-api');
const { getShortUnique, getSensorEvent } = require('./test-helper');
const sinon = require('sinon');
const SensorsRepository = require('../src/data-access/sensors-repository');
const { AppError } = require('../src/error-handling');

let expressApp;

beforeAll(async () => {
expressApp = await startWebServer();
});

afterAll(async () => {
await stopWebServer();
});

beforeEach(() => {
nock('http://localhost').get('/notification').reply(200, {
success: true,
});
});

afterEach(() => {
sinon.restore();
});

describe('Sensors test', () => {
test('Just checking that testing works on your machine', () => {
expect('Me boosting my testing knowledge in the workshop').toBeTruthy();
// 💡 TIP: The the tests in watch mode: npm run test:dev
// 💡 TIP: When in watch mode, within the terminal/CMD type "p" -> Then start typing this file name, choose it
// It should run only this file. Click "w" to return to the main menu
});

// ✅ TASK: Test that when a new event is posted to /event route, if category or temperature are not specified -> the API returns HTTP 400
// 💡 TIP: Down below, there is an example event schema
test('When category is not specified, should get http 400 error', async () => {
// Arrange
const eventToAdd = {
temperature: 20,
color: 'Green',
weight: 80,
status: 'active',
category: 'Kids-Room',
// 💡 TIP: Consider explicitly specify that category is undefined by assigning 'undefined'
};
Math.random() < 0.5 ? eventToAdd.temperature = undefined : eventToAdd.category = undefined;

// Act

// 💡 TIP: use any http client lib like Axios OR supertest
// 💡 TIP: This is how it is done with Supertest ->
const receivedResponse = await request(expressApp).post("/sensor-events").send(eventToAdd);

// Assert

// 💡 TIP: Check that the received response is indeed as stated in the test name
// 💡 TIP: Use this syntax for example:
expect(receivedResponse.status).toBe(400);
});

// ✅ TASK: Test that when a new valid event is posted to /sensor-events route, we get back a valid response
// 💡 TIP: Consider checking both the HTTP status and the body
test('When inserting a valid event, should get successful response', async () => {
// Arrange
const eventToAdd = {
temperature: 20,
color: 'Green',
weight: 80,
status: 'active',
category: 'Kids-Room',
};

// Act
// 💡 TIP: use any http client lib like Axios OR supertest
// 💡 TIP: This is how it is done with Supertest ->
const receivedResponse = await request(expressApp).post("/sensor-events").send(eventToAdd);

// Assert
// 💡 TIP: You may check the body and the status all together with the following syntax:
expect(receivedResponse).toMatchObject({ status: 200, body: eventToAdd });
});

// ✅ TASK: Test that when a new valid event is posted to /sensor-events route, it's indeed retrievable from the DB
// 💡 TIP: In the assert phase, query to get the event that was added
// 💡 TIP: Whenever possible, use the public API for verification (not direct DB access)
test('When inserting a valid event, should be retrievable', async () => {
// Arrange
const eventToAdd = {
temperature: 20,
color: 'Green',
weight: 80,
status: 'active',
category: 'Kids-Room',
};

// Act
const successResponse = await request(expressApp).post("/sensor-events").send(eventToAdd);
const receivedResponse = await request(expressApp).get(`/sensor-events/${successResponse.body.id}`).send();

// Assert
expect(receivedResponse).toMatchObject({ status: 200, body: eventToAdd });
});

// ✅ Keep the tests very short and readable, strive not to pass 7 statements per test
// 💡 TIP: If it gets too long, extract obvious parts into an external helper

// ✅🚀 TASK: Code the following test below
test('When an internal unknown error occurs during request, Then get back 500 error', async () => {
// Arrange
// 💡 TIP: Factor a valid event here, otherwise the request will get rejected on start and the failure won't happen
// 💡 TIP: Make some internal function fail, choose any class method
// 💡 TIP: Use the library sinon to alter the behaviour of existing function and make it throw error
// https://sinonjs.org/releases/latest/stubs/
// 💡 TIP: Here is the syntax: sinon.stub(someClass.prototype, 'methodName').rejects(new Error("Error explanation"));
const eventToAdd = {
temperature: 20,
color: 'Green',
weight: 80,
status: 'active',
category: 'Kids-Room',
};

sinon
.stub(SensorsRepository.prototype, 'addSensorsEvent')
.rejects(new AppError('db-is-unaccessible', true, 500));

// Act
const receivedResponse = await request(expressApp).post("/sensor-events").send(eventToAdd);

// Assert
expect(receivedResponse).toMatchObject({ status: 500 });
});

// ✅ Ensure that the webserver is closed when all the tests are completed
// 💡 TIP: Use the right test hook to call the API and instruct it to close

// ✅🚀 Spread your tests across multiple files, let the test runner invoke tests in multiple processes - Ensure all pass
// 💡 TIP: You might face port collision where two APIs instances try to open the same port
// 💡 TIP: Use the flag 'jest --maxWorkers=<num>'. Assign zero for max value of some specific number greater than 1
});
Loading
Loading