Skip to content

Adopt ensureNoDisposablesAreLeakedInTestSuite in unit tests #190503

Closed

Description

Background

Changes in the order tests ran in caused spurious failures in a test that used ensureNoDisposablesAreLeakedInTestSuite. This was almost certainly a result of leaky tests further up the chain, but it's very hard to figure out which test it is. We should seek to adopt ensureNoDisposablesAreLeakedInTestSuite in the unit tests we own.

Adoption

ensureNoDisposablesAreLeakedInTestSuite is a utility that ensures that disposables created in a test are disposed of. This is very useful to find leaks in code you own.

If you have a setup and teardown blocks, make sure the function is called before the former and after the latter, like so:

suite('myCoolTests', () => {
  teardown(() => { /* ... */ });

  ensureNoDisposablesAreLeakedInTestSuite();

  setup(() => { /* ... */ });

  // ...tests
});
  • Else, simply call the function at the beginning of the suite:
suite('myCoolTests', () => {
  ensureNoDisposablesAreLeakedInTestSuite();

  // ...tests
});

The method also returns a store: DisposableStore you can use for convenience to dispose instances within your tests, like so:

suite('myCoolTests', () => {
  const store = ensureNoDisposablesAreLeakedInTestSuite();

  test('MyEditorWorks', () => {
    const editor = store.add(new CodeEditor());

    // ...assertions
  });
});

It will then emit an error in the "after each" step if there are any dangling disposables. The error shown will have a stacktrace to where the leaked disposable was created.

Sometimes, it will not be the specific disposable that wasn't disposed of, but the thing that created it. You will want to walk up the displayed stack to find where the leak is.

⚠️ As referenced, leaks in earlier tests can your tests to fail when the full suite is run in CI. If this is the case, comment out your ensureNoDisposablesAreLeakedInTestSuite. We can do a more efficient pass later as we uncomment the leak detector.

Ask

We have a lot of tests! Please spend some time in the next debt week adding this function call to your tests, prioritizing ones that do more complex setups such as those involving code editors. It's not expected that we'll get every test in the first pass, and we can start calling out individual tests in future items.


Activity

  1. added
    debt
    engineering
    on Aug 15, 2023
  2. 109 remaining items

  3. removed their assignment
    on Oct 5, 2023
  4. removed their assignment
    on Oct 17, 2023
  5. Tyriar commented on Oct 23, 2023

    @Tyriar
    Contributor

    The bulk of this is done so we can remove it from the plan next month, Andrea Mah (@andreamah) let's close this out as high priority next week though

  6. removed their assignment
    on Oct 30, 2023
  7. connor4312 commented on Oct 31, 2023

    @connor4312
    MemberAuthor

    Thanks all. I will open a followup issue for owners of specific tests who haven't adopted the disposable checker

  8. locked and limited conversation to collaborators on Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

debtengineering

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions