JavaScriptテストにおけるJestモックの高度な活用:同じ関数を2回モックする
Jest で同じ関数を異なる引数で2回モックする方法
mockReturnValueOnce を使用する
mockReturnValueOnce
メソッドを使用すると、モック関数が1回だけ特定の値を返すように設定できます。このメソッドを2回呼び出すことで、異なる引数に対して異なる値を返すように設定することができます。
// mock.js
module.exports = {
myFunction: jest.fn(),
};
// test.js
const mock = require('./mock');
jest.mock('./mock');
test('mockReturnValueOnce を使用する', () => {
mock.myFunction.mockReturnValueOnce(10);
mock.myFunction.mockReturnValueOnce(20);
const result1 = mock.myFunction(1);
const result2 = mock.myFunction(2);
expect(result1).toBe(10);
expect(result2).toBe(20);
});
この例では、myFunction
関数を2回モックし、1回目の呼び出しでは 10
を、2回目の呼び出しでは 20
を返すように設定しています。
nthCalledWith マッチャーを使用する
nthCalledWith
マッチャーを使用すると、モック関数がn回目の呼び出しで特定の引数を受け取ったことを確認することができます。このマッチャーを使用して、同じ関数を異なる引数で2回呼び出したことを確認し、それぞれの呼び出しで期待される引数が渡されたことを確認することができます。
// mock.js
module.exports = {
myFunction: jest.fn(),
};
// test.js
const mock = require('./mock');
jest.mock('./mock');
test('nthCalledWith マッチャーを使用する', () => {
mock.myFunction(1);
mock.myFunction(2);
expect(mock.myFunction).nthCalledWith(1, 1);
expect(mock.myFunction).nthCalledWith(2, 2);
});
どちらの方法を使用するべきか
どちらの方法を使用するかは、テストの状況によって異なります。
mockReturnValueOnce
は、モック関数が特定の引数に対して特定の値を返す必要がある場合に適しています。nthCalledWith
マッチャーは、モック関数が特定の引数で呼び出されたことを確認する必要がある場合に適しています。
// mock.js
module.exports = {
myFunction: jest.fn(),
};
// test.js
const mock = require('./mock');
jest.mock('./mock');
test('Mock the same function twice with different arguments', () => {
// Mock the myFunction function to return different values for different arguments
mock.myFunction.mockImplementation((arg) => {
if (arg === 1) {
return 'Hello';
} else if (arg === 2) {
return 'World';
} else {
return 'Default value';
}
});
// Call the myFunction function with different arguments
const result1 = mock.myFunction(1);
const result2 = mock.myFunction(2);
// Assert that the function returned the expected values
expect(result1).toBe('Hello');
expect(result2).toBe('World');
});
In this example, the myFunction
function is mocked to return different values for different arguments using the mockImplementation
method. The mockImplementation
method takes a callback function that is called whenever the mocked function is called. The callback function can return any value, and it can also access the arguments passed to the mocked function.
In this case, the callback function checks the arg
argument and returns the appropriate string value. The result1
variable is assigned the result of calling the myFunction
function with the argument 1
, and the result2
variable is assigned the result of calling the myFunction
function with the argument 2
.
Finally, the expect
statements are used to assert that the result1
and result2
variables are equal to the expected values.
// mock.js
module.exports = {
myFunction: jest.fn(),
};
// test.js
const mock = require('./mock');
jest.mock('./mock');
test('Mock the same function twice with different arguments using mockReturnValue and mockReset', () => {
// Mock the myFunction function to return different values
mock.myFunction.mockReturnValue('Hello');
// Call the myFunction function with argument 1
const result1 = mock.myFunction(1);
// Reset the mock function
mock.myFunction.mockReset();
// Mock the myFunction function to return a different value
mock.myFunction.mockReturnValue('World');
// Call the myFunction function with argument 2
const result2 = mock.myFunction(2);
// Assert that the function returned the expected values
expect(result1).toBe('Hello');
expect(result2).toBe('World');
});
In this approach, the mockReturnValue
method is used to mock the myFunction
function to return a specific value. The mockReset
method is then used to reset the mock function to its original state. This allows us to mock the function again with a different return value.
Using multiple spyOn calls
// mock.js
module.exports = {
myFunction: jest.fn(),
};
// test.js
const mock = require('./mock');
jest.mock('./mock');
test('Mock the same function twice with different arguments using multiple spyOn calls', () => {
// Spy on the myFunction function
const spy1 = jest.spyOn(mock, 'myFunction');
// Call the myFunction function with argument 1
const result1 = mock.myFunction(1);
// Mock the myFunction function to return a different value
spy1.mockReturnValue('World');
// Call the myFunction function with argument 2
const result2 = mock.myFunction(2);
// Assert that the function returned the expected values
expect(result1).toBe('Hello');
expect(result2).toBe('World');
});
In this approach, the spyOn
method is used to create a spy on the myFunction
function. The spy can then be used to mock the function to return different values for different arguments.
Here is a table summarizing the different approaches:
Approach | Description |
---|---|
mockReturnValueOnce | Use mockReturnValueOnce to mock the function to return different values for each call. |
nthCalledWith | Use nthCalledWith to verify that the function was called with the expected arguments. |
mockReturnValue and mockReset | Use mockReturnValue to mock the function to return a value, then use mockReset to reset the function, and then use mockReturnValue again to mock the function with a different value. |
Multiple spyOn calls | Use spyOn to create a spy on the function, then use the spy to mock the function to return different values for different arguments. |
javascript reactjs jestjs