0

As I understand, there is no destructors in typescript unlike most languages. I have an object in my tests which initialize a connection to the database. What approach should I take in order to make sure the programmer called the method disconnect before the test ends?

Is there a way to make the object autonomous in the detection of connection leaks, or is the only option to remember to call in the afterAll block. What do people usually do?

MathieuAuclair
  • 1,229
  • 11
  • 41
  • 2
    You could mock the method. That said, this is pretty broad. Which frameworks/libraries are you using? A [mcve] of the relevant code under test/test framework would go a long way to clarify what you're dealing with here. – ggorlen Apr 26 '22 at 00:10
  • 2
    Depending on the test framework and your coding style: a) use the [disposer pattern](https://stackoverflow.com/questions/28915677/what-is-the-promise-disposer-pattern) instead of `.connect()` and `.disconnect()` methods b) just call `afterAll` from the `getInitialisedConnection()` test helper c) use a linter rule that catches the missing cleanup. – Bergi Apr 26 '22 at 01:42
  • This sounds like a pretty standard mocking problem. `sinon` for example has a way to detect if a function was called. Isn't that what you're after? – Evert Apr 26 '22 at 04:47
  • It's a functional test, I will not mock the database, also I wanted to know in general what is the common practice for connection handling objects in typescript, I'll try @Bergi proposition, looks like a solution to my problem, thanks for the solution! – MathieuAuclair Apr 26 '22 at 05:23

0 Answers0