Hi evereyone, I'm Karam, CompSci student. This is my first post here, don't hesitate to point out anything wrong i wrote.
I'm working on TempusStack, a simple Docker orchestration tool I'm building to learn about containerization and testing workflows. I've hit a tricky race condition that I can't seem to solve.
The Problem
My Jest tests work perfectly when run sequentially (jest --runInBand) but fail randomly during parallel execution. Tests see Docker containers from other test files that should have been cleaned up.
beforeAll(async () => {
await down(); // removes all tempusstack_* containers
});
test('CLI status prints no containers', async () => {
const { stdout } = await execa('node', ['bin/tempusstack.js', 'status']);
expect(stdout).toMatch(/No tempusstack container/);
});
What I've Tried
Polling after removal:
async function waitForContainersToBeGone(maxRetries = 20, delayMs = 200) {
for (let i = 0; i < maxRetries; i++) {
const containers = await getTempusstackContainers(docker);
if (containers.length === 0) {
await new Promise(resolve => setTimeout(resolve, 100));
const doubleCheck = await getTempusstackContainers(docker);
if (doubleCheck.length === 0) return;
}
await new Promise(resolve => setTimeout(resolve, delayMs));
}
}
Other attempts:
- Shared Docker instance across all tests
- Longer wait times and forced removal
- Test-specific cleanup with unique prefixes
None of these approaches resolved the intermittent failures.
My Theory
The issue appears to be Docker's asynchronous internal cleanup:
- container.remove() completes successfully
- docker.listContainers() shows the container is gone
- Docker's internal state isn't fully consistent yet
- Parallel tests see "ghost" containers
Looking for Input
Have you encountered similar Docker timing issues with Jest? I'm particularly interested in:
- Better approaches for waiting on Docker cleanup completion
- Alternative test isolation strategies
- Docker API timing quirks I might be missing
Full details and code examples are in the GitHub issue.
Any insights would be greatly appreciated. This seems like a common enough pattern that others must have solved it before.
Top comments (0)