Skip to content

Latest commit

 

History

History
34 lines (22 loc) · 946 Bytes

prefer-promise-strategies.md

File metadata and controls

34 lines (22 loc) · 946 Bytes

Prefer Promise Strategies

This rule recommends using resolveTo(X) and rejectWith(X) instead of returnValue(Promise.resolve(X)) and returnValue(Promise.reject(X)).

Aside from leading to more concise code, transforming returnValue(Promise.reject(X)) can also avoid an unwanted UnhandledPromiseRejectionWarning.

Rule details

Examples of incorrect code:

const spy = jasmine.createSpy();

spy.withArgs(0).returnValue(Promise.resolve(123));
spy.and.returnValue(Promise.reject(123));

Examples of correct code:

const spy = jasmine.createSpy();

spy.withArgs(0).resolveTo(123);
spy.and.rejectWith(123);

Limitations

To avoid false positives, this rule only considers returnValue calls that match the pattern X.{and,withArgs(Y)}.returnValue(Z). Thus, the following incorrect code is not recognized:

const strategy = jasmine.createSpy().and;
strategy.returnValue(Promise.resolve(123));