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 Option
s. 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.
The problem in your code is that you are passing an array as the third argument, and, as you can see in the returned message, it is trying to check it with an strictEqual (===). You can get the desired result using the deepEqual
function provided by mockito.
verify(mockedBufferedMetricsLogger.increment('my.key.name', 1, deepEqual([`myTag:${XXX}`]))).once();
And be sure you are dealing with the parameters correctly, as I had to change the code a bit to make it work. Here's my full working code:
// myScript.ts
import { BufferedMetricsLogger } from 'datadog-metrics';
export class MyClass {
private bufferedMetricsLogger: BufferedMetricsLogger;
constructor(bufferedMetricsLogger: 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, deepEqual } from 'ts-mockito';
import { BufferedMetricsLogger } from 'datadog-metrics';
import { MyClass } from './myScript';
describe('Testing MyClass', () => {
it('verify that myMethod increments', () => {
const mockedBufferedMetricsLogger: BufferedMetricsLogger = mock(BufferedMetricsLogger);
const bufferedMetricsLogger: BufferedMetricsLogger = instance(mockedBufferedMetricsLogger);
const myClassOption1 = new MyClass(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, deepEqual([`myTag:XXX`]))).once();
})
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With