TestingUnit Tests

Like the name implies, unit tests are testing individual units of our application like a single function as opposed to an entire package or action. In Pup, unit tests are defined and run using the Jest testing framework.

Typically, unit tests are relatively easy to write as long as our code is simple. Essentially, we're asking "given these inputs, do we get the expected outputs from this unit (function)?" By their very nature, unit tests themselves should be simple; we don't need to be excessive. For example, if we're the only ones using the function and we expect numbers to be passed, we don't necessarily need to test that it throws an error when a string is passed. It should be pretty clear in our product that the function is being misused.

On the right, we've taken an example unit tests from Pup, verifying that the parseMarkdown module works as expected. Our function is simple: it's supposed to take in a string of Markdown and return us some HTML. That's it. Our test, then, verifies this is the case.

Here, we import the parseMarkdown function into our test file and invoke it inside of our test(), passing a string of Markdown. At the bottom, we expect(html) to match the string of HTML we've passed to .toBe(). If the two match, our test will pass and we've verified that our function is working as expected!

Testing JavaScript Course

If you're looking to dig deeper into the what, why, and how of testing in JavaScript, the Testing JavaScript course by Kent C. Dodds is highly recommended.

import { Parser, HtmlRenderer } from 'commonmark';

export default (markdown, options) => {
  const reader = new Parser();
  const writer = options ? new HtmlRenderer(options) : new HtmlRenderer();
  const parsed = reader.parse(markdown);
  return writer.render(parsed);
import parseMarkdown from './parseMarkdown';

describe('parseMarkdown.js', () => {
  test('it returns HTML when passed a string of Markdown', () => {
    const html = parseMarkdown('### Testing\n**Markdown** is working.');
    expect(html).toBe('<h3>Testing</h3>\n<p><strong>Markdown</strong> is working.</p>\n');

Running Unit Tests

Unit and integration tests are run via Jest and one of two NPM scripts: npm run test or npm run test-watch. The former runs your unit and integration tests once and then exits, while the latter runs your tests once and the again whenever it detects changes/new tests (good for development).

meteor npm run test
meteor npm run test-watch