From 953203e7069570081d4383f041d940e81d2a8ca1 Mon Sep 17 00:00:00 2001 From: Vincent Ricard Date: Sun, 26 Apr 2020 17:48:16 +0200 Subject: [PATCH] Prints the Symbol name into the error message with a custom asymmetric matcher Fixes #7534 --- CHANGELOG.md | 1 + examples/mongodb/__test__/db.test.js | 42 ------------------- examples/mongodb/babel.config.js | 4 -- examples/mongodb/jest.config.js | 15 ------- examples/mongodb/mongo-environment.js | 37 ---------------- examples/mongodb/package.json | 21 ---------- examples/mongodb/setup.js | 29 ------------- examples/mongodb/teardown.js | 12 ------ jest.config.js | 3 +- package.json | 2 +- .../__snapshots__/extend.test.js.snap | 12 ++++++ packages/expect/src/__tests__/extend.test.js | 17 ++++++++ packages/expect/src/jestMatchersObject.ts | 2 +- 13 files changed, 34 insertions(+), 163 deletions(-) delete mode 100644 examples/mongodb/__test__/db.test.js delete mode 100644 examples/mongodb/babel.config.js delete mode 100644 examples/mongodb/jest.config.js delete mode 100644 examples/mongodb/mongo-environment.js delete mode 100644 examples/mongodb/package.json delete mode 100644 examples/mongodb/setup.js delete mode 100644 examples/mongodb/teardown.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 29ccfffca4ae..8c4ff4225682 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/examples/mongodb/__test__/db.test.js b/examples/mongodb/__test__/db.test.js deleted file mode 100644 index 9b6e4cc6201e..000000000000 --- a/examples/mongodb/__test__/db.test.js +++ /dev/null @@ -1,42 +0,0 @@ -const {MongoClient} = require('mongodb'); - -let connection; -let db; - -beforeAll(async () => { - connection = await MongoClient.connect(global.__MONGO_URI__, { - useNewUrlParser: true, - useUnifiedTopology: true, - }); - db = await connection.db(global.__MONGO_DB_NAME__); -}); - -afterAll(async () => { - await connection.close(); -}); - -it('should aggregate docs from collection', async () => { - const files = db.collection('files'); - - await files.insertMany([ - {type: 'Document'}, - {type: 'Video'}, - {type: 'Image'}, - {type: 'Document'}, - {type: 'Image'}, - {type: 'Document'}, - ]); - - const topFiles = await files - .aggregate([ - {$group: {_id: '$type', count: {$sum: 1}}}, - {$sort: {count: -1}}, - ]) - .toArray(); - - expect(topFiles).toEqual([ - {_id: 'Document', count: 3}, - {_id: 'Image', count: 2}, - {_id: 'Video', count: 1}, - ]); -}); diff --git a/examples/mongodb/babel.config.js b/examples/mongodb/babel.config.js deleted file mode 100644 index ca27cf6fd2e1..000000000000 --- a/examples/mongodb/babel.config.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = { - // See https://babeljs.io/docs/en/babel-preset-env#targets - presets: [['@babel/preset-env', {targets: {node: 'current'}}]], -}; diff --git a/examples/mongodb/jest.config.js b/examples/mongodb/jest.config.js deleted file mode 100644 index 1ec6b095197b..000000000000 --- a/examples/mongodb/jest.config.js +++ /dev/null @@ -1,15 +0,0 @@ -// For a detailed explanation regarding each configuration property, visit: -// https://jestjs.io/docs/en/configuration.html - -module.exports = { - // A path to a module which exports an async function that is triggered once before all test suites - globalSetup: './setup.js', - - // A path to a module which exports an async function that is triggered once after all test suites - globalTeardown: './teardown.js', - - // The test environment that will be used for testing - testEnvironment: './mongo-environment.js', - - transformIgnorePatterns: ['/node_modules/', '/packages/'], -}; diff --git a/examples/mongodb/mongo-environment.js b/examples/mongodb/mongo-environment.js deleted file mode 100644 index 849f9f804b97..000000000000 --- a/examples/mongodb/mongo-environment.js +++ /dev/null @@ -1,37 +0,0 @@ -// mongo-environment.js -const NodeEnvironment = require('jest-environment-node'); - -const path = require('path'); - -const fs = require('fs'); - -const globalConfigPath = path.join(__dirname, 'globalConfig.json'); - -class MongoEnvironment extends NodeEnvironment { - constructor(config) { - super(config); - } - - async setup() { - console.log('Setup MongoDB Test Environment'); - - const globalConfig = JSON.parse(fs.readFileSync(globalConfigPath, 'utf-8')); - - this.global.__MONGO_URI__ = globalConfig.mongoUri; - this.global.__MONGO_DB_NAME__ = globalConfig.mongoDBName; - - await super.setup(); - } - - async teardown() { - console.log('Teardown MongoDB Test Environment'); - - await super.teardown(); - } - - runScript(script) { - return super.runScript(script); - } -} - -module.exports = MongoEnvironment; diff --git a/examples/mongodb/package.json b/examples/mongodb/package.json deleted file mode 100644 index b9d49fb18970..000000000000 --- a/examples/mongodb/package.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "example-mongodb", - "version": "0.0.0", - "main": "index.js", - "license": "MIT", - "private": true, - "dependencies": { - "jest-environment-node": "*", - "mongodb": "^3.1.13", - "mongodb-memory-server": "^6.3.2" - }, - "devDependencies": { - "@babel/core": "*", - "@babel/preset-env": "*", - "babel-jest": "*", - "jest": "*" - }, - "scripts": { - "test": "jest" - } -} diff --git a/examples/mongodb/setup.js b/examples/mongodb/setup.js deleted file mode 100644 index 17e5159cc1e8..000000000000 --- a/examples/mongodb/setup.js +++ /dev/null @@ -1,29 +0,0 @@ -// setup.js -const path = require('path'); - -const fs = require('fs'); - -const {MongoMemoryServer} = require('mongodb-memory-server'); - -const globalConfigPath = path.join(__dirname, 'globalConfig.json'); - -const mongod = new MongoMemoryServer({ - autoStart: false, -}); - -module.exports = async () => { - if (!mongod.isRunning) { - await mongod.start(); - } - - const mongoConfig = { - mongoDBName: 'jest', - mongoUri: await mongod.getConnectionString(), - }; - - // Write global config to disk because all tests run in different contexts. - fs.writeFileSync(globalConfigPath, JSON.stringify(mongoConfig)); - - // Set reference to mongod in order to close the server during teardown. - global.__MONGOD__ = mongod; -}; diff --git a/examples/mongodb/teardown.js b/examples/mongodb/teardown.js deleted file mode 100644 index ab260a66005a..000000000000 --- a/examples/mongodb/teardown.js +++ /dev/null @@ -1,12 +0,0 @@ -// teardown.js -const path = require('path'); - -const fs = require('fs'); - -const globalConfigPath = path.join(__dirname, 'globalConfig.json'); - -module.exports = async function () { - await global.__MONGOD__.stop(); - - fs.unlinkSync(globalConfigPath); -}; diff --git a/jest.config.js b/jest.config.js index cb189da42604..7b623881c826 100644 --- a/jest.config.js +++ b/jest.config.js @@ -28,13 +28,14 @@ module.exports = { 'website/.*', 'e2e/runtime-internal-module-registry/__mocks__', ], - projects: ['', '/examples/*/'], + projects: [''], setupFilesAfterEnv: ['/testSetupFile.js'], snapshotSerializers: [ '/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/', diff --git a/package.json b/package.json index 8658d7699537..0851e93a2a53 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/packages/expect/src/__tests__/__snapshots__/extend.test.js.snap b/packages/expect/src/__tests__/__snapshots__/extend.test.js.snap index b1b67afefea8..68129f9c16d7 100644 --- a/packages/expect/src/__tests__/__snapshots__/extend.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/extend.test.js.snap @@ -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`] = `No message was specified for this matcher.`; + +exports[`prints the Symbol into the error message 1`] = ` +expect(received).toEqual(expected) // deep equality + +- Expected - 1 ++ Received + 1 + + Object { +- "a": toBeSymbol, ++ "a": Symbol(foo), + } +`; diff --git a/packages/expect/src/__tests__/extend.test.js b/packages/expect/src/__tests__/extend.test.js index 7861eafe5852..11b915332fee 100644 --- a/packages/expect/src/__tests__/extend.test.js +++ b/packages/expect/src/__tests__/extend.test.js @@ -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 @@ -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(); +}); diff --git a/packages/expect/src/jestMatchersObject.ts b/packages/expect/src/jestMatchersObject.ts index bf08dca6db08..24b2c087071c 100644 --- a/packages/expect/src/jestMatchersObject.ts +++ b/packages/expect/src/jestMatchersObject.ts @@ -78,7 +78,7 @@ export const setMatchers = ( } toAsymmetricMatcher() { - return `${this.toString()}<${this.sample.join(', ')}>`; + return `${this.toString()}<${this.sample.map(String).join(', ')}>`; } }