What is Nostr?
Doug Parker 🕸️ /
npub1hvt…jhta
2024-06-08 01:25:18

Doug Parker 🕸️ on Nostr: #JavaScript #Testing #Idea: Instead of manually managing `beforeEach` and `afterEach` ...

#JavaScript #Testing #Idea: Instead of manually managing `beforeEach` and `afterEach` in #Jest / #Jasmine / etc., what if we had a way to register a "run this on test cleanup" callback at any time? Kind of like how `defer` in #Go will automatically clean up a resource at the end of a function.

```javascript
it('does a thing', () => {
const thing = createAThing();
useTheThing(thing);
});

function createAThing() {
const thing = makeThing();
jasmine.afterThisTest(() => thing.cleanup());
return thing;
}
```

This feels much more ergonomic and allows you to create resources without messing up your scoping with `beforeEach` / `afterEach`.

Has anyone tried this? I don't think I've seen this pattern in a JS testing context.
Author Public Key
npub1hvt595dnyyfx57xf5edvhxuppfykj0qmw6myg6hqt87rluxv90eqdwjhta