I mocked request-promise
module, everything is fine except tslint complains a warning.
Here is my unit test:
import * as request from 'request-promise';
jest.mock('request-promise', () => {
return {
__esModule: true,
get: jest.fn(),
};
});
describe('csv.service.ts', () => {
it('should mock request-promise module correctly', () => {
expect(jest.isMockFunction(request.get)).toBeTruthy();
});
it('should mock get method correctly', async () => {
(request.get as jest.Mock).mockResolvedValueOnce('go?');
const actualValue = await request.get('1');
expect(actualValue).toBe('go?');
});
});
Refactor this redundant 'await' on a non-promise. (no-invalid-await)tslint(1)
It seems that request.get('1')
is not treat as a promise after executing mockResolvedValueOnce
on request.get
.
update
If I remove async/await
, the second unit test will failed.
FAIL src/tests/services/core/csv.service.spec.ts
csv.service.ts
✓ should mock request-promise module correctly (5ms)
✕ should mock get method correctly (9ms)
● csv.service.ts › should mock get method correctly
expect(received).toBe(expected) // Object.is equality
Expected: "go?"
Received: {}
Difference:
Comparing two different types of values. Expected string but received object.
17 |
18 | const actualValue = request.get('1');
> 19 | expect(actualValue).toBe('go?');
| ^
20 | });
21 | });
22 |
at Object.it (src/tests/services/core/csv.service.spec.ts:19:25)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time: 1.203s, estimated 2s
I use typecast to solve this issue. Because I am sure request.get('1')
is a Promise
after executing mockResolvedValueOnce
on it.
const actualValue = await (request.get('1') as PromiseLike<string>);