Skip to content

Commit d76db8d

Browse files
committed
test: add Ut
1 parent 38bc061 commit d76db8d

3 files changed

Lines changed: 65 additions & 1 deletion

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"lint:fix": "eslint --quiet --fix src test",
2727
"prepare": "husky install",
2828
"test": "npm run test:unit && npm run test:integ",
29-
"test:unit": "mocha test/unit/*.spec.js test/unit/**/*.spec.js --timeout=3000",
29+
"test:unit": "APP_CONFIG=./test/unit/utils/.app.json mocha test/unit/*.spec.js test/unit/**/*.spec.js test/unit/**/*.spec.js test/unit/**/**/*.spec.js --timeout=3000",
3030
"test:integ": "mocha test/integration/*.spec.js test/integration/**/*.spec.js --timeout=3000",
3131
"printReportsLink": "echo Detailed unit test coverage report: file:///$(pwd)/coverage-unit/index.html && echo Detailed integration test coverage report: file:///$(pwd)/coverage-integration/index.html",
3232
"cover": "npm run cover:unit",

test/unit/utils/.app.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"port": "5000",
3+
"authKey": "YWxhZGRpbjpvcGVuc2VzYW1l",
4+
"allowPublicAccess": false,
5+
"mysql": {
6+
"host": "localhost",
7+
"port": "3306",
8+
"database": "testdb",
9+
"user": "root",
10+
"password": "1234"
11+
}
12+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*global describe, it*/
2+
import * as assert from 'assert';
3+
import * as chai from 'chai';
4+
import {deleteAppConfig, getConfigs} from "../../../src/utils/configs.js";
5+
6+
let expect = chai.expect;
7+
8+
describe('unit Tests', function () {
9+
10+
it('verify config fail if APP_CONFIG not set properly', function () {
11+
const backEnv = process.env.APP_CONFIG;
12+
delete process.env.APP_CONFIG;
13+
deleteAppConfig();
14+
let exceptionOccurred = false;
15+
try {
16+
getConfigs();
17+
} catch (e) {
18+
expect(e.toString()).eql('Error: Please provide valid app config file by setting APP_CONFIG' +
19+
' environment variable for example APP_CONFIG=./abc.json');
20+
exceptionOccurred = true;
21+
}
22+
expect(exceptionOccurred).eql(true);
23+
process.env.APP_CONFIG = backEnv;
24+
25+
});
26+
it('getConfigShould pass', function () {
27+
let configs = getConfigs();
28+
_verifyConfigs(configs);
29+
configs = getConfigs();
30+
// call verify config second time
31+
_verifyConfigs(configs);
32+
33+
});
34+
it('default port should be 5000', function () {
35+
let configs = getConfigs();
36+
_verifyConfigs(configs);
37+
configs = getConfigs();
38+
// call verify config second time
39+
_verifyConfigs(configs);
40+
});
41+
});
42+
43+
function _verifyConfigs(configs) {
44+
expect(configs.port).to.eql('5000');
45+
expect(configs.authKey.length).to.eql(24);
46+
expect(configs.mysql.port).to.eql('3306');
47+
expect(configs.mysql.user.length).to.gt(0);
48+
expect(configs.mysql.password.length).to.gt(0);
49+
expect(configs.mysql.host.length).to.gt(0);
50+
expect(configs.mysql.database.length).to.gt(0);
51+
52+
}

0 commit comments

Comments
 (0)