英文:
Showing this detection message => Jest has detected the following 1 open handle potentially keeping Jest from exiting
问题
Here is the translated content you requested:
我正在尝试使用jest测试我的项目中的一些功能。为此,我导入了一个名为`displayVouchers`的函数,该函数应返回一个字符串,它来自模块`'../Buy Raffle Voucher/index'`。现在,这个`displayVouchers`函数在任何情况下都不与redis通信,尽管`'../Buy Raffle Voucher/index'`模块导入了一个来自模块('./selectedVoucherHandlers')的函数,该模块具有redis连接。在我的测试文件`buyRaffleVoucher.test.js`文件中,无论测试是否通过或失败,一旦测试运行结束,我总是收到消息`Jest检测到以下1个打开的句柄,可能会阻止Jest退出`。我使用`--detectOpenHandles`标志运行我的测试。
# 这是测试运行后我收到的完整消息
Jest检测到以下1个打开的句柄,可能会阻止Jest退出:
● 超时
1 | const Redis = require('ioredis');
2 |
> 3 | const redis = new Redis();
| ^
4 |
5 | redis.on('connect', () => {
6 | console.log('Redis connection successful');
at node_modules/ioredis/built/connectors/StandaloneConnector.js:44:21
at StandaloneConnector.connect (node_modules/ioredis/built/connectors/StandaloneConnector.js:43:16)
at node_modules/ioredis/built/Redis.js:121:64
at EventEmitter.connect (node_modules/ioredis/built/Redis.js:103:25)
at new Redis (node_modules/ioredis/built/Redis.js:77:18)
at Object.<anonymous> (app/config/redis.js:3:15)
at Object.require (app/Buy Raffle Voucher/selectedVoucherHandlers.js:2:15)
at Object.require (app/Buy Raffle Voucher/index.js:1:36)
at Object.require (app/tests/buyRaffleVoucher.test.js:1:29)
# 这是我的buyRaffleVoucher.test.js文件
```javascript
const { displayVouchers } = require('../Buy Raffle Voucher/index');
describe("所有购买夺宝券都在这里", () => {
it('应该返回可用的券', () => {
expect(displayVouchers()).toBe("选择券的金额\n1. N200\n2. N500\n3. N1000");
});
});
我的redis连接文件
const Redis = require('ioredis');
const redis = new Redis();
redis.on('connect', () => {
console.log('Redis连接成功');
});
// redis.on('error', () => {
// console.log('Redis连接失败');
// })
module.exports = redis;
我将redis连接导入到我的测试文件,然后在afterAll()
钩子中调用了redis.disconnect()
,这样可以正常工作。但是,我还有其他需要与redis通信的函数要测试,我必须模拟redis以测试这些函数,但一旦我模拟了redis,redis.disconnect()
就不再起作用了。
在我模拟redis之后的测试文件
const { displayVouchers } = require('../Buy Raffle Voucher/index');
const { onAmountSelected } = require('../Buy Raffle Voucher/selectedVoucherHandlers');
let redis = require('../config/redis');
afterAll(() => {
redis.disconnect();
})
jest.mock('../config/redis');
describe("所有购买夺宝券都在这里", () => {
it('应该返回可用的券', () => {
expect(displayVouchers()).toBe("选择券的金额\n1. N200\n2. N500\n3. N1000");
});
it("应返回接收者类型", async () => {
const result = await onAmountSelected(['0', '1'], 'dasdjl')
redis.hset.mockResolvedValueOnce(1);
expect(redis.hset).toHaveBeenCalled();
expect(result).toBe('CON券适用于\n1 自己\n2 其他人');
});
});
请注意,此翻译中的代码部分未被翻译。
<details>
<summary>英文:</summary>
I'm trying to test a few functions in my project with jest. And for that, I imported one of the functions `displayVouchers` that should returns a string, from the module `'../Buy Raffle Voucher/index'`. Now, this `displayVouchers` function does not communicate with redis in any way whatsoever, although the `'../Buy Raffle Voucher/index'` module imports a function from a module (`./selectedVoucherHandlers`) that has a redis connection. In my test file `buyRaffleVoucher.test.js` file, as soon the test finishes running, regardless of whether the test passes or fails, I always get the message `Jest has detected the following 1 open handle potentially keeping Jest from exiting`. I'm running my test with `--detectOpenHandles` flag.
# This is the full message I'm getting after the test runs
Jest has detected the following 1 open handle potentially keeping Jest from exiting:
● Timeout
1 | const Redis = require('ioredis');
2 |
> 3 | const redis = new Redis();
| ^
4 |
5 | redis.on('connect', () => {
6 | console.log('Redis connection successful');
at node_modules/ioredis/built/connectors/StandaloneConnector.js:44:21
at StandaloneConnector.connect (node_modules/ioredis/built/connectors/StandaloneConnector.js:43:16)
at node_modules/ioredis/built/Redis.js:121:64
at EventEmitter.connect (node_modules/ioredis/built/Redis.js:103:25)
at new Redis (node_modules/ioredis/built/Redis.js:77:18)
at Object.<anonymous> (app/config/redis.js:3:15)
at Object.require (app/Buy Raffle Voucher/selectedVoucherHandlers.js:2:15)
at Object.require (app/Buy Raffle Voucher/index.js:1:36)
at Object.require (app/tests/buyRaffleVoucher.test.js:1:29)
# this is my buyRaffleVoucher.test.js file
const { displayVouchers } = require('../Buy Raffle Voucher/index');
describe("All buy-raffle-voucher here", () => {
it('should return available vouchers', () => {
expect(displayVouchers()).toBe("Select a voucher amount\n1. N200\n2. N500\n3. N1000");
});
});
# my redis connection file
const Redis = require('ioredis');
const redis = new Redis();
redis.on('connect', () => {
console.log('Redis connection successful');
});
// redis.on('error', () => {
// console.log('Redis connection failed');
// })
module.exports = redis;
I imported the redis connection to my test file, then called `redis.disconnect()` in the `afterAll()` hook, and it worked. But then I have other functions that communicate with redis to test, and I have to mock redis in order to test these functions, but as soon as I mock redis, `redis.disconnect()` doesn't work anymore.
# my test file after I mock redis
const { displayVouchers } = require('../Buy Raffle Voucher/index');
const { onAmountSelected } = require('../Buy Raffle Voucher/selectedVoucherHandlers');
let redis = require('../config/redis');
afterAll(() => {
redis.disconnect();
})
jest.mock('../config/redis');
describe("All buy-raffle-voucher here", () => {
it('should return available vouchers', () => {
expect(displayVouchers()).toBe("Select a voucher amount\n1. N200\n2. N500\n3. N1000");
});
it("should return recipient types", async () => {
const result = await onAmountSelected(['0', '1'], 'dasdjl')
redis.hset.mockResolvedValueOnce(1);
expect(redis.hset).toHaveBeenCalled();
expect(result).toBe('CON Voucher is for \n 1 Self \n 2 Others');
});
});
</details>
# 答案1
**得分**: 0
I finally got it working with the snippet:
```javascript
afterAll(async () => {
const actualRedis = jest.requireActual('../config/redis');
await actualRedis.quit();
});
英文:
Ok... I finally got it working with the snippet:
afterAll(async () => {
const actualRedis = jest.requireActual('../config/redis');
await actualRedis.quit();
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论