* place and for easy resource cleanup at the end of tests. Also prints which resources are created | ||
* and destroyed in case tests are aborted midway through and manual cleanup is required. | ||
*/ | ||
export class TestResourceManager { |
The reason will be displayed to describe this comment to others. Learn more.
Rather than introducing a new class/concept to the codebase, I would consider reusing Disposable
(an Unsubscribable
supporting async resource disposal) pattern from sourcegraph-typescript (example usage) :
// Return `Promise<AsyncDisposable>` instead of `Promise<void>`.
export async function ensureLoggedInOrCreateTestUser(...): Promise<AsyncDisposable> {
console.log(`Creating test user "${name}"`)
return {
dispose: () => {
console.log(`Destroying user ${name}`)
return deleteUser(...)
}
}
}
describe('Search regression test suite', () => {
const disposables = new Set<Disposable | AsyncDisposable | Unsubscribable>()
// Free resources created during testing.
afterAll(() => disposeAllAsync(disposables))
test('something', () => {
disposables.add(
await ensureLoggedInOrCreateTestUser({...})
)
})
})
Pros:
ensureLoggedInOrCreateTestUser()
is called from multiple test suites. Callers need not look up how a given resource should be cleaned up, they simply need to handle the returned disposable / subscription.
Rather than introducing a new class/concept to the codebase, I would consider reusing
Disposable
(anUnsubscribable
supporting async resource disposal) pattern from sourcegraph-typescript (example usage) :Pros:
ensureLoggedInOrCreateTestUser()
is called from multiple test suites. Callers need not look up how a given resource should be cleaned up, they simply need to handle the returned disposable / subscription.