Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ow.number message when NaN is encountered #232

Merged
merged 2 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion source/predicates/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ export class Predicate<T = unknown> implements BasePredicate<T> {
// We do not include type in this label as we do for other messages, because it would be redundant.
const label_ = label?.slice(this.type.length + 1);

// TODO: The NaN check can be removed when `@sindresorhus/is` is fixed: https://github.com/sindresorhus/ow/issues/231#issuecomment-1047100612
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
return `Expected ${label_ || 'argument'} to be of type \`${this.type}\` but received type \`${is(value)}\``;
return `Expected ${label_ || 'argument'} to be of type \`${this.type}\` but received type \`${Number.isNaN(value) ? 'NaN' : is(value)}\``;
},
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
validator: value => (is as any)[typeString](value),
Expand Down
4 changes: 4 additions & 0 deletions test/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ test('number', t => {
ow('12' as any, ow.number);
}, {message: 'Expected argument to be of type `number` but received type `string`'});

t.throws(() => {
ow(Number.NaN as any, ow.number);
}, {message: 'Expected argument to be of type `number` but received type `NaN`'});

t.throws(() => {
ow('12' as any, 'foo', ow.number);
}, {message: 'Expected `foo` to be of type `number` but received type `string`'});
Expand Down