Skip to content

Commit

Permalink
feat(typings): improve type information
Browse files Browse the repository at this point in the history
All functions are now type guards with an optional
generic which can be provided if you want to
override or otherwise be more specific about what
type your asserted value is.

BREAKING CHANGE
The return types have changed from eg. `boolean`
to `value is number[]`, which may result in type
errors in your projects.
  • Loading branch information
JamieMason committed Aug 8, 2021
1 parent 2cbe184 commit 4cff05e
Show file tree
Hide file tree
Showing 203 changed files with 683 additions and 502 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
packages/*/dist
packages/*/node_modules
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-after.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printExpected, printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is a valid instance of `Date` whose value occurs after that of ${otherDate}.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeAfterMatcher = () => {
export const toBeAfterMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any, otherDate: Date) {
compare(value: unknown, otherDate: Date) {
const pass = isAfter(otherDate, value);
const message = pass
? `expected ${printReceived(
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-array-of-booleans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is an `Array` containing only `Boolean` values.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeArrayOfBooleansMatcher = () => {
export const toBeArrayOfBooleansMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any) {
compare(value: unknown) {
const pass = isArrayOfBooleans(value);
const message = pass
? `expected ${printReceived(
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-array-of-numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is an `Array` containing only `Number` values.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeArrayOfNumbersMatcher = () => {
export const toBeArrayOfNumbersMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any) {
compare(value: unknown) {
const pass = isArrayOfNumbers(value);
const message = pass
? `expected ${printReceived(value)} not to be a non-empty array, containing only numbers`
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-array-of-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is an `Array` containing only `Object` values.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeArrayOfObjectsMatcher = () => {
export const toBeArrayOfObjectsMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any) {
compare(value: unknown) {
const pass = isArrayOfObjects(value);
const message = pass
? `expected ${printReceived(value)} not to be a non-empty array, containing only objects`
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-array-of-size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printExpected, printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is an `Array` containing ${size} number of values.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeArrayOfSizeMatcher = () => {
export const toBeArrayOfSizeMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any, size: number) {
compare(value: unknown, size: number) {
const pass = isArrayOfSize(size, value);
const message = pass
? `expected ${printReceived(value)} not to be an array containing exactly ${printExpected(
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-array-of-strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is an `Array` containing only `String` values.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeArrayOfStringsMatcher = () => {
export const toBeArrayOfStringsMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any) {
compare(value: unknown) {
const pass = isArrayOfStrings(value);
const message = pass
? `expected ${printReceived(value)} not to be a non-empty array, containing only strings`
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is a valid `Array` containing none or any number of items of any type.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeArrayMatcher = () => {
export const toBeArrayMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any) {
compare(value: unknown) {
const pass = isArray(value);
const message = pass
? `expected ${printReceived(value)} not to be an array`
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-async-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is a function using async/await syntax.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeAsyncFunctionMatcher = () => {
export const toBeAsyncFunctionMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any) {
compare(value: unknown) {
const pass = isAsyncFunction(value);
const message = pass
? `expected ${printReceived(value)} not to be a function using async/await syntax`
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-before.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printExpected, printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is a valid instance of `Date` whose value occurs before that of ${otherDate}.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeBeforeMatcher = () => {
export const toBeBeforeMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any, otherDate: Date) {
compare(value: unknown, otherDate: Date) {
const pass = isBefore(otherDate, value);
const message = pass
? `expected ${printReceived(
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is `true`, `false`, `new Boolean(true)`, or `new Boolean(false)`.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeBooleanMatcher = () => {
export const toBeBooleanMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any) {
compare(value: unknown) {
const pass = isBoolean(value);
const message = pass
? `expected ${printReceived(value)} not to be true, false, or an instance of Boolean`
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-calculable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Assert value can be used in Mathemetic calculations despite not being a `Number`, for example `'1' * '2' === 2` whereas `'wut?' * 2 === NaN`.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeCalculableMatcher = () => {
export const toBeCalculableMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any) {
compare(value: unknown) {
const pass = isCalculable(value);
const message = pass
? `expected ${printReceived(value)} not to be coercible for use in mathemetical operations`
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is an instance of `Date`.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeDateMatcher = () => {
export const toBeDateMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any) {
compare(value: unknown) {
const pass = isDate(value);
const message = pass
? `expected ${printReceived(value)} not to be an instance of Date`
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-decimal-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is a `Number` with positive decimal places.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeDecimalNumberMatcher = () => {
export const toBeDecimalNumberMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any) {
compare(value: unknown) {
const pass = isDecimalNumber(value);
const message = pass
? `expected ${printReceived(value)} not to be a number with positive decimal places`
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-divisible-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printExpected, printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is a `Number` which results in a whole number when divided by ${otherNumber}.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeDivisibleByMatcher = () => {
export const toBeDivisibleByMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any, otherNumber: number) {
compare(value: unknown, otherNumber: number) {
const pass = isDivisibleBy(otherNumber, value);
const message = pass
? `expected ${printReceived(value)} not to be divisible by ${printExpected(otherNumber)}`
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-empty-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is a valid `Array` containing no items.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeEmptyArrayMatcher = () => {
export const toBeEmptyArrayMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any) {
compare(value: unknown) {
const pass = isEmptyArray(value);
const message = pass
? `expected ${printReceived(value)} not to be an array containing no items`
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-empty-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is a valid `Object` containing no instance members.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeEmptyObjectMatcher = () => {
export const toBeEmptyObjectMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any) {
compare(value: unknown) {
const pass = isEmptyObject(value);
const message = pass
? `expected ${printReceived(value)} not to be an empty object`
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-empty-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is a valid `String` containing no characters.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeEmptyStringMatcher = () => {
export const toBeEmptyStringMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any) {
compare(value: unknown) {
const pass = isEmptyString(value);
const message = pass
? `expected ${printReceived(value)} not to be an empty string or empty instance of String`
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-even-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is an even `Number`.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeEvenNumberMatcher = () => {
export const toBeEvenNumberMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any) {
compare(value: unknown) {
const pass = isEvenNumber(value);
const message = pass
? `expected ${printReceived(value)} not to be an even number`
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-more-jasmine/src/to-be-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printReceived } from 'jest-matcher-utils';

declare global {
namespace jasmine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<T> {
/**
* Asserts that ${value} is a `Function`.
Expand All @@ -14,9 +15,9 @@ declare global {
}
}

export const toBeFunctionMatcher = () => {
export const toBeFunctionMatcher: jasmine.CustomMatcherFactory = () => {
return {
compare(value: any) {
compare(value: unknown) {
const pass = isFunction(value);
const message = pass
? `expected ${printReceived(value)} not to be a function or async function`
Expand Down
Loading

0 comments on commit 4cff05e

Please sign in to comment.