kolodny / safetest

MIT License
1.38k stars 34 forks source link

Open Handle keeping Jest from closing #32

Open Anoerak opened 7 months ago

Anoerak commented 7 months ago

Hello,

I've been running into an issue where an open handle prevented Jest to close.

The quick fix for now is to add a jest.teardown,js file with this snippet:

afterAll(async () => {
    // Close all open handles
    const handles = process._getActiveHandles();
    handles.forEach((handle) => {
      if (handle.constructor.name === 'DirHandle') {
        handle.close();
      }
    });
  });

More details about the config:

    "scripts": {
        "safetest": "cross-env OPT_URL=${OPT_URL:-http://localhost:10088/src} react-scripts --inspect test --runInBand --detectOpenHandles --testMatch '**/*.safetest.{j,t}s{,x}' --setupFilesAfterEnv ./setup-safetest.tsx --testTimeout=30000",
        "safetest:ci:test": "OPT_URL=http://localhost:10088/ OPT_ARTIFACTS=artifacts.json OPT_DOCKER=1 OPT_CI=1 npm run safetest -- --watchAll=false --json --outputFile=results.json --bail=5 --ci=1",
        "safetest:ci": "(npm run safetest:ci:test || true)",
        "safetest:regenerate-screenshots": "npm run safetest -- --watchAll=false --docker=1 --update-snapshot",
        "process:ci": "npx safetest add-artifact-info artifacts.json results.json && cp results.json ../../../JAWS_Project/jaws/public/safetest/fleet.json"
    },

And the message from the console:

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

  ●  DIRHANDLE

      1 | import { setup } from 'safetest/setup';
      2 |
    > 3 | setup({
        |      ^
      4 |     bootstrappedAt: require.resolve('./src/main.jsx'),
      5 |     ciOptions: {
      6 |         usingArtifactsDir: 'artifacts'

      at setOptions (node_modules/safetest/src/set-options.ts:54:7)
      at setup (node_modules/safetest/src/setup.ts:59:13)
      at Object.<anonymous> (setup-safetest.tsx:3:6)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

safetest-server-1.43.1-srn7a1rcjck
safetest-server-1.43.1-4xx2i5pvy6c

If need anything else, please let me know.

Thank you.

kolodny commented 6 months ago

That's due to Safetest not waiting for the docker container to stop before the tests are officially done. This speeds up testing by however many seconds it takes for docker to close those per test file. Perhaps we can nohup those when the tests finish. I'm open to a PR if you want to do that. The current message is just a red herring and shouldn't affect anything

kolodny commented 6 months ago

@Anoerak Did you mean without that change Jest doesn't close at all? If so can you create a repro case?

Anoerak commented 6 months ago

Hi, Sorry, I've been working on something else for the past weeks. Thanks for your answer. Let me get back into this project and I'll keep you updated.