I have a controller that calls newrelic.incrementMetric()
and I want to write an assert that checks that its called with the properly
my controller looks like
const newrelic = require('newrelic')
async index() {
// some stuff here
newrelic.incrementMetric('hello', 1)
}
I tried this in my test
const newrelic = require('newrelic')
// describe block here...
it('should call newRelic', async () => {
newrelic.incremetentMetric = jest.fn().mockResolvedValueOnce({});
expect (newrelic.incrementMetric).toHaveBeenCalledWith('hello', 1)
});
Whats the proper way to do this?
My code has an error
Matcher error: received value must be a mock or spy function
Received has type: function
Received has value: [Function incrementMetric]
```
Using jest.mock
factory to mock newrelic
. In the test case you have to call controller's function (A - action).
index.ts
import newrelic from 'newrelic'
class Controller {
index() {
// some stuff here
newrelic.incrementMetric('hello', 1)
}
}
export default new Controller()
index.spec.ts
import newrelic from 'newrelic'
import controller from './index'
jest.mock('newrelic', () => {
return {
incrementMetric: jest.fn(),
}
})
describe('Controller', () => {
it('should call incrementMetric with correct', () => {
controller.index()
expect(newrelic.incrementMetric).toHaveBeenCalledWith('hello', 1)
})
})