Skip to content

Is there interest in replacing mocha with node:test #2338

@cjihrig

Description

@cjihrig

Since v18, Node has shipped a built in test runner, which we could replace mocha with. This is what the change would look like.

Pros:

  • Fewer dependencies, which have been causing us grief lately. Mocha hasn't been problematic, and is probably my second favorite test runner. However, mocha still weighs in at 8.5MB and 104 dependencies.
  • Better security posture because it is built in.
  • node:test seems to be better at reporting things that mocha skips over like unhandled rejections from not awaiting assert.rejects().
  • For what we do in this project, the required API is essentially the same.
  • It also has built in code coverage, although that is still experimental, mocking, snapshots, etc.

Cons:

  • The biggest con is speed. In my testing, node:test and mocha have pretty similar speed when run in the same mode. Unfortunately, Node's "fast" mode (running all test files in the same process) only exists in v22 and up. Since we test on v18 and v20, we would have to use process level isolation for each test file, which adds a bit of overhead, although I don't think it's enough to really be noticeable. Fun fact: this is how I learned that we were using chai-as-promised inconsistently.
  • Because it was only introduced in v18, it has been less stable between versions. From my testing, none of the changes between versions seem to impact this project other than having to use process level isolation.

A number of notable Node projects have adopted node:test, so I don't think this would mean using something immature.

Disclaimer: I wrote node:test, so I am definitely biased here. If we don't want to migrate, no hard feelings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions