{"record":{"id":"7bb87517a9c18dc0","repo":"Automattic/mongoose","slug":"invalid-arg-arg-to-sortbycount-must-be-str","errorCode":null,"errorMessage":"Invalid arg \"${arg}\" to sortByCount(), must be string or object","messagePattern":"Invalid arg \"(.+?)\" to sortByCount\\(\\), must be string or object","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"warning","filePath":"lib/aggregate.js","lineNumber":553,"sourceCode":" *\n *     aggregate.sortByCount('users');\n *     aggregate.sortByCount({ $mergeObjects: [ \"$employee\", \"$business\" ] })\n *\n * @see $sortByCount https://www.mongodb.com/docs/manual/reference/operator/aggregation/sortByCount/\n * @param {object|string} arg\n * @return {Aggregate} this\n * @api public\n */\n\nAggregate.prototype.sortByCount = function(arg) {\n  if (arg && typeof arg === 'object') {\n    return this.append({ $sortByCount: arg });\n  } else if (typeof arg === 'string') {\n    return this.append({\n      $sortByCount: (arg[0] === '$') ? arg : '$' + arg\n    });\n  } else {\n    throw new TypeError('Invalid arg \"' + arg + '\" to sortByCount(), ' +\n      'must be string or object');\n  }\n};\n\n/**\n * Appends new custom $lookup operator to this aggregate pipeline.\n *\n * #### Example:\n *\n *     aggregate.lookup({ from: 'users', localField: 'userId', foreignField: '_id', as: 'users' });\n *\n * @see $lookup https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/#pipe._S_lookup\n * @param {object} options to $lookup as described in the above link\n * @return {Aggregate}\n * @api public\n */\n\nAggregate.prototype.lookup = function(options) {","sourceCodeStart":535,"sourceCodeEnd":571,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/aggregate.js#L535-L571","documentation":"This module-level warning prints once when Mongoose is first required inside a Jest run that uses the default jsdom test environment. jsdom provides a browser-like global `window`, under which the MongoDB driver behaves differently (and historically failed on missing Node APIs), so Mongoose nudges you to run Node-style tests in the node environment. It is purely informational — gated on typeof jest !== 'undefined' && typeof window !== 'undefined' — and is hidden by SUPPRESS_JEST_WARNINGS.","triggerScenarios":"Requiring mongoose in any test file while Jest's testEnvironment is 'jsdom' (the default in jest-environment-jsdom setups and in older Jest versions); integration tests that spin up a real connection inside component tests configured for the browser environment.","commonSituations":"Frontend repos that added API tests without changing jest.config; Create React App / Next.js default configs that use jsdom for everything; CI logs polluted by the warning after adding a database test suite; upgrading Jest where jsdom stopped being the bundled default and configs were pinned to it.","solutions":["Set the Node environment for backend tests: in jest.config.js use testEnvironment: 'node'.","Per-file override when a mixed suite exists: add the docblock /** @jest-environment node */ at the top of the DB test file.","If some tests genuinely need jsdom, split projects with Jest's projects option: one node project for DB tests, one jsdom for UI.","As a last resort set SUPPRESS_JEST_WARNINGS=true in the env — but fixing the environment is the real fix."],"exampleFix":"// before (jest.config.js)\nmodule.exports = { testEnvironment: 'jsdom' };\n\n// after\nmodule.exports = { testEnvironment: 'node' };\n\n// or per file, first line of test:\n/** @jest-environment node */","handlingStrategy":"validation","validationCode":"// jest.config.js — fail fast on wrong environment for DB suites\nmodule.exports = {\n  projects: [\n    { displayName: 'unit', testEnvironment: 'jsdom', testMatch: ['<rootDir>/src/**/*.test.js'] },\n    { displayName: 'api', testEnvironment: 'node', testMatch: ['<rootDir>/test/**/*.test.js'] }\n  ]\n};","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set testEnvironment: 'node' for any suite that requires mongoose.","Add /** @jest-environment node */ docblocks to individual DB test files in mixed suites.","Keep server and UI tests in separate Jest projects so environments never leak."],"tags":["mongoose","jest","jsdom","test-environment","warning"],"backgroundTag":"jest-environment-mismatch","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}