Skip to content

Commit

Permalink
Prints the Symbol name into the error message with a custom asymmetri…
Browse files Browse the repository at this point in the history
…c matcher

Fixes #7534
  • Loading branch information
ghostd committed Apr 26, 2020
1 parent c024dec commit 953203e
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 163 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

### Fixes

- `[expect]` Prints the Symbol name into the error message with a custom asymmetric matcher ([#9888](https://github.com/facebook/jest/pull/9888))
- `[@jest/environment]` Make sure not to reference Jest types ([#9875](https://github.com/facebook/jest/pull/9875))
- `[jest-message-util]` Code frame printing should respect `--noStackTrace` flag ([#9866](https://github.com/facebook/jest/pull/9866))
- `[jest-runtime]` Support importing CJS from ESM using `import` statements ([#9850](https://github.com/facebook/jest/pull/9850))
Expand Down
42 changes: 0 additions & 42 deletions examples/mongodb/__test__/db.test.js

This file was deleted.

4 changes: 0 additions & 4 deletions examples/mongodb/babel.config.js

This file was deleted.

15 changes: 0 additions & 15 deletions examples/mongodb/jest.config.js

This file was deleted.

37 changes: 0 additions & 37 deletions examples/mongodb/mongo-environment.js

This file was deleted.

21 changes: 0 additions & 21 deletions examples/mongodb/package.json

This file was deleted.

29 changes: 0 additions & 29 deletions examples/mongodb/setup.js

This file was deleted.

12 changes: 0 additions & 12 deletions examples/mongodb/teardown.js

This file was deleted.

3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ module.exports = {
'website/.*',
'e2e/runtime-internal-module-registry/__mocks__',
],
projects: ['<rootDir>', '<rootDir>/examples/*/'],
projects: ['<rootDir>'],
setupFilesAfterEnv: ['<rootDir>/testSetupFile.js'],
snapshotSerializers: [
'<rootDir>/packages/pretty-format/build/plugins/ConvertAnsi.js',
require.resolve('jest-snapshot-serializer-raw'),
],
testEnvironment: './packages/jest-environment-node',
testMatch: ['**/expect/src/__tests__/**/*.[jt]s'],
testPathIgnorePatterns: [
'/__arbitraries__/',
'/node_modules/',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
"test-ci-partial": "yarn jest --color -i --config jest.config.ci.js",
"test-pretty-format-perf": "node packages/pretty-format/perf/test.js",
"test-leak": "yarn jest -i --detectLeaks jest-mock jest-diff jest-repl",
"test": "yarn lint && yarn jest",
"test": "yarn jest",
"verify-old-ts": "node ./scripts/verifyOldTs.js",
"watch": "yarn build && node ./scripts/watch.js",
"watch:ts": "yarn build:ts --watch"
Expand Down
12 changes: 12 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/extend.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,15 @@ exports[`is available globally when matcher is unary 1`] = `expected 15 to be di
exports[`is available globally when matcher is variadic 1`] = `expected 15 to be within range 1 - 3`;
exports[`is ok if there is no message specified 1`] = `<r>No message was specified for this matcher.</>`;
exports[`prints the Symbol into the error message 1`] = `
<d>expect(</><r>received</><d>).</>toEqual<d>(</><g>expected</><d>) // deep equality</>
<g>- Expected - 1</>
<r>+ Received + 1</>
<d> Object {</>
<g>- "a": toBeSymbol<Symbol(bar)>,</>
<r>+ "a": Symbol(foo),</>
<d> }</>
`;
17 changes: 17 additions & 0 deletions packages/expect/src/__tests__/extend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ jestExpect.extend({

return {message, pass};
},
toBeSymbol(actual, expected) {
const pass = actual === expected;
const message = () => `expected ${actual} to be Symbol ${expected}`;

return {message, pass};
},
toBeWithinRange(actual, floor, ceiling) {
const pass = actual >= floor && actual <= ceiling;
const message = pass
Expand Down Expand Up @@ -137,3 +143,14 @@ it('defines asymmetric variadic matchers that can be prefixed by not', () => {
}),
).not.toThrow();
});

it('prints the Symbol into the error message', () => {
const foo = Symbol('foo');
const bar = Symbol('bar');

expect(() =>
jestExpect({a: foo}).toEqual({
a: jestExpect.toBeSymbol(bar),
}),
).toThrowErrorMatchingSnapshot();
});
2 changes: 1 addition & 1 deletion packages/expect/src/jestMatchersObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const setMatchers = (
}

toAsymmetricMatcher() {
return `${this.toString()}<${this.sample.join(', ')}>`;
return `${this.toString()}<${this.sample.map(String).join(', ')}>`;
}
}

Expand Down

0 comments on commit 953203e

Please sign in to comment.