Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis tests don't exit after passing (using jest)

I implemented basic caching functionality for a project and ran into a problem during the testing. I test using jest and redis-mock and all the tests pass. The problem is when I import a file which imports the redis-file. The test-file doesn't exit.

Example:

index.test.js

import redis from 'redis'
import redis_mock from 'redis-mock'
jest.spyOn(redis, 'createClient').mockImplementation(red_mock.createClient)
import fileUsingRedis from './index'

describe('index', () => {
  it('should pass', () => expect(true).toBeTruthy())
}

index.js

import {set} from './redis'
export default function ...

redis.js

import redis from 'redis'
const client = redis.createClient()

export function set(key, value) {...}

'1 passed'...'Ran all test suites matching ...'

But then it keeps waiting, I assume because the redis.createClient() is async or something or other. Seeing as it happens on the import I can't just resolve it. Do I have to close the redis-instance connection after every test?

What is the solution/best-practice here?

like image 835
Lennert Hofman Avatar asked May 17 '18 09:05

Lennert Hofman


1 Answers

So yeah, closing the instance did it.

index.test.js

import redis from './redis'
...
afterAll(() => redis.closeInstance())

redis.js

export function closeInstance(callback) {
  client.quit(callback)
}
like image 155
Lennert Hofman Avatar answered Oct 19 '22 17:10

Lennert Hofman