22

I have the following file I'm trying to write unit tests for:

import { document } from '../../globals';
const Overlay = () => {
  console.log(document.getElementsByTagName()); // this outputs 'undefined'
};

I'm struggling to mock the getElementsByTagName function. My test looks like this.

import { document } from '../../globals';
jest.mock('../../globals', () => ({
  document: {
    getElementsByTagName: jest.fn().mockReturnValue('foo')
  }
}));
console.log(document.getElementsByTagName()); // this outputs 'foo'

But unfortunately, the console.log in the top file always outputs undefined. It can see the document object and the getElementsByTagName mock, but the return value is always undefined.

If I console.log(document.getElementsByTagName) I get the following:

{ getElementsByTagName:
   { [Function: mockConstructor]
     _isMockFunction: true,
     getMockImplementation: [Function],
     mock: [Getter/Setter],
     mockClear: [Function],
     mockReset: [Function],
     mockReturnValueOnce: [Function],
     mockReturnValue: [Function],
     mockImplementationOnce: [Function],
     mockImplementation: [Function],
     mockReturnThis: [Function],
     mockRestore: [Function] },
}

But if I do the same in the other file I get this:

function () {
  return fn.apply(this, arguments);
}

My suspicion is that jest.mock is wrapping the jest.fn mock in another function.. any ideas?

2
  • Does it work if you mock it like this : getElementsByTagName: () => 'foo'? Sep 26, 2017 at 19:43
  • 1
    'Calling jest.mock('') returns a useful "automatic mock" you can use to spy on calls to the class constructor and all of its methods. It replaces the ES6 class with a mock constructor, and replaces all of its methods with mock functions that always return undefined.'
    – amyloula
    Oct 2, 2018 at 16:00

1 Answer 1

12

Try using .mockReturnValue() after assignment

I encountered the same behavior and found success by calling mockFn.mockReturnValue(value) after assignment.

For example:

import { document } from '../../globals.js'

jest.mock('../../globals.js', () => ({
  document: {
    getElementsByTagName: jest.fn()
  }
}))

document.getElementsByTagName.mockReturnValue('foo')

console.log(document.getElementsByTagName()); // this outputs 'foo'

I have seen both patterns within the jest documentation.

I wonder if the latter case requires some other pattern of interaction to assert on the mocked return value, but I haven't figured it out. I can't seem to replicate success using the spyOn pattern suggested in another answer here.

1
  • 5
    So weird... In my case calling mockImplementation inside test case worked like a charm, but if I define it inside jest.mock it returns undefined. Even though mock function is picked up by component
    – Lukenzo
    Feb 21, 2022 at 21:31

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.