Skip to content

Instantly share code, notes, and snippets.

@mltsy
Last active March 3, 2026 17:07
Show Gist options
  • Select an option

  • Save mltsy/629d2b5b703f14e3e97eb8396518cfad to your computer and use it in GitHub Desktop.

Select an option

Save mltsy/629d2b5b703f14e3e97eb8396518cfad to your computer and use it in GitHub Desktop.
Monkey Patch for the unfriendly errors thrown by Sequelize 6
/**
* This monkey patch fixes an error reporting issue with sequelize errors in tests,
* which was fixed in Sequelize v7, but hasn't been back-ported to Sequelize v6.
* See https://github.com/sequelize/sequelize/issues/14807#issuecomment-1854398131
*
* It should only be used in the test env, to make Sequelize error messages show up.
* This can be removed when we upgrade to Sequelize 7, or when the issue is fixed in v6.
*/
const { Sequelize } = require('sequelize')
const sequelizeVersion = Sequelize['version'] || '6.x.x' // doesn't (currently) exist in v6
const major = Number(sequelizeVersion.split('.')[0])
if (major >= 7) {
console.warn(
'This patch was probably made redundant in Sequelize v7, you should check!'
)
}
/**
* @param {Sequelize} instance An instance of Sequelize
* @returns {Sequelize} Monkey-patched Sequelize instance that throws Jest-compatible errors
*/
function monkeyPatchSequelizeErrorsForJest(instance) {
if (typeof jest === 'undefined') return instance
const origQueryFunc = instance.query
instance.query = async function query(...args) {
let result
try {
result = await origQueryFunc.apply(this, args)
} catch (err) {
throw fixSequelizeError(err)
}
return result
}
return instance
}
const isSequelizeError = e =>
e instanceof Error && e.name.startsWith('Sequelize')
const fixSequelizeError = error => {
if (!isSequelizeError(error)) return error
let { message } = error.parent
if (error.sql) {
message += '\nSQL: ' + error.sql
}
if (error.parameters) {
const stringifiedParameters = JSON.stringify(error.parameters)
if (
stringifiedParameters !== 'undefined' &&
stringifiedParameters !== '{}'
) {
message += '\nParameters: ' + stringifiedParameters
}
}
message += '\n' + error.stack
error.message = message
Error.captureStackTrace(error)
return error
}
module.exports = monkeyPatchSequelizeErrorsForJest
@dyllandry
Copy link

Thanks for writing this! Really makes me happy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment