News for hackers. Stuff that matters.
Re:Stop requiring only one assertion per unit test: Multiple... by beezlewax on Saturday November 05, 2022 @10:19AM (#33481715)
Is there something like this available for javascript?
Re:Stop requiring only one assertion per unit test: Multiple... by tobyhinloopen on Saturday November 05, 2022 @10:40AM (#33481907)
Not really, no.

I like to use Jest’s toMatchObject to combine multiple assertions in a single assertion. If the assertion fails, the full object on both sides is shown in logs. You can easily debug tests that way.

The only way to make it even possible is to do some eval magic or to use a pre-processor like babel or a typescript compiler plugin.

But if you find something, Lemme know.

Re:Stop requiring only one assertion per unit test: Multiple... by fiddlerwoaroof on Saturday November 05, 2022 @12:43PM (#33483107)
Well, Function.toString() should print the code of a lambda in JavaScript. So I think you could do it without a pre-processor: use Babel as a library to parse the body of the function; run each sub-expression separately and display the results.
Re:Stop requiring only one assertion per unit test: Multiple... by tobyhinloopen on Sunday November 06, 2022 @04:31PM (#33497447)
Can you actually do that, since you don’t have the context the function is running in?

You can inspect the source, but functions don’t run in isolation so you cannot extract the source and run it somewhere else.

Simple example:

    var x = 0;
    var y = function() { x++; };
You can’t reliably execute function y with just y’s function body.

Maybe if you replace the function with another by injecting code into a copy of the source, but I’m not sure if that’s even possible.

Re:Stop requiring only one assertion per unit test: Multiple... by fiddlerwoaroof on Sunday November 06, 2022 @04:40PM (#33497555)
You can just document a constraint that these function must use the purely functional subset of JavaScript, which is enough for the sorts of assertions I typically write. Alternatively, you could constrain it to the subset that has no unbound variables or side effects.
Re:Stop requiring only one assertion per unit test: Multiple... by xeromal on Saturday November 05, 2022 @11:06AM (#33482174)
I'm not aware of any related to testing, but perhaps you could use something like this in tandem with some tests to pull it off?

https://github.com/gvergnaud/ts-pattern

Re:Stop requiring only one assertion per unit test: Multiple... by eurasiantiger on Saturday November 05, 2022 @12:03PM (#33482727)
You could parse the failing expression using babel and get an AST back.
Re:Stop requiring only one assertion per unit test: Multiple... by dalmo3 on Saturday November 05, 2022 @06:33PM (#33486623)
It might not be the general solution you're looking for, but idiomatic Jest does the job for me.

   expect(width).toBeGreaterThan(0); expect(x+width).toBeLessThan(screenWidth);
When an assertion fails it will tell you: "Expected: X; Received: Y"
Re:Stop requiring only one assertion per unit test: Multiple... by fomine3 on Monday November 07, 2022 @12:59AM (#33501745)
power-assert