I would like to test if a logger increments correctly (using Jest). Looking at the code below this means I would like check if the increment (1) (meaning the content of the void myMethod) is getting called.
// myScript.ts
import dd from 'datadog-metrics';
export class MyClass {
private bufferedMetricsLogger: dd.BufferedMetricsLogger;
constructor(bufferedMetricsLogger: dd.BufferedMetricsLogger) {
this.bufferedMetricsLogger = bufferedMetricsLogger;
}
public myMethod(myInput: String): void {
this.bufferedMetricsLogger.increment('my.key.name', 1, [`myTag:${myInput}`]); // (1)
}
}
// myScript.test.ts
import { mock, instance, verify } from 'ts-mockito';
import { BufferedMetricsLogger } from 'datadog-metrics';
import { myClass } from 'path/to/myScript';
describe('Testing MyClass', () => {
it('verify that myMethod increments', () => {
const mockedBufferedMetricsLogger: BufferedMetricsLogger = mock(BufferedMetricsLogger);
const bufferedMetricsLogger: BufferedMetricsLogger = instance(mockedBufferedMetricsLogger);
const mockMyClass: MyClass = mock(MyClass);
const myClassOption1: MyClass = instance(mockMyClass);
const myClassOption2: MyClass = new MyClass(mockedBufferedMetricsLogger);
myClassOption1.myMethod('XXX'); // Option 1
myClassOption2.myMethod('XXX'); // Option 2
bufferedMetricsLogger.increment('my.key.name', 1, [`myTag:${XXX}`]); // Option 3
verify(mockedBufferedMetricsLogger.increment('my.key.name', 1, [`myTag:${XXX}`])).once();
})
});
I tried three different Options. Every Option results in the message
Expected "increment(strictEqual(my.key.name)", strictEqual(1), strichtEqual([myTag:XXX]))" to be called 1 time(s). But has been called 0 time(s).
Actually either Option 1 or Option 2 is what I want. For Option 3 I just tried it cause I thought this would definitely work.
Could somebody help me with that?
Thanks in advance.