question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sequelize errors don't print properly in jest

See original GitHub issue

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don’t fill in the requested information
  • I have read the contribution guidelines

Bug Description

When using sequelize in jest, errors don’t print their message, which makes debugging difficult. You get a trace back into the query interface in sequelize, but you have to set breakpoints in sequelize in order to determine the nature of the error (syntax error, foreign key constraint issue, etc.).

I have verified that other thrown errors print properly, so it seems to be an issue with these in particular in Sequelize.

Reproducible Example

it("passes", async () => {
  await db.query("SELECT 1 )").
});

In this example, let’s assume I want to write a test that selects 1 and passes. Assuming db is a Sequelize instance pointing at a postgres db, this test that we want to be successful fails due to a syntax error. In the console, I get a trace, but the error message is missing.

What do you expect to happen?

The error message prints indicating the sql syntax error. The full printout should look someting like:

DatabaseError: syntax error at or near ")"

      at Query.run (../node_modules/sequelize/src/dialects/postgres/query.js:76:25)
      at ../node_modules/sequelize/src/sequelize.js:641:28
      at PostgresQueryInterface.select (../node_modules/sequelize/src/dialects/abstract/query-interface.js:1001:12)
      at … remaining trace back to test

What is actually happening?

The error is printed, but only the stack trace appears, which will look something like this (note that message is omitted):

      at Query.run (../node_modules/sequelize/src/dialects/postgres/query.js:76:25)
      at ../node_modules/sequelize/src/sequelize.js:641:28
      at PostgresQueryInterface.select (../node_modules/sequelize/src/dialects/abstract/query-interface.js:1001:12)
      at … remaining trace back to test

Environment

  • Sequelize version: 6.21.3
  • Node.js version: 16.15.0
  • If TypeScript related: TypeScript version:
  • Database & Version: Postgres 11.x
  • Connector library & Version: pg@8.7.3

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I will need guidance.
  • No, I don’t have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don’t have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as “+1” will be removed.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:19
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ephyscommented, Nov 28, 2022

We should fix this issue in all environments, it’s a problem in mocha and other tools that only print the .stack property

Following #13347, the stack went from

SequelizeDatabaseError: Invalid column name 'unknown_column'.
    at Query.formatError (/Users/harry/code/sequelize/lib/dialects/mssql/query.js:314:12)
    at Query._run (/Users/harry/code/sequelize/lib/dialects/mssql/query.js:93:18)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

To

Error
    at Query.run (/Users/harry/code/sequelize/lib/dialects/mssql/query.js:119:25)
    at /Users/harry/code/sequelize/lib/sequelize.js:619:28
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Context.<anonymous> (/Users/harry/code/sequelize/test/integration/sequelize/query.test.js:319:11)

But it should have become this instead:

SequelizeDatabaseError: Invalid column name 'unknown_column'.
    at Query.run (/Users/harry/code/sequelize/lib/dialects/mssql/query.js:119:25)
    at /Users/harry/code/sequelize/lib/sequelize.js:619:28
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Context.<anonymous> (/Users/harry/code/sequelize/test/integration/sequelize/query.test.js:319:11)

It should merge both stacks (keeping the error message but replacing the trace) instead of fully replacing it.

0reactions
plutowniumcommented, Dec 27, 2022

What are other programmers doing to debug errors while Jest isn’t reporting them correctly? My portfolio project is held up due to this bug … need my portfolio project done fast

edit: I discovered that I could get the truncated error message to print in full by surrounding my Sequelize code with

    public getAllBatchNums = async () => {
        try {
            const batches = await Batch.findAll({});
            const justNums: number[] = batches.map(b => b.batchId);
            return justNums;
        } catch (err) {
            console.log(err);
            throw err;
        }
    };

Had to do it in like 40 places to find the right one but, I got it! Hope this helps someone

Read more comments on GitHub >

github_iconTop Results From Across the Web

When SQL errors occur, add 'message 'to DEBUG log ... - GitHub
When invoking Sequelize methods that create errors, the 'message' attribute of the ... Sequelize errors don't print properly in jest #14807.
Read more >
How to refactor my ExpressJS + Sequelize files so I can use ...
Once I moved that, running Jest only has Jest output now, with no errors. index.js- import { app, port, whichServer } from './server';...
Read more >
Troubleshooting - Jest
Troubleshooting. Uh oh, something went wrong? Use this guide to resolve issues with Jest. Tests are Failing and You Don't Know Why​.
Read more >
sequelize execute a query which would set an environment or ...
When using sequelize in jest, errors don't print their message, which makes debugging difficult. You get a trace back into the query interface...
Read more >
How To Build and Deploy a Node.js Application To ...
In this tutorial, you'll build an address book API service with Node.js and use Semaphore to test, build, and deploy it to DigitalOcean ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found