Typescript nested forEach as decorator

Viewed 74

We have rather complicated api that should be nested with multiple parameters. Now tests looks like callback hell

[true, false, null].forEach(customer => {
   [true, false, null].forEach(sale => {
    it('test', ()=> {
       // Do something with customer and sale
     })
  })
})

Tests can have up to 10 parameters and all should be checked.

I am thinking of switching to typescript. So I wonder if there is a possibility to rewrite test to something like

let customer: []
@param(customer, [true, false, null])
it('test', ()=>{})

So that the functionality stays the same. Is it any way for doing that?

1 Answers

Decorators are applicable to classes only. it is regular function and cannot be used with a decorator. it cannot be unnested this case because it should receive customer value that is specific to this test, and this is done in nested function scope.

This is not what callback hell usually means. The level of nesting of forEach callbacks is strictly determined by loop nesting and doesn't complicate control flow. In case this is a concern, loops can be rewritten with any other loop structure:

for (const customer of [true, false, null]) {
  ...
}

Jest API has each that allows to provide data in a loop in more concise way.

Related