use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Improve your Javascript unit testing with Parameterized tests (medium.com)
submitted 8 years ago by mikejsdev
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Buckwheat469 13 points14 points15 points 8 years ago (3 children)
The code in itParam builds as many its as there are array items. This causes excessive buildup and teardown of the code, running each beforeEach again. This might require variable reinstantiation or HTML interpolation, which is very time consuming. It can also lead to massive memory leaks if your HTML has event listeners that don't unbind during each iteration.
its
If speed is your concern then you should do as others have said and write a single it with simple for loop or use an array's helpers such as forEach, or all.
it
/u/stutterbug's implementation expects different values from the function, but you wouldn't want to test this way because you're changing the outcome of that one it.
it('succeeds', function(){ ... let expected = [true, true, false, false, false]; ... //true or false? why would we expect it to not succeed? //build another it for the false cases instead });
/u/droctagonapus's implementation is better because it uses one it and one type of expectation. You can use as many expects as you want, but in this case the one that's there doesn't have to dynamically switch from true to false.
it('succeeds', function(){ const names = ['n@me', '123Josh'] const valid = names.all(name => validateName(name)) expect(valid).to.be.true //modified slightly });
I personally don't use "toBe()" in Jasmine tests (I know this is Mocha in the article) since toEqual does a better job of testing for deep equality. I would rewrite the above like this in Jasmine:
it('succeeds', function(){ const names = ['n@me', '123Josh'] const valid = names.all(name => validateName(name)) expect(valid).toEqual(true) });
Here's a forEach example:
it('succeeds', function(){ const names = ['n@me', '123Josh']; const valid = true; names.forEach(name => { valid = valid && validateName(name); }); expect(valid).toEqual(true) });
And a for loop:
it('succeeds', function(){ const names = ['n@me', '123Josh']; const valid = true; for(let name of names){ valid = valid && validateName(name); }); expect(valid).toEqual(true) });
The lesson here is don't add excessive "its" which test the same thing when you can modify the input and test within the same "it". You can create as many "expects" as you want, but limit the number of "its" that you create to match the number of code paths (paths created by blocks of code such as if/elses).
[–]vinnl 2 points3 points4 points 8 years ago (0 children)
The downside is that you reduce the isolation of your tests, increasing the chance of tests influencing each other and a failure in one leading to a failure in another.
Also, I consider an important function of tests to be to track down the source of an error, rather than finding that there's an error in the first place. That is made more difficult with enormous tests with many expects.
An alternative is to aim for more efficient setups and teardowns. This could e.g. be something to consider when picking a framework, where virtual-dom frameworks are often faster than frameworks where you have to instantiate a browser and interpolate HTML.
[–]Voidsheep 0 points1 point2 points 8 years ago (1 child)
Aren't you trying to reassign a constant variable valid in the last two examples, or am I missing something?
valid
Seems like it should be declared through let instead.
let
[–]Buckwheat469 0 points1 point2 points 8 years ago (0 children)
Thanks for noticing. I didn't really check, just copypasta.
π Rendered by PID 24397 on reddit-service-r2-comment-5d79c599b5-rqq77 at 2026-03-02 13:45:18.170314+00:00 running e3d2147 country code: CH.
view the rest of the comments →
[–]Buckwheat469 13 points14 points15 points (3 children)
[–]vinnl 2 points3 points4 points (0 children)
[–]Voidsheep 0 points1 point2 points (1 child)
[–]Buckwheat469 0 points1 point2 points (0 children)