- У меня есть класс API ( baseapi < /strong>), что настройка возвращает прокси к Оберните набор вызовов REST
Этот класс имеет ручную макет < /li>
< /ul>
< /li>
У меня есть еще один файл, где этот класс инициализируется на уровне файла
Эти методы API-прокси-ED (и post в основном) вызываются в функциях в этом файлеКод: Выделить всё
get
< /ul>
// auth/base.js
class BaseApi {
constructor() {
return new Proxy(this, {
get(api, calledProp, receiver) {
const reflectedProp = Reflect.get(api, calledProp, receiver);
const fetchCalls = ['post', 'delete', 'get', 'patch'];
if (fetchCalls.indexOf(calledProp) >= 0) {
return (...args) => api.apiCall(calledProp.toUpperCase(), ...args);
}
return reflectedProp;
},
});
}
async apiCall(method, endpoint, headers, queryParams, body = null) {
//implementation ...
}
}
module.exports = { BaseApi };
< /code>
// auth/__mocks__/base.js
class BaseApi {
apiCall = jest.fn().mockImplementation(method, endpoint, headers, queryParams, body = null)
get = jest.fn().mockResolvedValue({
ok: false,
});
}
module.exports = { BaseApi };
< /code>
// main/index.js
const { BaseApi } = require('../auth/base');
const baseApi = new BaseApi();
// ... more stuff
const processRequest = async function() {
const processCall = await baseApi.get(route, headers);
if (processCall.ok) {
return 'Success'
} else {
return null
}
}
module.exports = { processRequest };
< /code>
// main/index.test.js
jest.mock('../auth/base');
const { BaseApi } = require('../auth/base');
const { processRequest } = require('./index');
describe('process', () => {
it('correct based on the global mock', async () => { // this is working as expected
const result = await processRequest();
expect(result).toBe(null);
});
it('this test is erro-ing', async () => {
/*
This fails with the following message: Property `get` does not exist in the provided object
*/
jest.spyOn(BaseApi, 'get').mockResolvedValueOnce({ ok: true });
const result = await processRequest();
expect(result).toBe('Success');
});
});
< /code>
So, my question is, how can I change the return of a method of a manual mocked class on a specific test?
Подробнее здесь: https://stackoverflow.com/questions/794 ... ss-on-jest