Skip to content

Commit ecbdca0

Browse files
committed
Add test cases for context inheritance
1 parent 3ba03b3 commit ecbdca0

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/test/acceptance-test/child-logger.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,60 @@ describe('Test child logger', function () {
116116
});
117117
});
118118

119+
describe('Test context features', function () {
120+
121+
describe('Create child logger without context', function () {
122+
123+
beforeEach(function () {
124+
childLogger = log.createLogger();
125+
});
126+
127+
it('cannot set context properties', function () {
128+
expect(childLogger.setContextProperty('some-field', 'some-value')).to.be.false;
129+
});
130+
131+
it('does not log a custom context property', function () {
132+
childLogger.setContextProperty('some-field', 'some-value');
133+
childLogger.warn('test-message');
134+
expect(lastOutput).to.not.include.key('some-field');
135+
});
136+
137+
it('does not log the correlation_id', function () {
138+
childLogger.warn("test-message");
139+
expect(lastOutput).to.not.include.key('correlation_id');
140+
});
141+
});
142+
143+
describe('Create child logger with new context', function () {
144+
145+
beforeEach(function () {
146+
childLogger = log.createLogger(null, true);
147+
});
148+
149+
it('sets context properties', function () {
150+
expect(childLogger.setContextProperty('some-field', 'some-value')).to.be.true;
151+
});
152+
153+
it('logs a custom context property', function () {
154+
childLogger.setContextProperty('some-field', 'some-value');
155+
childLogger.warn('test-message');
156+
expect(lastOutput).to.include.property('some-field', 'some-value');
157+
});
158+
159+
it('inherits the context', function () {
160+
childLogger.setContextProperty('some-field', 'some-value');
161+
childLogger.createLogger().warn('test-message');
162+
expect(lastOutput).to.include.property('some-field', 'some-value');
163+
});
164+
165+
it('generates a correlation_id', function () {
166+
childLogger.warn("test-message");
167+
expect(lastOutput).to.include.key('correlation_id');
168+
expect(lastOutput.correlation_id).to.match(/[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}/);
169+
});
170+
});
171+
});
172+
119173
describe('Set logging level threshold per child logger', function () {
120174

121175
describe('Child logger sets treshold', function () {

src/test/acceptance-test/global-context.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,26 @@ describe('Test logging in global context', function () {
183183

184184
});
185185

186+
describe('Test disabled context', function () {
187+
188+
it('cannot set context properties', function () {
189+
expect(log.setContextProperty("some-field", "some-value")).to.be.false;
190+
});
191+
192+
it('cannot set the correlation_id', function () {
193+
expect(log.setCorrelationId("f79ed23f-cff6-4599-8668-12838c898b70")).to.be.false;
194+
});
195+
196+
it('cannot set the tenant_id', function () {
197+
expect(log.setTenantId("some-value")).to.be.false;
198+
});
199+
200+
it('cannot set the correlation_id', function () {
201+
expect(log.setTenantSubdomain("some-value")).to.be.false;
202+
});
203+
204+
});
205+
186206
after(function () {
187207
log.setLoggingLevel("info");
188208
})

0 commit comments

Comments
 (0)