Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.JS testing DOM with Mocha?

I'm trying to do some simple view testing in my Node.JS app using the Mocha testing framework...

For example, I want to test that the <ol> on the page has n <li> children, given the number of records I setup in my test.

I tried using Apricot to do this, and while I got it to work, when it fails the error messages are fantastically unhelpful... also, it doesn't always work.

What I'd love is a simple way to test the response body for HTML elements, so I can determine if they match the data they should be displaying.

Here's my test in it's current state: https://gist.github.com/2330685

Anyone know how I can do this?

like image 472
neezer Avatar asked Apr 07 '12 17:04

neezer


People also ask

How do you run a test case with Mocha?

In order to run our tests in a browser, we need to set up a simple HTML page to be our test runner page. The page loads Mocha, the testing libraries and our actual test files. To run the tests, we'll simply open the runner in a browser. If you're using Node.

Which is better jest or Mocha?

Jest is also faster than Mocha. It has built-in support for snapshot testing, which means tests are run automatically on each change to the code. This makes it easy to keep your tests up to date as you work. Mocha has more features out of the box since it is a more mature tool with a larger community of contributors.


1 Answers

Posting the comment as answer as well.

For DOM manipulation or element finding, I am suggesting the great library cheerio, which can load the html as string and then use jQuery-like selectors. Also it seems to be really lightweight. I replaced the JSDOM with request + cheerio combination.

like image 185
Risto Novik Avatar answered Nov 15 '22 04:11

Risto Novik