Bug 1957513 Comment 0 Edit History

Note: The actual edited comment in the bug view page will always show the original commenter’s name and original timestamp.

We don't really have a good story right now across the tree for setup/teardown of individual tasks in a test. We have head files, `add_setup`, and `registerCleanupFunction`, but these work at the scale of entire tests/sets of tasks.

I currently find myself using the following pattern in Nimbus tests:

```
function setupTest() {
  // common test code
  return {
    cleanup: () => { /* cleanup code */ },
    /* other utilities that are used in the test */
  }
}

add_task(function test_foo() {
  const { cleanup } = setupTest();
  /* do the actual test */
  cleanup();
});
```
Here ([1](https://searchfox.org/mozilla-central/rev/7857ea04d142f2abc0d777085f9e54526c7cedf9/toolkit/components/nimbus/test/unit/test_FirefoxLabs.js), [2](https://searchfox.org/mozilla-central/rev/7857ea04d142f2abc0d777085f9e54526c7cedf9/toolkit/components/nimbus/test/unit/test_Migrations.js#85)) are some examples of tests doing just that.

NormandyTestUtils exposes a [`decorate_task`](https://searchfox.org/mozilla-central/rev/7857ea04d142f2abc0d777085f9e54526c7cedf9/toolkit/components/normandy/test/NormandyTestUtils.sys.mjs#181-201) method uses the decorator pattern to extract setup and teardown that is common across functions. The decorator pattern looks roughly like this:

```
function setupTest(fn) {
  return async function(ctx) {
    /* do the test setup */
    try {
      await fn(ctx);
    } finally {
      /* do test setup */
    }
  };
}

decorate_task(setupTest, function test(ctx) {
  /* actual test, no setup/teardown gunk */
});
```
Normandy server is dead and so Normandy client is slated to to be removed [soon](https://bugzilla.mozilla.org/show_bug.cgi?id=1955807), so
I propose that we add this functionality to SimpleTest.js and testing/xpcshell/head.js so that we can use this everywhere across the tree.
We don't really have a good story right now across the tree for setup/teardown of individual tasks in a test. We have head files, `add_setup`, and `registerCleanupFunction`, but these work at the scale of entire tests/sets of tasks.

I currently find myself using the following pattern in Nimbus tests:

```
function setupTest() {
  // common test code
  return {
    cleanup: () => { /* cleanup code */ },
    /* other utilities that are used in the test */
  }
}

add_task(function test_foo() {
  const { cleanup } = setupTest();
  /* do the actual test */
  cleanup();
});
```
Here ([1](https://searchfox.org/mozilla-central/rev/7857ea04d142f2abc0d777085f9e54526c7cedf9/toolkit/components/nimbus/test/unit/test_FirefoxLabs.js), [2](https://searchfox.org/mozilla-central/rev/7857ea04d142f2abc0d777085f9e54526c7cedf9/toolkit/components/nimbus/test/unit/test_Migrations.js#85)) are some examples of tests doing just that.

NormandyTestUtils exposes a [`decorate_task`](https://searchfox.org/mozilla-central/rev/7857ea04d142f2abc0d777085f9e54526c7cedf9/toolkit/components/normandy/test/NormandyTestUtils.sys.mjs#181-201) method uses the decorator pattern to extract setup and teardown that is common across functions. The decorator pattern looks roughly like this:

```
function setupTest(fn) {
  return async function(ctx) {
    /* do the test setup */
    try {
      await fn(ctx);
    } finally {
      /* do test setup */
    }
  };
}

decorate_task(setupTest, function test(ctx) {
  /* actual test, no setup/teardown gunk */
});
```
Normandy server is dead and so Normandy client is slated to to be removed [soon](https://bugzilla.mozilla.org/show_bug.cgi?id=1955807), so I propose that we add this functionality to SimpleTest.js and testing/xpcshell/head.js so that we can use this everywhere across the tree.

Back to Bug 1957513 Comment 0