Touching more Jest tests
Tags: opensource
Published: 2022-03-13 04:22:13

## What've I contributed to 2.8
I've had little impact this release. My parser service e2e test works but not perfect, it does test how valid and invalid feeds should be processed when added to the `feedQueue` but this service doesn't shutdown gracefully. There are a few open handles that hangs Jest: 
* [setInterval()](https://github.com/Seneca-CDOT/telescope/blob/66c553d56450a51ba6170ae72aedf9dc2eec601e/src/api/parser/src/utils/indexer.js#L148-L157)

```js
  const timer = new Promise((resolve, reject) => {
    timerId = setTimeout(() => {
      reject(
        new Error(
          'Unable to connect to Elasticsearch. Use `MOCK_ELASTIC=1` in your `.env` to mock Elasticsearch, or install (see https://github.com/Seneca-CDOT/telescope/blob/master/docs/environment-setup.md)'
        )
      );
    }, DELAY);
  });

  const connectivity = new Promise((resolve) => {
    intervalId = setInterval(async () => {
      try {
        await checkConnection();
        resolve();
      } catch (error) {
        logger.info('Attempting to connect to elasticsearch...');
      }
    }, 500);
  });

  await Promise.race([timer, connectivity]);

  await clearInterval(intervalId);
  clearTimeout(timerId);
```
The result of `Promise.race` if all things are well should be a resolved Promise from `connectivity`, after which is cleared by `clearInterval`, somehow Jest still detects as open, this could be related to JavaScript event queue. I also tried using `unref()` following [Tim Perry's blog post](https://httptoolkit.tech/blog/unblocking-node-with-unref/#timeoutunref) but to a avail. 

* `logger`.
 
Thanks to [David's PR](https://github.com/Seneca-CDOT/telescope/pull/3178) turning logger off during test, this problem went away

* `Redis` open connections

Some `Redis` connections are still open after Jest finishes all tests, this was straightforward enough, I tried adding some code that keeps track of all open connections and close them after use and it worked but the logic already exist in Satellite, therefore, I wanted to reuse that code. Thanks to Francesco's work to merge Satellite to Telescope, adding anything to Satellite has become easier. 

## Modifying Satellite

I opened a [PR](https://github.com/Seneca-CDOT/telescope/pull/3162/files) to modify the `stop()` from `Satellite` class to close all database connections (ElasticSearch & Redis). However, I struggled more than I thought. 

First of all I changed `stop()` to return an array of Promises, I used [`promisify`](https://nodejs.org/dist/latest-v8.x/docs/api/util.html) to make `server.close()` return a Promise but it lost access to `this.server` in Satellite so I had to bind it with `this.server` instance. 

```js
    const { server } = this;

    const promisifiedClose = server
      ? promisify(server.close.bind(server))
      : () => Promise.resolve();
```

One of my mistakes was not to pay attention to this piece of code 
```js
afterEach(() => {
  service.stop()
});
```
Yes, the brackets are not needed, I should only return the Promise for Jest to wait. 

However, there's still problems with the service not shutting properly after the Satellite's change 😕