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

Jest did not exit one second after the test run has completed #2993

Closed
simophin opened this issue Jun 21, 2020 · 9 comments
Closed

Jest did not exit one second after the test run has completed #2993

simophin opened this issue Jun 21, 2020 · 9 comments

Comments

@simophin
Copy link

simophin commented Jun 21, 2020

Goals

Trying to close Realm instance after each unit test.

Expected Results

Everything shutdown nicely.

Actual Results

Test succeeds but Jest complains there are asynchronous operations that weren't stopped, then the Jest process will hang.

Steps to Reproduce

  1. Minimal realm set up
  2. Open the database during one test
  3. Close the database once done.

Code Sample

https://github.com/simophin/realm-close-issue

$ npm install && npm run test

This is the result I got:

npm run test

> [email protected] test /Users/***/Temp/testrealm
> jest ./src

 PASS  src/__tests__/realm-tests.ts
  realmTest
    ✓ should exist (17 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.339 s, estimated 2 s
Ran all test suites matching /.\/src/i.
Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.Also, if I add `--detectOpenHandles` to jest, the test will succeed, without the complain, but still hang.

If I added --detectOpenHandles --forceExit to jest, the test will succeed, without the complain, and terminate successfully.

This does not happen in realm-js 5.0.x, only after we upgraded to 6.0.

Version of Realm and Tooling

  • Realm JS SDK Version: 6.0.2
  • Node or React Native: Node 10.21.0
  • Client OS & Version: Mac OS
  • Which debugger for React Native: None
@kraenhansen
Copy link
Member

kraenhansen commented Oct 8, 2020

  • I can reproduce the issue with the repository you've linked to.
  • I tried upgrading to the latest Realm JS version (6.1.3), but that didn't fix the issue.
  • I tried changing to the synchronous variant of constructing the database, which didn't fix the issue.
  • I've tried adding wtfnode to the project, but that didn't reveal a smoking gun either.

We'll need to investigate this further.

@sonyboy232
Copy link

FWIW I'm experiencing these same symptoms using react native

Realm ^6.1.1

React Native info:

    OS: Linux 4.15 Ubuntu 18.04.5 LTS (Bionic Beaver)
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 15.30 GB / 31.27 GB
    Shell: 4.4.20 - /bin/bash
  Binaries:
    Node: 10.22.1 - /usr/bin/node
    Yarn: Not Found
    npm: 6.14.6 - /usr/bin/npm
    Watchman: Not Found
  SDKs:
    Android SDK:
      API Levels: 23, 26, 28, 29
      Build Tools: 28.0.3, 29.0.2
      System Images: android-22 | Google APIs Intel x86 Atom, android-28 | Intel x86 Atom_64, android-28 | Google APIs Intel x86 Atom, android-29 | Google APIs Intel x86 Atom, android-29 | Google Play Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: 4.0 AI-193.6911.18.40.6626763
  Languages:
    Java: 1.8.0_265 - /usr/bin/javac
    Python: 2.7.17 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1 
    react-native: 0.63.2 => 0.63.2 
  npmGlobalPackages:
    *react-native*: Not Found

@thomasarogers
Copy link

thomasarogers commented Oct 20, 2020

Experiencing the same with Realm-js v6.0.0

@RanderGabriel
Copy link

Same here with Realm-js v6.1.7

@Overtorment
Copy link

I solved it with mocks

const realmInstanceMock = {
  close: function () {},
  objects: function () {
    const wallets = {
      filtered: function () {
        return [];
      },
    };
    return wallets;
  },
};
jest.mock('realm', () => {
  return {
    open: jest.fn(() => realmInstanceMock),
  };
});

@mdbiscan
Copy link

mdbiscan commented May 26, 2021

I am experiencing this issue. This warning shows up on any single test (even for files that don't import Realm) or after all tests are run. If I remove the reference to Realm in files, the issue persists. It's only when I completely remove Realm from package.json does this warning go away.

When I run --detectOpenHandles, the warning no longer appears, but also there is no other message output.

I do use Realm.clearTestState(); to clean up after tests, not that I believe this would solve it.

I have seen this warning with issues using Mongoose, when the connection is not being closed correctly. Realm doesn't seem to have an explicit way of closing connections. Would someone be able to explain this?

I'm using Realm in an unmanaged Expo app:

 React Native: ~0.63.4
 Expo: ^40.0.0
 Jest Expo: ^41.0.0
 Realm: ^10.4.1
 Node: v14.16.1

Here's an abridged example of how Realm is being used:

      const wallet = await KeychainStorage.getItemAsync('wallet');
      const realm = await Realm.open({ schema: [WalletAddressSchema] });
      const walletAddresses = await realm.objects('WalletAddress');
      const index = walletAddresses.length;
      const address = await Generator.generateAddress(
        wallet.seed,
        index,
        network,
        scheme
      );

      realm.write(() => {
        realm.create('WalletAddress', {
          address,
          index,
        });
      });

But like I said, I could comment out this code and any import and reference to Realm, and the issue persists. It isn't until Realm is no longer in package.json does this warning go away.

Update 1 I did find examples of using real.close() in the codebase, but this did not solve the issue.
Update 2 I see in CI that Realm is attempting to write to disc for every test, even though Realm is only used in 1 file. I moved the clearTestState method to a single test, but this does not solve the issue.

@cesar-medina-25
Copy link

cesar-medina-25 commented Jun 7, 2021

I am experiencing the same issue with Jest and React native.

 React Native: 0.64.1
 Jest: ^26.6.3
 Realm: ^10.4.1
 Node: v14.16.1

Using --detectOpenHandles just hides the warning but the test still waits to be finished
Setting environmental variable REALM_DISABLE_ANALYTICS to true avoid some async calls, but Realm.open method specifically Realm constructor still generates this warning.
Doing afterAll method async produce the same warning too.

This is the code of a test using a Realm in memory database:

import Realm from 'realm'
import { UserModel } from '../../src/database/models/User'

describe('user repository', () => {
  let realm: any

  beforeAll(async () => {
    realm = await Realm.open({
      inMemory: true,
      schema: [UserModel],
    })
  })

  afterAll(() => {
    realm.close()
    Realm.clearTestState()
  })

  test('validate empty users table', () => {
    const users = realm.objects('USERS')
    expect(users.length).toBe(0)
  })
})

Jest warning after run the test:

Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue.

@takameyer
Copy link
Contributor

@simophin we have found the issue and the fix should arrive in the next release.

@kneth
Copy link
Contributor

kneth commented Oct 7, 2021

The fix has been released in version 10.8.0.

@kneth kneth closed this as completed Oct 7, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants