Skip to content

🛠️ Repo: improve speed and reliability of watch mode tests #5714

@jedwards1211

Description

@jedwards1211

Tooling Suggestion Checklist

Overview

Once my changes in #5409 are merged, I want to adapt my IPC messaging approach to improve other preexisting watch mode tests.

For example, one of the basic tests is

  it("reruns test when watched test file is touched", function () {
    const testFile = path.join(tempDir, "test.js");
    copyFixture(DEFAULT_FIXTURE, testFile);

    return runMochaWatchJSONAsync([testFile], tempDir, () => {
      touchFile(testFile);
    }).then((results) => {
      expect(results, "to have length", 2);
    });
  });

All of these runMochaWatchJSONAsync tests by default sleep for 2 seconds before and after the change function (in this case touchFile). This causes two problems:

  • A hiccup in task switching or virtualization in the CI runner might cause Mocha to take longer to boot up or run tests than those 2 seconds, causing the test to fail
  • Running the whole test suite is slow, so it's harder to iterate on improvements

I imagine refactoring this test to work like this:

  it("reruns test when watched test file is touched", async function () {
    const testFile = path.join(tempDir, "test.js");
    copyFixture(DEFAULT_FIXTURE, testFile);

    const results = await runMochaWatchJSONAsync(
      [testFile],
      tempDir,
      // would refactor runMochaWatchJSONAsync to turn on IPC messaging
      // and provide this API
      async (mochaProcess, { gotMessage }) => {
        // no more waiting 2 seconds when Mocha runs quickly,
        // but we can wait longer if necessary when there's a hiccup
        await gotMessage((msg) => msg.runFinished);
        touchFile(testFile);
        await gotMessage((msg) => msg.runFinished);
      }
    )
    expect(results, "to have length", 2);
  });

And likewise for the other tests.

I would refactor my runMochaWatchWithChokidarMock tests to use runMochaWatchJSONAsync with a mockChokidar: true option.

Additional Info

No response

Metadata

Metadata

Assignees

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions