Имитаторы классов ES6
Jest может использоваться для создания имитаторов классов ES6, импортируемых в файлы, которые вы желаете протестировать.
Классы ES6 являются функциями-конструкторами с некоторым синтаксическим сахаром. Поэтому любой объект-имитатор для класса ES6 должен быть функцией или фактическим классом ES6 (который, в свою очередь, также является функцией). Таким образом, вы можете имитировать поведение, используя функции создания имитаторов.
Пример класса ES6
Мы будем использовать пример класса, который воспроизводит звуковые файлы, Класс soundplayer
, и потребительский класс, который использует этот класс, SoundPlayerConsumer
. Мы создадим имитатор для SoundPlayer
в наших тестах для SoundPlayerConsumer
.
export default class SoundPlayer {
constructor() {
this.foo = 'bar';
}
playSoundFile(fileName) {
console.log('Playing sound file ' + fileName);
}
}
import SoundPlayer from './sound-player';
export default class SoundPlayerConsumer {
constructor() {
this.soundPlayer = new SoundPlayer();
}
playSomethingCool() {
const coolSoundFileName = 'song.mp3';
this.soundPlayer.playSoundFile(coolSoundFileName);
}
}
4 способа создания имитатора класса ES6
Автоматический имитатор
Зовущий jest.mock('/ sound-player')
возвращает полезный "автоматический макет", который можно использовать для отслеживания вызовов конструктора класса и всех его методов. Он заменит класс ES6 на конструктор имитатора, а также заменит все его методы mock-функциями, всегда возвращающими undefined
. Method calls are saved in theAutomaticMock.mock.instances[index].methodName.mock.calls
.
If you use arrow functions in your classes, they will not be part of the mock. The reason for that is that arrow functions are not present on the object's prototype, they are merely properties holding a reference to a function.
Если вам не нужно заменять реализацию класса, это самый простой вариант для настройки. Например:
import SoundPlayer from './sound-player';
import SoundPlayerConsumer from './sound-player-consumer';
jest.mock('./sound-player'); // SoundPlayer is now a mock constructor
beforeEach(() => {
// Clear all instances and calls to constructor and all methods:
SoundPlayer.mockClear();
});
it('We can check if the consumer called the class constructor', () => {
const soundPlayerConsumer = new SoundPlayerConsumer();
expect(SoundPlayer).toHaveBeenCalledTimes(1);
});
it('We can check if the consumer called a method on the class instance', () => {
// Show that mockClear() is working:
expect(SoundPlayer).not.toHaveBeenCalled();
const soundPlayerConsumer = new SoundPlayerConsumer();
// Constructor should have been called again:
expect(SoundPlayer).toHaveBeenCalledTimes(1);
const coolSoundFileName = 'song.mp3';
soundPlayerConsumer.playSomethingCool();
// mock.instances is available with automatic mocks:
const mockSoundPlayerInstance = SoundPlayer.mock.instances[0];
const mockPlaySoundFile = mockSoundPlayerInstance.playSoundFile;
expect(mockPlaySoundFile.mock.calls[0][0]).toBe(coolSoundFileName);
// Equivalent to above check:
expect(mockPlaySoundFile).toHaveBeenCalledWith(coolSoundFileName);
expect(mockPlaySoundFile).toHaveBeenCalledTimes(1);
});
Создаваемые вручную имитаторы
Create a manual mock by saving a mock implementation in the __mocks__
folder. Это позволяет указать реализацию и использовать ее в тестовых файлах.
// Import this named export into your test file:
export const mockPlaySoundFile = jest.fn();
const mock = jest.fn().mockImplementation(() => {
return {playSoundFile: mockPlaySoundFile};
});
export default mock;
Импортируйте макет и метод макета, совместно используемые всеми экземплярами:
import SoundPlayer, {mockPlaySoundFile} from './sound-player';
import SoundPlayerConsumer from './sound-player-consumer';
jest.mock('./sound-player'); // SoundPlayer is now a mock constructor
beforeEach(() => {
// Clear all instances and calls to constructor and all methods:
SoundPlayer.mockClear();
mockPlaySoundFile.mockClear();
});
it('We can check if the consumer called the class constructor', () => {
const soundPlayerConsumer = new SoundPlayerConsumer();
expect(SoundPlayer).toHaveBeenCalledTimes(1);
});
it('We can check if the consumer called a method on the class instance', () => {
const soundPlayerConsumer = new SoundPlayerConsumer();
const coolSoundFileName = 'song.mp3';
soundPlayerConsumer.playSomethingCool();
expect(mockPlaySoundFile).toHaveBeenCalledWith(coolSoundFileName);
});
Calling jest.mock()
with the module factory parameter
jest.mock(path, moduleFactory)
takes a module factory argument. A module factory is a function that returns the mock.
Чтобы издеваться над функцией конструктора, фабрика модулей должна возвращать функцию конструктора. Другими словами, фабрика модулей должна быть функцией, которая возвращает функцию-функцию более высокого порядка (HOF).
import SoundPlayer from './sound-player';
const mockPlaySoundFile = jest.fn();
jest.mock('./sound-player', () => {
return jest.fn().mockImplementation(() => {
return {playSoundFile: mockPlaySoundFile};
});
});