Jestでmoduleのstubを作る
簡易
code:usecases.ts
import { repository } from './repositories';
export function usecase() {
const result = repository();
return result * 10;
}
code:repositories.ts
export function repository() {
const result = db;
return result;
}
const db = 42;
code:usecases.test.ts
import { repository } from './repositories';
import { usecase } from './usecases';
jest.mock('./repositories.ts');
describe('', () => {
test('', () => {
(repository as jest.Mock).mockReturnValue(10);
const sut = usecase();
expect(sut).toEqual(100);
});
});