testjavascript / nodejs-integration-tests-best-practices

✅ Beyond the basics of Node.js testing. Including a super-comprehensive best practices list and an example app (March 2024)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proper cleaning with before/after hooks

DanielGluskin opened this issue · comments

Hey,

Some thoughts regarding cleaning after nock (and in general),

some tests have:

nock.disableNetConnect();
nock.enableNetConnect('127.0.0.1');

Inside the beforeAll hook, but no proper clean up is never done:
nock.enableNetConnect();

We also use nock(...).persist() on a few occasions and never clean them up so they persist between different tests. A good nock clean up would be:

nock.cleanAll();
nock.enableNetConnect();

Maybe add a new rule such as: “Each test should clean after itself”? (same goes for sinon and all). If we use nock inside the beforeAll hook, we need to clean inside afterAll, same for beforeEach and afterEach.

Therefore, we shouldn’t have calls like this:

beforeEach(() => {
    nock.cleanAll();

Because we assume if nock was used in another test it was already cleaned up by that test.

Another small thing, we use async (done) inside most of the before/after hooks, isn't redundant to use both async and done in the same time?