You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Particularly when using wildcards, I might want to make assertions about how the client was used.
Sample (with workaround to show want I want to do - should run with npx ts-node if the client and the mock are installed):
import{Client}from'@elastic/elasticsearch';importMock,{MockPattern}from'@elastic/elasticsearch-mock';// Setupconstmock=newMock();constclient=newClient({node: 'http://localhost:9200',Connection: mock.getConnection(),});// Prepare the mock for my "test"// Use this to track calls (could use something more sophisticated like jest.fn)constesIndexMockCalls: Array<MockPattern>=[];mock.add({method: ['POST','PUT'],path: '/:index/_doc/:id',},(params: MockPattern)=>{// Record the callesIndexMockCalls.push(params);return{status: 'ok'};});// Run the "test"client.index({index: 'myindex',id: 'mydocument',body: {foo: 'bar',},}).then(()=>{// Make "assertions"// Check that only one call was madeif(esIndexMockCalls.length!==1){thrownewError('Expected one call');}// Check that we indexed the right thing in the right placeif(esIndexMockCalls[0].path!=='/myindex/_doc/mydocument'){thrownewError('Expected index of mydocument in myindex');}console.log('Indexed mydocument in myindex as expected');});
The alternative is to put these assertions in the callback, eg:
mock.add({method: ['POST','PUT'],path: '/:index/_doc/:id',},(params: MockPattern)=>{if(params.path!=='myindex/_doc/mydocument'{thrownewError('Unexpected index path');}return{status: 'ok'};});
but this doesn't allow for things like checking how many calls were made. Eg if I have code where I index multiple documents in parallel, I want to be able to check that each one got indexed exactly once.
Is something like this handled by the mock client? Or is using something like jest.fn to track the calls from within the callback the best way of going about this?
The text was updated successfully, but these errors were encountered:
Hello! Currently, it's not possible, but it would be a nice feature!
It should be opt-in in my opinion, so if a user want to use a different mock method (jest.fn for example), they can do it.
Would you like to work on it? :)
It's not great, because a failure doesn't make a distinction between "wasn't actually called at all" and "was called with the wrong index name." You could work around this by matching any index and asserting the shape of the call to the deleteSpy, but it's quite verbose.
Naturally, the same approach (of passing a spy with mock implementation as the response body getter) will work on any method/path.
Particularly when using wildcards, I might want to make assertions about how the client was used.
Sample (with workaround to show want I want to do - should run with
npx ts-node
if the client and the mock are installed):The alternative is to put these assertions in the callback, eg:
but this doesn't allow for things like checking how many calls were made. Eg if I have code where I index multiple documents in parallel, I want to be able to check that each one got indexed exactly once.
Is something like this handled by the mock client? Or is using something like
jest.fn
to track the calls from within the callback the best way of going about this?The text was updated successfully, but these errors were encountered: