Skip to content

Commit

Permalink
Add toBeSymbol matcher (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel authored Oct 11, 2021
1 parent c4961cd commit adb82b8
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin
- [.toInclude(substring)](#toincludesubstring)
- [.toIncludeRepeated(substring, times)](#toincluderepeatedsubstring-times)
- [.toIncludeMultiple([substring])](#toincludemultiplesubstring)
- [Symbol](#symbol)
- [.toBeSymbol()](#tobesymbol)
- [LICENSE](#license)

## Installation
Expand Down Expand Up @@ -1003,6 +1005,19 @@ test('passes when value includes all substrings', () => {
});
```
### Symbol
#### .toBeSymbol()
Use `.toBeSymbol` when checking if a value is a `Symbol`.
```js
test('passes when value is a symbol', () => {
expect(Symbol()).toBeSymbol();
expect(true).not.toBeSymbol();
});
```
## LICENSE
[MIT](/LICENSE)
2 changes: 2 additions & 0 deletions src/matchers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ 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';
Expand Down Expand Up @@ -91,6 +92,7 @@ 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;
Expand Down
15 changes: 15 additions & 0 deletions src/matchers/toBeSymbol/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toBeSymbol fails when given a symbol 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBeSymbol()</>
Expected value to not be a symbol, received:
<red>Symbol()</>"
`;
exports[`.toBeSymbol fails when not given a symbol 1`] = `
"<dim>expect(</><red>received</><dim>).toBeSymbol()</>
Expected to receive a symbol, received:
<red>false</>"
`;
26 changes: 26 additions & 0 deletions src/matchers/toBeSymbol/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { matcherHint, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = received => () =>
matcherHint('.not.toBeSymbol', 'received', '') +
'\n\n' +
'Expected value to not be a symbol, received:\n' +
` ${printReceived(received)}`;

const failMessage = received => () =>
matcherHint('.toBeSymbol', 'received', '') +
'\n\n' +
'Expected to receive a symbol, received:\n' +
` ${printReceived(received)}`;

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

return { pass: false, message: failMessage(expected) };
},
};
26 changes: 26 additions & 0 deletions src/matchers/toBeSymbol/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import matcher from './';

expect.extend(matcher);

describe('.toBeSymbol', () => {
test('passes when given a symbol', () => {
expect(Symbol()).toBeSymbol();
});

test('fails when not given a symbol', () => {
expect(() => expect(false).toBeSymbol()).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toBeSymbol', () => {
test.each([[false], [''], [0], [{}], [[]], [undefined], [null], [NaN], [() => {}]])(
'passes when not given a symbol: %s',
given => {
expect(given).not.toBeSymbol();
},
);

test('fails when given a symbol', () => {
expect(() => expect(Symbol()).not.toBeSymbol()).toThrowErrorMatchingSnapshot();
});
});
1 change: 1 addition & 0 deletions src/matchers/toBeSymbol/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default expected => typeof expected === 'symbol';
14 changes: 14 additions & 0 deletions src/matchers/toBeSymbol/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import predicate from './predicate';

describe('toBeSymbol Predicate', () => {
test('returns true when given a symbol', () => {
expect(predicate(Symbol())).toBe(true);
});

test.each([[false], [''], [0], [{}], [[]], [undefined], [null], [NaN], [() => {}]])(
'returns false when given: %s',
given => {
expect(predicate(given)).toBe(false);
},
);
});
12 changes: 11 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ declare namespace jest {
* @param {String | RegExp} message
*/
toThrowWithMessage(type: Function, message: string | RegExp): R;

/**
* Use `.toBeSymbol` when checking if a value is a `Symbol`.
*/
toBeSymbol(): R;
}

// noinspection JSUnusedGlobalSymbols
Expand Down Expand Up @@ -746,6 +751,11 @@ declare namespace jest {
/**
* Use `.toBeEmptyObject` when checking if a value is an empty `Object`.
*/
toBeEmptyObject(): R;
toBeEmptyObject(): any;

/**
* Use `.toBeSymbol` when checking if a value is a `Symbol`.
*/
toBeSymbol(): any;
}
}

0 comments on commit adb82b8

Please sign in to comment.