There are a few different ways to approach it.
You can mock only foo using jest.spyOn and something like mockImplementation:
import { MyClass } from './MyClass';
import * as FooFactory from './somewhere/FooFactory';
describe('test MyClass', () => {
test('construct', () => {
const mock = jest.spyOn(FooFactory, 'foo'); // spy on foo
mock.mockImplementation((arg: string) => 'TEST'); // replace implementation
const c = new MyClass('test');
expect(c).toBeDefined();
expect(c.getState()).toEqual('TEST'); // SUCCESS
mock.mockRestore(); // restore original implementation
});
});
Similarly, you can auto-mock FooFactory with jest.mock, then provide an implementation for foo:
import { MyClass } from './MyClass';
import * as FooFactory from './somewhere/FooFactory';
jest.mock('./somewhere/FooFactory'); // auto-mock FooFactory
describe('test MyClass', () => {
test('construct', () => {
const mockFooFactory = FooFactory as jest.Mocked<typeof FooFactory>; // get correct type for mocked FooFactory
mockFooFactory.foo.mockImplementation(() => 'TEST'); // provide implementation for foo
const c = new MyClass('test');
expect(c).toBeDefined();
expect(c.getState()).toEqual('TEST'); // SUCCESS
});
});
You can also mock FooFactory using a module factory passed to jest.mock:
import { MyClass } from './MyClass';
jest.mock('./somewhere/FooFactory', () => ({
foo: () => 'TEST'
}));
describe('test MyClass', () => {
test('construct', () => {
const c = new MyClass('test');
expect(c).toBeDefined();
expect(c.getState()).toEqual('TEST'); // SUCCESS
});
});
And finally, if you plan to use the same mock across multiple test files you can mock the user module by creating a mock at ./somewhere/__mocks__/FooFactory.ts:
export function foo(arg: string) {
return 'TEST';
}
...then call jest.mock('./somewhere/FooFactory'); to use the mock in the test:
import { MyClass } from './MyClass';
jest.mock('./somewhere/FooFactory'); // use the mock
describe('test MyClass', () => {
test('construct', () => {
const c = new MyClass('test');
expect(c).toBeDefined();
expect(c.getState()).toEqual('TEST'); // SUCCESS
});
});
Answer from Brian Adams on Stack OverflowI fell into a hole setting up fetch mocking in Jest one too many times so I wrote this guide to save you time and frustration
How To Mock Fetch in Jest
Jest Unit Testing Mocking Questions
It's something of a philosophical question. The finest unit tests you can write test only your own code and not much else. The coarsest acceptance test runs your program in a production environment and invokes it via its interface. Between the two there are many levels of testing, but what to call them and which you should use is heavily context and opinion dependent. If you want to use the real moment.js, go nuts. I'd probably do that, too. I do advise not using the real system clock, though. Time-dependent tests can be fragile.
More on reddit.comIs it possible to test a single function in jest?
It depends where getUser comes from, is it imported or defined in the file.
One option would be to inject the function you can do this:
// api.js
const request = (url, getUser = getUser) => {
...
}Then in your test pass in a stub as the second argument.
More on reddit.com