【问题标题】:Redis tests don't exit after passing (using jest)Redis 测试通过后不退出(使用玩笑)
【发布时间】:2018-10-27 11:38:16
【问题描述】:

我为一个项目实现了基本的缓存功能,但在测试过程中遇到了问题。 我使用 jest 和 redis-mock 进行测试,所有测试都通过了。 问题是当我导入一个导入 redis 文件的文件时。 测试文件不退出。

例子:

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 通过'...'运行所有匹配的测试套件...'

但是它一直在等待,我认为是因为 redis.createClient() 是异步的或其他的。看到它在导入时发生,我不能解决它。 每次测试后我是否必须关闭重新实例连接?

这里的解决方案/最佳实践是什么?

【问题讨论】:

  • redis.createClient() 打开一个文件句柄,所以 jest 等待该句柄关闭。要知道哪些句柄仍处于打开状态,您可以使用 --detectOpenHandles 标志运行 jest

标签: node.js jestjs node-redis


【解决方案1】:

是的,关闭实例就可以了。

index.test.js

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

redis.js

export function closeInstance(callback) {
  client.quit(callback)
}

【讨论】:

    猜你喜欢
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 2021-02-10
    • 2020-07-31
    • 1970-01-01
    • 2018-01-29
    相关资源
    最近更新 更多