'Fastify test unit keeps getting timed out (Node Tap)

I am testing a NodeJS app on Fastify , using Node Tap. I'm new to both platforms. The test case unit is supposed to connect to graphql and run a sample query. I can see that the query is executed and the test assertions being passed successfully :

Executing (default): SELECT `id`, `title`, `type`, `description`, `published`,` FROM `articles` AS `Article` WHERE `Article`.`section_id` IS NOT NULL AND `Article`.`date_published` <= NOW() AND ORDER BY `Article`.`TITLE` ASC,LIMIT 5;

However, the test case keeps taking 30 seconds and eventually getting timed out. I

FAIL  test/fastify-tests.js
 ✖ timeout!

  test: TAP
  signal: SIGTERM
  handles:
    - type: Socket
      events:
        - end
        - data
        - error
        - clientError
        - close
        - drain
  expired: TAP

I also noticed it adds an extra assertion to the number of tests. For example , in the case where I have only one assertion , it states it is testing two :

Asserts:  1 failed, 1 passed, of 2

My test case :

const tap = require('tap');
const fastify = require('../fastifyapp');

tap.test('POST  to `/graphql` route', t => {
t.plan(1); //plan for 1 assertion
const app = fastify.FastifyApp();
app.inject({
    method: 'POST',
    url: 'http://localhost/graphql',
    payload: {
        "operationName:": null,
        "query": '{\n' +
            'article(limit: 5) {\n' +
            'id\n' +
            '}\n' +
            '}\n',
    "variables": {
        "visibility": "PUBLIC",
        "articletype": "SPORTS",
        "OFFSET": 0,
        "articleSortBy": "ID",
        "articleSort": "ASC",
        }
    }
}, (err, response) => {
    t.error(err);  // assertion 1
});
});

Would love it if someone could help me understand why :

  • it times out every time
  • it adds an extra assertion to the list of tests being taken in


Solution 1:[1]

Add t.end() after your t.error() assertion

Solution 2:[2]

You need to close your connection to the database.


1. By Make use of fastify hooks:

I have an example with mongoose and you can search postgresql, mysql, or whatever db you're connecting to:

fastify
      .decorate('mongoose', db)
      .addHook('onClose', (fastify, done) => {
        fastify.mongoose.connection.close();
      });

2. By separate the database connection:

you can provide the database instance as an option. and define .close() callback function to call every time you make tests.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 maxwondercorn
Solution 2 oussama boumaad