Код: Выделить всё
service.jsКод: Выделить всё
class Service {
constructor() {
this.value = 'A';
}
getValue() {
return this.value;
}
}
export default new Service();
< /code>
main.jsКод: Выделить всё
import service from './service';
export function run() {
const value = service.getValue();
console.log('Executing service with value ' + value);
}
< /code>
main.test.jsimport { jest } from '@jest/globals';
import { run } from './main';
jest.mock('./service', () => {
return {
__esModule: true,
default: {
getValue: jest.fn().mockReturnValue('mocked_value'),
},
};
});
describe('test run', () => {
it('should log the correct message', () => {
console.log = jest.fn();
run();
expect(console.log).toHaveBeenCalledWith('Executing service with value mocked_value');
});
});
< /code>
What happens?
Expected: "Executing service with value mocked_value"
Received: "Executing service with value A"
< /code>
Can anyone help getting the mock work? Thanks.
Подробнее здесь: https://stackoverflow.com/questions/794 ... n-instance
Мобильная версия