While working with the request-promise
module, everything seems to be functioning correctly except for a warning from tslint.
Below 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 accurately mock the request-promise module', () => {
expect(jest.isMockFunction(request.get)).toBeTruthy();
});
it('should accurately mock the get method', async () => {
(request.get as jest.Mock).mockResolvedValueOnce('go?');
const actualValue = await request.get('1');
expect(actualValue).toBe('go?');
});
});
The linter warns about using 'await' on a non-promise. (no-invalid-await)tslint(1)
It appears that request.get('1')
is not being recognized as a promise after executing mockResolvedValueOnce
on request.get
.
update
If I remove the async/await
, the second unit test will fail.
FAIL src/tests/services/core/csv.service.spec.ts
csv.service.ts
✓ should accurately mock the request-promise module (5ms)
✕ should accurately mock the get method (9ms)
● csv.service.ts › should accurately mock the get method
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