atlas-cleanup-tests
Seamlessly wrap sync mocha tests with your cleanup script. Supports 'done' callback.
install
npm install --save-dev atlas-cleanup-tests
why
If you are mocking an API in a mocha test, you may need to revert changes after the test is done. The afterEach
hook in mocha runs after mocha tries to output the results for the test. This means that if your test mocks an API that mocha depends on (e.g. process.stdout
), mocha itself will fail.
If we try and mock console.log
, mocha will break:
// Animal.test.jsconst mocha = ;const expect = ;const rewire = const Animal = // a handle for reverting mockslet revert;
Fixing this isn't too difficult to do inside your tests, but I like to keep a clean house. Additionally, mocking standard output is pretty avoidable. By using a wrapper (e.g. a Logger) around console.log
or process.stdout
, you can easily mock your application's logging functionality. For simple stuff though, using a special logger feels uneccesary.
This package gives you a cleanup function which runs after your test runs, but before afterEach
runs. This way, your home stays clean before mocha gets invited over.
examples
done
callback
without // Animal.test.js...const Cleaner = ;const cleanup = // seamlessly wrap your tests
done
callback
with Even if your test is sync, mocha's done
callback can be used to test (partial) code-paths. The cleanup
function will also be able to wrap these tests without any work on your part:
// Animal.test.js...
caveats
This wrapper assumes that you have mocked your async dependencies. It will not catch unhandled async errors (e.g. assertions made in an async callback). Unless you are unit testing your async functions or doing integration tests, it's probably better to mock them anyway.