After converting some files in a project to TypeScript, I encountered a test failure related to the following code:
expect(mocks.request).toHaveBeenCalledWith({
headers: { 'Content-Type': 'bar' },
method: 'put',
params: file.slice(),
path: 'foo',
responseType: 'text',
cancelToken: expect.any(Object),
});
The error message is as follows:
expect(jest.fn()).toHaveBeenCalledWith(...expected)
- Expected
+ Received
Object {
- "cancelToken": Any<Object>,
+ "cancelToken": CancelToken {
+ "promise": Promise {},
+ },
"headers": Object {
"Content-Type": "bar",
This particular section of code is being tested:
this.cancelToken = axios.CancelToken.source();
return request({
method: 'put',
path: presignedUrl,
params: file.slice(),
cancelToken: this.cancelToken.token,
headers: {
'Content-Type': contentType,
},
});
I attempted to update the test using expect.anything()
, like so:
cancelToken: expect.anything(),
Unfortunately, this modification led to another error:
- Expected
+ Received
Object {
- "cancelToken": Anything,
+ "cancelToken": CancelToken {
+ "promise": Promise {},
+ },
Any suggestions on how to resolve this issue with my test code?
Note: I made sure to compare the value passed to the function request
by adding a
console.log(this.cancelToken.token)
statement, and it remained consistent between the main branch and the new branch. Therefore, I am puzzled as to why Jest failed in this manner.