I am testing the functionality of two functions in the same file. One of the functions is supposed to call the other, and I need to verify that this interaction occurs.
import * as strings from './strings';
const generateUuidSpy = jest.spyOn(strings, 'generateUuid');
describe('getContextId()', () => {
it('should return a string id when provided with context and a number', () => {
const actual = strings.getContextId('testQueue', 5);
const expected = 's1-s5';
expect(typeof actual).toBe('string');
expect(actual).toEqual(expected);
});
it('should trigger generateUuid()', () => {
strings.getContextId('notTestQueue');
expect(generateUuidSpy).toHaveBeenCalled();
});
});
describe('generateUuid()', () => {
it('should return a UUID string with 36 characters', () => {
generateUuidSpy.mockRestore();
const actual = strings.generateUuid();
const expected = /[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}/;
expect(actual.length).toBe(36);
expect(typeof actual).toBe('string');
expect(expected.test(actual)).toBe(true);
});
});
However, I encountered the following error:
expect(jest.fn()).toHaveBeenCalled()
Expected mock function to have been called, but it was not called.
12 | it('it should execute generateUuid()', () => {
13 | strings.getContextId('notTestQueue');
> 14 | expect(generateUuidSpy).toHaveBeenCalled();
| ^
15 | });
16 | });
17 |
at Object.toHaveBeenCalled (src/helpers/strings.test.js:14:29)