Commit ceaa56f9 authored by Juha's avatar Juha

Unit and API tests added

parent 28ec4f69
Pipeline #8425818 passed with stage
......@@ -10,7 +10,7 @@
"exports": "always-multiline",
"functions": "never"
}],
"import/no-extraneous-dependencies": ["error", {"devDependencies": ["**/*.test.js"]}]
"import/no-extraneous-dependencies": ["error", {"devDependencies": ["**/*.test.js","**/*.apitest.js"]}]
},
"env": {
"jest": true
......
{
"collectCoverage": true,
"collectCoverageFrom": [
"app/**/*.js",
"index.js"
],
"coverageDirectory": "shippable/codecoverage",
"coverageReporters": [
"text",
"cobertura"
],
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
},
"rootDir": "../../",
"testEnvironment": "node",
"testMatch": [
"**/?(*.)(apitest|test).js?(x)"
],
"testResultsProcessor": "./node_modules/jest-junit"
}
{
"collectCoverage": false,
"rootDir": "../../",
"testEnvironment": "node",
"testMatch": [
"**/?(*.)(test).js?(x)"
]
}
module.exports = function (grunt) {
grunt.initConfig({
env: {
ci: {
NODE_ENV: 'continuous_integration',
JEST_JUNIT_OUTPUT: './shippable/testresults/results.xml',
},
},
run: {
eslint: {
exec: 'eslint .',
},
test: {
exec: 'jest --config config/jest/jest.json',
},
test_unit_only: {
exec: 'jest --config config/jest/jest_unit_tests.json',
},
},
});
grunt.loadNpmTasks('grunt-env');
grunt.loadNpmTasks('grunt-run');
grunt.registerTask('default', [
'env:ci',
'run:test',
'run:eslint',
]);
grunt.registerTask('unit_tests_only', [
'env:ci',
'run:test_unit_only',
]);
};
/* eslint-disable global-require */
global.console = { log: jest.fn() };
const request = require('supertest');
describe('API test example', () => {
let server;
beforeAll(() => {
server = require('./index');
});
afterAll(() => {
server.close();
});
test('Should return a client error for #get /', done => (
request(server)
.get('/')
.expect(404, {
code: 'ResourceNotFound',
message: '/ does not exist',
})
.end((err) => {
if (err) return done.fail(err);
return done();
})
));
});
......@@ -7,3 +7,5 @@ const server = restify.createServer({ name: 'Auth Service' });
server.listen(SERVER_PORT, () => {
console.log(`Server running on: ${JSON.stringify(server.address(), undefined, 2)}`);
});
module.exports = server;
/* eslint-disable global-require */
describe('Unit test example', () => {
const mockCreate = jest.fn();
const mockListen = jest.fn();
beforeEach(() => {
jest.mock('restify', () => {
const restify = jest.genMockFromModule('restify');
const dummyServer = { listen: mockListen };
mockCreate.mockReturnValueOnce(dummyServer);
restify.createServer = mockCreate;
return restify;
});
});
test('Should start Restify with right function calls', () => {
require('./index.js');
expect(mockCreate).toHaveBeenCalledTimes(1);
expect(mockListen).toHaveBeenCalledTimes(1);
});
});
......@@ -6,7 +6,8 @@
"scripts": {
"start": "node index.js",
"start:dev": "node-dev index.js",
"test": "echo \"Success: tests are passing\" && exit 0"
"test": "grunt",
"test:unit": "grunt unit_tests_only"
},
"repository": {
"type": "git",
......@@ -27,6 +28,12 @@
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.10.3",
"node-dev": "^3.1.3"
"grunt": "^1.0.1",
"grunt-env": "^0.4.4",
"grunt-run": "^0.6.0",
"jest-cli": "^20.0.3",
"jest-junit": "^1.5.1",
"node-dev": "^3.1.3",
"supertest": "^3.0.0"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment