Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open handle error when using with Jest #308

Open
soapwong703 opened this issue Nov 19, 2024 · 1 comment
Open

Open handle error when using with Jest #308

soapwong703 opened this issue Nov 19, 2024 · 1 comment

Comments

@soapwong703
Copy link

I am trying to setup test cases with jest, but after the test was finished the process will stalled and does not exit.

Using the command jest --detectOpenHandles will show this error message preventing jest from exiting.

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

  ●  MESSAGEPORT

      1 | import { factory, primaryKey } from "@mswjs/data";
      2 |
    > 3 | const db = factory({
        |                   ^
      4 |   user: {
      5 |     userCode: primaryKey(String),
      6 |     name: String,

      at Object.sync (node_modules/.pnpm/@[email protected][email protected]/node_modules/@mswjs/data/lib/extensions/sync.js:69:19)
      at factory (node_modules/.pnpm/@[email protected][email protected]/node_modules/@mswjs/data/lib/factory.js:50:12)
      at Object.<anonymous> (src/mocks/db.ts:3:19)
      at Object.<anonymous> (src/mocks/handlers/user.ts:13:52)
      at Object.<anonymous> (src/mocks/server.ts:12:54)
      at Object.<anonymous> (src/setupTests.ts:10:17)

Actual Code:

import { factory, primaryKey } from "@mswjs/data";

const db = factory({
  user: {
    userCode: primaryKey(String),
    name: String,
    rank: String,
    updateDate: () => new Date(),
  },
});


export default db;

After investigation, I found the issue should be related to the unclosed BroadcastChannel 'mswjs/data/sync' in this file:

const channel = new BroadcastChannel('mswjs/data/sync')

I am suggesting to expose an method for user to manually close the broadcast channel after the test case is completed.

@kettanaito
Copy link
Member

Hi, @soapwong703. Thanks for reporting this!

We need to .unref() the broadcast channel to free memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants