![](/img/trans.png)
[英]How can i configure tslint to not throw warning when underscore in a variable name?
[英]Throw tslint warning when use mockResolvedValueOnce and async/await
我嘲笑了request-promise
模塊,除了tslint抱怨警告之外,一切都很好。
這是我的單元測試:
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?');
});
});
在無承諾的情況下重構此冗余的“等待”。 (無無效等待)tslint(1)
似乎request.get('1')
不當作執行后承諾mockResolvedValueOnce
上request.get
。
更新
如果刪除async/await
,則第二個單元測試將失敗。
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
我使用類型轉換解決此問題。 因為我確定request.get('1')
在對它執行了mockResolvedValueOnce
之后是一個Promise
。
const actualValue = await (request.get('1') as PromiseLike<string>);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.