Skip to content

Commit

Permalink
chore: migrate to named exports from rules (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Oct 11, 2021
1 parent 405f1e7 commit 9966215
Show file tree
Hide file tree
Showing 132 changed files with 667 additions and 849 deletions.
10 changes: 9 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,13 @@
"no-trailing-spaces": 1,
"object-curly-spacing": [1, "always"],
"quotes": ["warn", "single", { "avoidEscape": true }]
}
},
"overrides": [
{
"files": "src/matchers/*/index.js",
"rules": {
"import/no-default-export": "error"
}
}
]
}
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ Since the [breaking changes]() in `25.5.0` you may also need to update your `tsc
Also note that when adding this for the first time this affects which files are compiled by the TypeScript compiler and you might need to add the `include` property as well. See the [TypeScript docs](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) for more details.

If the above import syntax does not work, replace it with the following:

```ts
/// <reference types="jest-extended" />
```
/// <reference types="jest-extended" />
```

## Asymmetric matchers

All matchers described in the API are also asymmetrical since [jest version 23](https://jestjs.io/blog/2018/05/29/jest-23-blazing-fast-delightful-testing#custom-asymmetric-matchers):
Expand All @@ -187,7 +187,6 @@ test('passes when using an asymmetrical matcher', () => {

#### .pass(message)


Passing assertion.

```js
Expand All @@ -196,7 +195,6 @@ expect().pass('should pass');

#### .fail(message)


Failing assertion.

```js
Expand Down Expand Up @@ -303,9 +301,7 @@ Use `.toIncludeAllPartialMembers` when checking if an `Array` contains all of th

```js
test('passes when given array values match the partial members of the set', () => {
expect([{ foo: 'bar', baz: 'qux' }]).toIncludeAllPartialMembers([
{ foo: 'bar' },
]);
expect([{ foo: 'bar', baz: 'qux' }]).toIncludeAllPartialMembers([{ foo: 'bar' }]);
});
```

Expand Down Expand Up @@ -351,8 +347,8 @@ Use `.toSatisfyAny` when you want to use a custom matcher by supplying a predica
```js
test('passes when any value in array pass given predicate', () => {
const isOdd = el => el % 2 === 1;
expect([2,3,6,8]).toSatisfyAny(isOdd);
expect([2,4,8,12]).not.toSatisfyAny(isOdd);
expect([2, 3, 6, 8]).toSatisfyAny(isOdd);
expect([2, 4, 8, 12]).not.toSatisfyAny(isOdd);
});
```

Expand Down
6 changes: 3 additions & 3 deletions src/matchers/fail/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ const failMessage = message => {
else return () => 'fails by .fail() assertion';
};

export default {
fail: (expected, message) => ({ pass: predicate(), message: failMessage(message) }),
};
export function fail(expected, message) {
return { pass: predicate(), message: failMessage(message) };
}
2 changes: 1 addition & 1 deletion src/matchers/fail/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import matcher from './';
import * as matcher from './';

expect.extend(matcher);

Expand Down
195 changes: 64 additions & 131 deletions src/matchers/index.js
Original file line number Diff line number Diff line change
@@ -1,131 +1,64 @@
import failMatcher from './fail';
import passMatcher from './pass';
import toBeAfterMatcher from './toBeAfter';
import toBeArrayMatcher from './toBeArray';
import toBeArrayOfSizeMatcher from './toBeArrayOfSize';
import toBeBeforeMatcher from './toBeBefore';
import toBeBooleanMatcher from './toBeBoolean';
import toBeDateMatcher from './toBeDate';
import toBeDateStringMatcher from './toBeDateString';
import toBeEmptyMatcher from './toBeEmpty';
import toBeEmptyObjectMatcher from './toBeEmptyObject';
import toBeEvenMatcher from './toBeEven';
import toBeExtensibleMatcher from './toBeExtensible';
import toBeFalseMatcher from './toBeFalse';
import toBeFiniteMatcher from './toBeFinite';
import toBeFrozenMatcher from './toBeFrozen';
import toBeFunctionMatcher from './toBeFunction';
import toBeHexadecimalMatcher from './toBeHexadecimal';
import toBeIntegerMatcher from './toBeInteger';
import toBeNaNMatcher from './toBeNaN';
import toBeNegativeMatcher from './toBeNegative';
import toBeNilMatcher from './toBeNil';
import toBeNumberMatcher from './toBeNumber';
import toBeObjectMatcher from './toBeObject';
import toBeOddMatcher from './toBeOdd';
import toBeOneOfMatcher from './toBeOneOf';
import toBePositiveMatcher from './toBePositive';
import toBeSealedMatcher from './toBeSealed';
import toBeStringMatcher from './toBeString';
import toBeSymbolMatcher from './toBeSymbol';
import toBeTrueMatcher from './toBeTrue';
import toBeValidDateMatcher from './toBeValidDate';
import toBeWithinMatcher from './toBeWithin';
import toContainAllEntriesMatcher from './toContainAllEntries';
import toContainAllKeysMatcher from './toContainAllKeys';
import toContainAllValuesMatcher from './toContainAllValues';
import toContainAnyEntriesMatcher from './toContainAnyEntries';
import toContainAnyKeysMatcher from './toContainAnyKeys';
import toContainAnyValuesMatcher from './toContainAnyValues';
import toContainEntriesMatcher from './toContainEntries';
import toContainEntryMatcher from './toContainEntry';
import toContainKeyMatcher from './toContainKey';
import toContainKeysMatcher from './toContainKeys';
import toContainValueMatcher from './toContainValue';
import toContainValuesMatcher from './toContainValues';
import toEndWithMatcher from './toEndWith';
import toEqualCaseInsensitiveMatcher from './toEqualCaseInsensitive';
import toHaveBeenCalledAfterMatcher from './toHaveBeenCalledAfter';
import toHaveBeenCalledBeforeMatcher from './toHaveBeenCalledBefore';
import toHaveBeenCalledOnceMatcher from './toHaveBeenCalledOnce';
import toIncludeMatcher from './toInclude';
import toIncludeAllMembersMatcher from './toIncludeAllMembers';
import toIncludeAllPartialMembersMatcher from './toIncludeAllPartialMembers';
import toIncludeAnyMembersMatcher from './toIncludeAnyMembers';
import toIncludeMultipleMatcher from './toIncludeMultiple';
import toIncludeRepeatedMatcher from './toIncludeRepeated';
import toIncludeSameMembersMatcher from './toIncludeSameMembers';
import toRejectMatcher from './toReject';
import toResolveMatcher from './toResolve';
import toSatisfyMatcher from './toSatisfy';
import toSatisfyAllMatcher from './toSatisfyAll';
import toSatisfyAnyMatcher from './toSatisfyAny';
import toStartWithMatcher from './toStartWith';
import toThrowWithMessageMatcher from './toThrowWithMessage';

// this is absolutely horrible, but all matchers are default exports of an object with the name of the matcher

export const fail = failMatcher.fail;
export const pass = passMatcher.pass;
export const toBeAfter = toBeAfterMatcher.toBeAfter;
export const toBeArray = toBeArrayMatcher.toBeArray;
export const toBeArrayOfSize = toBeArrayOfSizeMatcher.toBeArrayOfSize;
export const toBeBefore = toBeBeforeMatcher.toBeBefore;
export const toBeBoolean = toBeBooleanMatcher.toBeBoolean;
export const toBeDate = toBeDateMatcher.toBeDate;
export const toBeDateString = toBeDateStringMatcher.toBeDateString;
export const toBeEmpty = toBeEmptyMatcher.toBeEmpty;
export const toBeEmptyObject = toBeEmptyObjectMatcher.toBeEmptyObject;
export const toBeEven = toBeEvenMatcher.toBeEven;
export const toBeExtensible = toBeExtensibleMatcher.toBeExtensible;
export const toBeFalse = toBeFalseMatcher.toBeFalse;
export const toBeFinite = toBeFiniteMatcher.toBeFinite;
export const toBeFrozen = toBeFrozenMatcher.toBeFrozen;
export const toBeFunction = toBeFunctionMatcher.toBeFunction;
export const toBeHexadecimal = toBeHexadecimalMatcher.toBeHexadecimal;
export const toBeInteger = toBeIntegerMatcher.toBeInteger;
export const toBeNaN = toBeNaNMatcher.toBeNaN;
export const toBeNegative = toBeNegativeMatcher.toBeNegative;
export const toBeNil = toBeNilMatcher.toBeNil;
export const toBeNumber = toBeNumberMatcher.toBeNumber;
export const toBeObject = toBeObjectMatcher.toBeObject;
export const toBeOdd = toBeOddMatcher.toBeOdd;
export const toBeOneOf = toBeOneOfMatcher.toBeOneOf;
export const toBePositive = toBePositiveMatcher.toBePositive;
export const toBeSealed = toBeSealedMatcher.toBeSealed;
export const toBeString = toBeStringMatcher.toBeString;
export const toBeSymbol = toBeSymbolMatcher.toBeSymbol;
export const toBeTrue = toBeTrueMatcher.toBeTrue;
export const toBeValidDate = toBeValidDateMatcher.toBeValidDate;
export const toBeWithin = toBeWithinMatcher.toBeWithin;
export const toContainAllEntries = toContainAllEntriesMatcher.toContainAllEntries;
export const toContainAllKeys = toContainAllKeysMatcher.toContainAllKeys;
export const toContainAllValues = toContainAllValuesMatcher.toContainAllValues;
export const toContainAnyEntries = toContainAnyEntriesMatcher.toContainAnyEntries;
export const toContainAnyKeys = toContainAnyKeysMatcher.toContainAnyKeys;
export const toContainAnyValues = toContainAnyValuesMatcher.toContainAnyValues;
export const toContainEntries = toContainEntriesMatcher.toContainEntries;
export const toContainEntry = toContainEntryMatcher.toContainEntry;
export const toContainKey = toContainKeyMatcher.toContainKey;
export const toContainKeys = toContainKeysMatcher.toContainKeys;
export const toContainValue = toContainValueMatcher.toContainValue;
export const toContainValues = toContainValuesMatcher.toContainValues;
export const toEndWith = toEndWithMatcher.toEndWith;
export const toEqualCaseInsensitive = toEqualCaseInsensitiveMatcher.toEqualCaseInsensitive;
export const toHaveBeenCalledAfter = toHaveBeenCalledAfterMatcher.toHaveBeenCalledAfter;
export const toHaveBeenCalledBefore = toHaveBeenCalledBeforeMatcher.toHaveBeenCalledBefore;
export const toHaveBeenCalledOnce = toHaveBeenCalledOnceMatcher.toHaveBeenCalledOnce;
export const toInclude = toIncludeMatcher.toInclude;
export const toIncludeAllMembers = toIncludeAllMembersMatcher.toIncludeAllMembers;
export const toIncludeAllPartialMembers = toIncludeAllPartialMembersMatcher.toIncludeAllPartialMembers;
export const toIncludeAnyMembers = toIncludeAnyMembersMatcher.toIncludeAnyMembers;
export const toIncludeMultiple = toIncludeMultipleMatcher.toIncludeMultiple;
export const toIncludeRepeated = toIncludeRepeatedMatcher.toIncludeRepeated;
export const toIncludeSameMembers = toIncludeSameMembersMatcher.toIncludeSameMembers;
export const toReject = toRejectMatcher.toReject;
export const toResolve = toResolveMatcher.toResolve;
export const toSatisfy = toSatisfyMatcher.toSatisfy;
export const toSatisfyAll = toSatisfyAllMatcher.toSatisfyAll;
export const toSatisfyAny = toSatisfyAnyMatcher.toSatisfyAny;
export const toStartWith = toStartWithMatcher.toStartWith;
export const toThrowWithMessage = toThrowWithMessageMatcher.toThrowWithMessage;
export { fail } from './fail';
export { pass } from './pass';
export { toBeAfter } from './toBeAfter';
export { toBeArray } from './toBeArray';
export { toBeArrayOfSize } from './toBeArrayOfSize';
export { toBeBefore } from './toBeBefore';
export { toBeBoolean } from './toBeBoolean';
export { toBeDate } from './toBeDate';
export { toBeDateString } from './toBeDateString';
export { toBeEmpty } from './toBeEmpty';
export { toBeEmptyObject } from './toBeEmptyObject';
export { toBeEven } from './toBeEven';
export { toBeExtensible } from './toBeExtensible';
export { toBeFalse } from './toBeFalse';
export { toBeFinite } from './toBeFinite';
export { toBeFrozen } from './toBeFrozen';
export { toBeFunction } from './toBeFunction';
export { toBeHexadecimal } from './toBeHexadecimal';
export { toBeInteger } from './toBeInteger';
export { toBeNaN } from './toBeNaN';
export { toBeNegative } from './toBeNegative';
export { toBeNil } from './toBeNil';
export { toBeNumber } from './toBeNumber';
export { toBeObject } from './toBeObject';
export { toBeOdd } from './toBeOdd';
export { toBeOneOf } from './toBeOneOf';
export { toBePositive } from './toBePositive';
export { toBeSealed } from './toBeSealed';
export { toBeString } from './toBeString';
export { toBeSymbol } from './toBeSymbol';
export { toBeTrue } from './toBeTrue';
export { toBeValidDate } from './toBeValidDate';
export { toBeWithin } from './toBeWithin';
export { toContainAllEntries } from './toContainAllEntries';
export { toContainAllKeys } from './toContainAllKeys';
export { toContainAllValues } from './toContainAllValues';
export { toContainAnyEntries } from './toContainAnyEntries';
export { toContainAnyKeys } from './toContainAnyKeys';
export { toContainAnyValues } from './toContainAnyValues';
export { toContainEntries } from './toContainEntries';
export { toContainEntry } from './toContainEntry';
export { toContainKey } from './toContainKey';
export { toContainKeys } from './toContainKeys';
export { toContainValue } from './toContainValue';
export { toContainValues } from './toContainValues';
export { toEndWith } from './toEndWith';
export { toEqualCaseInsensitive } from './toEqualCaseInsensitive';
export { toHaveBeenCalledAfter } from './toHaveBeenCalledAfter';
export { toHaveBeenCalledBefore } from './toHaveBeenCalledBefore';
export { toHaveBeenCalledOnce } from './toHaveBeenCalledOnce';
export { toInclude } from './toInclude';
export { toIncludeAllMembers } from './toIncludeAllMembers';
export { toIncludeAllPartialMembers } from './toIncludeAllPartialMembers';
export { toIncludeAnyMembers } from './toIncludeAnyMembers';
export { toIncludeMultiple } from './toIncludeMultiple';
export { toIncludeRepeated } from './toIncludeRepeated';
export { toIncludeSameMembers } from './toIncludeSameMembers';
export { toReject } from './toReject';
export { toResolve } from './toResolve';
export { toSatisfy } from './toSatisfy';
export { toSatisfyAll } from './toSatisfyAll';
export { toSatisfyAny } from './toSatisfyAny';
export { toStartWith } from './toStartWith';
export { toThrowWithMessage } from './toThrowWithMessage';
9 changes: 8 additions & 1 deletion src/matchers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ describe('asymmetric matchers', () => {
describe('all matchers', () => {
test('must be exported', () => {
const directories = fs.readdirSync(__dirname).filter(dir => fs.statSync(path.join(__dirname, dir)).isDirectory());
const namedMatchers = Object.keys(matchers);

expect(Object.keys(matchers)).toHaveLength(directories.length);
try {
expect(namedMatchers).toHaveLength(directories.length);
} catch (error) {
const missing = new Set(directories.filter(dir => !namedMatchers.includes(dir)));
console.error('Missing', missing);
throw error;
}
});

describe('must be functions', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/matchers/pass/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ const passMessage = message => {
else return () => 'passes by .pass() assertion';
};

export default {
pass: (expected, message) => ({ pass: predicate(), message: passMessage(message) }),
};
export function pass(expected, message) {
return { pass: predicate(), message: passMessage(message) };
}
2 changes: 1 addition & 1 deletion src/matchers/pass/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import matcher from './';
import * as matcher from './';

expect.extend(matcher);

Expand Down
16 changes: 7 additions & 9 deletions src/matchers/toBeAfter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ const failMessage = (received, after) => () =>
`Expected date to be after ${printReceived(after)} but received:\n` +
` ${printReceived(received)}`;

export default {
toBeAfter: (date, after) => {
const pass = predicate(date, after);
if (pass) {
return { pass: true, message: passMessage(date, after) };
}
export function toBeAfter(date, after) {
const pass = predicate(date, after);
if (pass) {
return { pass: true, message: passMessage(date, after) };
}

return { pass: false, message: failMessage(date, after) };
},
};
return { pass: false, message: failMessage(date, after) };
}
2 changes: 1 addition & 1 deletion src/matchers/toBeAfter/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import matcher from './';
import * as matcher from './';

expect.extend(matcher);

Expand Down
16 changes: 7 additions & 9 deletions src/matchers/toBeArray/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ const failMessage = received => () =>
'Expected value to be an array received:\n' +
` ${printReceived(received)}`;

export default {
toBeArray: expected => {
const pass = predicate(expected);
if (pass) {
return { pass: true, message: passMessage(expected) };
}
export function toBeArray(expected) {
const pass = predicate(expected);
if (pass) {
return { pass: true, message: passMessage(expected) };
}

return { pass: false, message: failMessage(expected) };
},
};
return { pass: false, message: failMessage(expected) };
}
2 changes: 1 addition & 1 deletion src/matchers/toBeArray/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import matcher from './';
import * as matcher from './';

expect.extend(matcher);

Expand Down
16 changes: 7 additions & 9 deletions src/matchers/toBeArrayOfSize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ Received:
value: ${printReceived(actual)}
length: ${printReceived(determinePropertyMessage(actual, 'length'))}`;

export default {
toBeArrayOfSize: (actual, expected) => {
const pass = predicate(actual, expected);
if (pass) {
return { pass: true, message: passMessage(actual, expected) };
}
export function toBeArrayOfSize(actual, expected) {
const pass = predicate(actual, expected);
if (pass) {
return { pass: true, message: passMessage(actual, expected) };
}

return { pass: false, message: failMessage(actual, expected) };
},
};
return { pass: false, message: failMessage(actual, expected) };
}
2 changes: 1 addition & 1 deletion src/matchers/toBeArrayOfSize/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import matcher from './';
import * as matcher from './';

expect.extend(matcher);

Expand Down
Loading

0 comments on commit 9966215

Please sign in to comment.