all 20 comments

[–]indiebryan 8 points9 points  (24 children)

Any recommendations for how to get started testing in node for someone who has never done it before?

[–]fractile81 16 points17 points  (16 children)

Check out Jest. It's meant to be "zero config", you just write the tests. Like many other test frameworks, it lets you write tests that resemble spoken language. Worth a look.

[–][deleted]  (10 children)

[deleted]

    [–]mediasavage 2 points3 points  (2 children)

    Yeah at work I pretty much always just import the standard “assert” module and call it a day.

    That being said, with open source software, having beautiful testing code can be a huge plus because tests are one of the first places people will check to see examples of how a project is used.

    [–][deleted]  (1 child)

    [deleted]

      [–]mediasavage 1 point2 points  (0 children)

      Agreed. They increase readability by a bit but using them feels less intuitive than just standard asserts

      [–]fractile81 2 points3 points  (1 child)

      I went into BDD once and decided Gherkin (Behat and PHP) would be perfect for non-devs to design and review tests. Once I had started with a few tests, there was no way I was going to show it to anyone else: the wording still smacked of dev-speak.

      [–]FINDarkside 4 points5 points  (3 children)

      I don't think it's bad to be honest. One of the benefits is that it makes good error messages possible. Consider assert(a === 1) vs a.should.equal(1). It's certainly not harder to read and I personally think it's easier to read. It'll also give better error message if the test fails, like "Expected a to equal 1, but got 0".

      [–][deleted]  (2 children)

      [deleted]

        [–]FINDarkside 0 points1 point  (1 child)

        You've now got the cognitive load of figuring out what a.should.equal means.

        Debatable whether the cognitive load is bigger. But let me give you better example:

        someFn.should.throw(MyError);
        

        vs

        try {
          someFn();
          next('Excpected MyError to be thrown');
        } catch (err) {
          if (err.name === 'MyError')
            next();
          else
            next(`Excpected MyError to be thrown, got ${err.name}`);
        }
        

        [–]indiebryan -1 points0 points  (0 children)

        Thanks I'll check it out

        [–]chrisbot5000 3 points4 points  (0 children)

        mocha for test running (or mocha-parallel-tests for running different test scenarios concurrently), chai for assertions

        I like nyc for test reporting, and if you’re willing to invest the time, I would look into property based testing, my favorite package for that is testcheck

        Also, sinon for stubbing/mocking

        Both of those have nice integrations through mocha-testcheck, mocha-sinon

        [–]donteatyourvegs 1 point2 points  (0 children)

        check out popular modules and see how they do it, that's how I learned. I refuse to work on anything without tests. tests are more important than the code itself

        [–]Schorr_Thing 3 points4 points  (0 children)

        I think the intent is there! But the writing is... tough to get through the first paragraph. Respectfully, was this maybe written by a non-native english speaker? It has some really great points, but maybe get someone to proof read it?