显示此检测消息 => Jest 检测到以下 1 个打开的句柄,可能阻止 Jest 退出

huangapple go评论55阅读模式
英文:

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&#39;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 `&#39;../Buy Raffle Voucher/index&#39;`. Now, this `displayVouchers` function does not communicate with redis in any way whatsoever, although the `&#39;../Buy Raffle Voucher/index&#39;` 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&#39;m running my test with `--detectOpenHandles` flag.


# This is the full message I&#39;m getting after the test runs

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

● Timeout

  1 | const Redis = require(&#39;ioredis&#39;);
  2 |
&gt; 3 | const redis = new Redis();
    |               ^
  4 |
  5 | redis.on(&#39;connect&#39;, () =&gt; {
  6 |     console.log(&#39;Redis connection successful&#39;);

  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.&lt;anonymous&gt; (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&#39;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(&quot;should return recipient types&quot;, async () =&gt; {
    const result = await onAmountSelected([&#39;0&#39;, &#39;1&#39;], &#39;dasdjl&#39;)
    redis.hset.mockResolvedValueOnce(1);
    expect(redis.hset).toHaveBeenCalled();
    expect(result).toBe(&#39;CON Voucher is for \n 1 Self \n 2 Others&#39;); 
});

});



</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 () =&gt; {
const actualRedis = jest.requireActual(&#39;../config/redis&#39;);
await actualRedis.quit();

});

huangapple
  • 本文由 发表于 2023年5月25日 23:17:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定