Skip to content

Commit cee995d

Browse files
committed
test: update tests
1 parent 30b17ae commit cee995d

File tree

5 files changed

+315
-3
lines changed

5 files changed

+315
-3
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# API docs
22
The list of all APIs:
33

4-
1. [index](index-API)
4+
1. [index](index-API)
5+
1. [utils/logger](logger-API)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
2+
3+
## createLogger
4+
5+
Creates a logger instance based on the stage configuration
6+
7+
* Development: Pretty formatted, colorized logs
8+
* Production/Staging: ECS JSON format for Elasticsearch
9+
10+
Returns **pino.Logger** Configured Pino logger instance
11+
12+
## createFastifyLogger
13+
14+
Creates a Fastify logger configuration based on the stage
15+
16+
* Development: Pretty formatted, colorized logs
17+
* Production/Staging: ECS JSON format for Elasticsearch
18+
19+
Returns **[object][1]** Fastify logger configuration
20+
21+
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

test/unit/utils/.app.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"stage": "dev",
23
"port": 5000,
34
"authKey": "hehe",
45
"allowPublicAccess": false,

test/unit/utils/configs-test.spec.js

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/*global describe, it*/
1+
/*global describe, it, beforeEach, afterEach*/
22
import * as chai from 'chai';
3-
import {clearAppConfig, getConfigs} from "../../../src/utils/configs.js";
3+
import {clearAppConfig, getConfigs, getStage} from "../../../src/utils/configs.js";
44

55
let expect = chai.expect;
66

@@ -49,3 +49,120 @@ function _verifyConfigs(configs) {
4949
expect(configs.mysql.database.length).to.gt(0);
5050

5151
}
52+
53+
describe('unit Tests for getStage', function () {
54+
let originalNodeEnv;
55+
56+
beforeEach(function () {
57+
originalNodeEnv = process.env.NODE_ENV;
58+
clearAppConfig();
59+
});
60+
61+
afterEach(function () {
62+
process.env.NODE_ENV = originalNodeEnv;
63+
clearAppConfig();
64+
});
65+
66+
it('should return "dev" and set NODE_ENV to "development" for stage "dev"', function () {
67+
const stage = getStage();
68+
expect(stage).to.eql('dev');
69+
expect(process.env.NODE_ENV).to.eql('development');
70+
});
71+
72+
it('should handle "development" stage and set NODE_ENV correctly', async function () {
73+
// Temporarily modify config to test different stage
74+
const fs = await import('fs');
75+
const originalConfig = fs.readFileSync(process.env.APP_CONFIG, 'utf8');
76+
const config = JSON.parse(originalConfig);
77+
config.stage = 'development';
78+
fs.writeFileSync(process.env.APP_CONFIG, JSON.stringify(config, null, 2));
79+
clearAppConfig();
80+
81+
const stage = getStage();
82+
expect(stage).to.eql('development');
83+
expect(process.env.NODE_ENV).to.eql('development');
84+
85+
// Restore original
86+
fs.writeFileSync(process.env.APP_CONFIG, originalConfig);
87+
});
88+
89+
it('should handle "prod" stage and set NODE_ENV to "production"', async function () {
90+
const fs = await import('fs');
91+
const originalConfig = fs.readFileSync(process.env.APP_CONFIG, 'utf8');
92+
const config = JSON.parse(originalConfig);
93+
config.stage = 'prod';
94+
fs.writeFileSync(process.env.APP_CONFIG, JSON.stringify(config, null, 2));
95+
clearAppConfig();
96+
97+
const stage = getStage();
98+
expect(stage).to.eql('prod');
99+
expect(process.env.NODE_ENV).to.eql('production');
100+
101+
// Restore original
102+
fs.writeFileSync(process.env.APP_CONFIG, originalConfig);
103+
});
104+
105+
it('should handle "production" stage and set NODE_ENV correctly', async function () {
106+
const fs = await import('fs');
107+
const originalConfig = fs.readFileSync(process.env.APP_CONFIG, 'utf8');
108+
const config = JSON.parse(originalConfig);
109+
config.stage = 'production';
110+
fs.writeFileSync(process.env.APP_CONFIG, JSON.stringify(config, null, 2));
111+
clearAppConfig();
112+
113+
const stage = getStage();
114+
expect(stage).to.eql('production');
115+
expect(process.env.NODE_ENV).to.eql('production');
116+
117+
// Restore original
118+
fs.writeFileSync(process.env.APP_CONFIG, originalConfig);
119+
});
120+
121+
it('should default to "development" when stage is missing', async function () {
122+
const fs = await import('fs');
123+
const originalConfig = fs.readFileSync(process.env.APP_CONFIG, 'utf8');
124+
const config = JSON.parse(originalConfig);
125+
delete config.stage;
126+
fs.writeFileSync(process.env.APP_CONFIG, JSON.stringify(config, null, 2));
127+
clearAppConfig();
128+
129+
const stage = getStage();
130+
expect(stage).to.eql('development');
131+
expect(process.env.NODE_ENV).to.eql('development');
132+
133+
// Restore original
134+
fs.writeFileSync(process.env.APP_CONFIG, originalConfig);
135+
});
136+
137+
it('should be case insensitive - DEV should work', async function () {
138+
const fs = await import('fs');
139+
const originalConfig = fs.readFileSync(process.env.APP_CONFIG, 'utf8');
140+
const config = JSON.parse(originalConfig);
141+
config.stage = 'DEV';
142+
fs.writeFileSync(process.env.APP_CONFIG, JSON.stringify(config, null, 2));
143+
clearAppConfig();
144+
145+
const stage = getStage();
146+
expect(stage).to.eql('DEV');
147+
expect(process.env.NODE_ENV).to.eql('development');
148+
149+
// Restore original
150+
fs.writeFileSync(process.env.APP_CONFIG, originalConfig);
151+
});
152+
153+
it('should be case insensitive - PROD should work', async function () {
154+
const fs = await import('fs');
155+
const originalConfig = fs.readFileSync(process.env.APP_CONFIG, 'utf8');
156+
const config = JSON.parse(originalConfig);
157+
config.stage = 'PROD';
158+
fs.writeFileSync(process.env.APP_CONFIG, JSON.stringify(config, null, 2));
159+
clearAppConfig();
160+
161+
const stage = getStage();
162+
expect(stage).to.eql('PROD');
163+
expect(process.env.NODE_ENV).to.eql('production');
164+
165+
// Restore original
166+
fs.writeFileSync(process.env.APP_CONFIG, originalConfig);
167+
});
168+
});

test/unit/utils/logger.spec.js

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*global describe, it, beforeEach, afterEach*/
2+
import * as chai from 'chai';
3+
import {createLogger, createFastifyLogger} from "../../../src/utils/logger.js";
4+
import {clearAppConfig} from "../../../src/utils/configs.js";
5+
6+
let expect = chai.expect;
7+
8+
describe('unit Tests for logger', function () {
9+
let originalNodeEnv;
10+
11+
beforeEach(function () {
12+
originalNodeEnv = process.env.NODE_ENV;
13+
clearAppConfig();
14+
});
15+
16+
afterEach(function () {
17+
process.env.NODE_ENV = originalNodeEnv;
18+
clearAppConfig();
19+
});
20+
21+
describe('createLogger', function () {
22+
it('should create a logger in development mode', function () {
23+
// Config has stage: "dev" by default
24+
const logger = createLogger();
25+
26+
expect(logger).to.exist;
27+
expect(typeof logger.info).to.eql('function');
28+
expect(typeof logger.warn).to.eql('function');
29+
expect(typeof logger.error).to.eql('function');
30+
expect(typeof logger.debug).to.eql('function');
31+
});
32+
33+
it('should create a logger in production mode', async function () {
34+
const fs = await import('fs');
35+
const originalConfig = fs.readFileSync(process.env.APP_CONFIG, 'utf8');
36+
const config = JSON.parse(originalConfig);
37+
config.stage = 'prod';
38+
fs.writeFileSync(process.env.APP_CONFIG, JSON.stringify(config, null, 2));
39+
clearAppConfig();
40+
41+
const logger = createLogger();
42+
43+
expect(logger).to.exist;
44+
expect(typeof logger.info).to.eql('function');
45+
expect(typeof logger.warn).to.eql('function');
46+
expect(typeof logger.error).to.eql('function');
47+
48+
// Restore original
49+
fs.writeFileSync(process.env.APP_CONFIG, originalConfig);
50+
});
51+
52+
it('should be able to log messages in development', function () {
53+
const logger = createLogger();
54+
55+
// Should not throw
56+
expect(() => logger.info('Test message')).to.not.throw();
57+
expect(() => logger.warn('Warning message')).to.not.throw();
58+
expect(() => logger.error('Error message')).to.not.throw();
59+
});
60+
61+
it('should be able to log messages in production', async function () {
62+
const fs = await import('fs');
63+
const originalConfig = fs.readFileSync(process.env.APP_CONFIG, 'utf8');
64+
const config = JSON.parse(originalConfig);
65+
config.stage = 'production';
66+
fs.writeFileSync(process.env.APP_CONFIG, JSON.stringify(config, null, 2));
67+
clearAppConfig();
68+
69+
const logger = createLogger();
70+
71+
// Should not throw
72+
expect(() => logger.info('Test message')).to.not.throw();
73+
expect(() => logger.warn('Warning message')).to.not.throw();
74+
expect(() => logger.error('Error message')).to.not.throw();
75+
76+
// Restore original
77+
fs.writeFileSync(process.env.APP_CONFIG, originalConfig);
78+
});
79+
80+
it('should set NODE_ENV to development for dev stage', function () {
81+
createLogger();
82+
expect(process.env.NODE_ENV).to.eql('development');
83+
});
84+
85+
it('should set NODE_ENV to production for prod stage', async function () {
86+
const fs = await import('fs');
87+
const originalConfig = fs.readFileSync(process.env.APP_CONFIG, 'utf8');
88+
const config = JSON.parse(originalConfig);
89+
config.stage = 'prod';
90+
fs.writeFileSync(process.env.APP_CONFIG, JSON.stringify(config, null, 2));
91+
clearAppConfig();
92+
93+
createLogger();
94+
expect(process.env.NODE_ENV).to.eql('production');
95+
96+
// Restore original
97+
fs.writeFileSync(process.env.APP_CONFIG, originalConfig);
98+
});
99+
});
100+
101+
describe('createFastifyLogger', function () {
102+
it('should create Fastify logger config in development mode', function () {
103+
const loggerConfig = createFastifyLogger();
104+
105+
expect(loggerConfig).to.exist;
106+
expect(typeof loggerConfig).to.eql('object');
107+
108+
// In development mode, should have transport property
109+
if (loggerConfig.transport) {
110+
expect(loggerConfig.transport.target).to.exist;
111+
}
112+
});
113+
114+
it('should create Fastify logger config in production mode', async function () {
115+
const fs = await import('fs');
116+
const originalConfig = fs.readFileSync(process.env.APP_CONFIG, 'utf8');
117+
const config = JSON.parse(originalConfig);
118+
config.stage = 'prod';
119+
fs.writeFileSync(process.env.APP_CONFIG, JSON.stringify(config, null, 2));
120+
clearAppConfig();
121+
122+
const loggerConfig = createFastifyLogger();
123+
124+
expect(loggerConfig).to.exist;
125+
expect(typeof loggerConfig).to.eql('object');
126+
127+
// Restore original
128+
fs.writeFileSync(process.env.APP_CONFIG, originalConfig);
129+
});
130+
131+
it('should return different configs for dev vs prod', async function () {
132+
// Get dev config
133+
const devConfig = createFastifyLogger();
134+
135+
// Change to prod
136+
const fs = await import('fs');
137+
const originalConfig = fs.readFileSync(process.env.APP_CONFIG, 'utf8');
138+
const config = JSON.parse(originalConfig);
139+
config.stage = 'prod';
140+
fs.writeFileSync(process.env.APP_CONFIG, JSON.stringify(config, null, 2));
141+
clearAppConfig();
142+
143+
const prodConfig = createFastifyLogger();
144+
145+
// Configs should be different objects
146+
expect(devConfig).to.not.deep.equal(prodConfig);
147+
148+
// Restore original
149+
fs.writeFileSync(process.env.APP_CONFIG, originalConfig);
150+
});
151+
152+
it('should set NODE_ENV to development for dev stage', function () {
153+
createFastifyLogger();
154+
expect(process.env.NODE_ENV).to.eql('development');
155+
});
156+
157+
it('should set NODE_ENV to production for prod stage', async function () {
158+
const fs = await import('fs');
159+
const originalConfig = fs.readFileSync(process.env.APP_CONFIG, 'utf8');
160+
const config = JSON.parse(originalConfig);
161+
config.stage = 'production';
162+
fs.writeFileSync(process.env.APP_CONFIG, JSON.stringify(config, null, 2));
163+
clearAppConfig();
164+
165+
createFastifyLogger();
166+
expect(process.env.NODE_ENV).to.eql('production');
167+
168+
// Restore original
169+
fs.writeFileSync(process.env.APP_CONFIG, originalConfig);
170+
});
171+
});
172+
});

0 commit comments

Comments
 (0)