Skip to content

Instantly share code, notes, and snippets.

@rstacruz
Created September 1, 2014 09:14
Show Gist options
  • Save rstacruz/4f7e5c11b5238056cf79 to your computer and use it in GitHub Desktop.
Save rstacruz/4f7e5c11b5238056cf79 to your computer and use it in GitHub Desktop.

Here's a simple Mocha test case. It emulates chai.js's expect(a).eq(b) behavior.

it('comparing strings', function () {
  var string1 = "div, p {\n  color: red;\n  margin: 0;\n  padding: 0;\n}";
  var string2 = "div, p {\n  color: red;\n  margin: 20px;\n  padding: 0;\n}";

  var e = new Error("Strings don't match");
  e.expected = string1;
  e.actual = string2;
  e.showDiff = true;

  throw e;
});

Doing mocha on this yields:

$ mocha -C
...
  1)  comparing strings:

      Error: Strings don't match
      + expected - actual

      +"div, p {\n  color: red;\n  margin: 0;\n  padding: 0;\n}"
      -"div, p {\n  color: red;\n  margin: 20px;\n  padding: 0;\n}"

Doing mocha --inline-diffs yields the exact same thing:

$ mocha -C
...
  1)  comparing strings:

      Error: Strings don't match
      + expected - actual

      +"div, p {\n  color: red;\n  margin: 0;\n  padding: 0;\n}"
      -"div, p {\n  color: red;\n  margin: 20px;\n  padding: 0;\n}"

If we remove err.showDiffs here, here's what we get, which is the expected behavior. (Strange how the diff shows up with showDiffs off!)

$ mocha -C
... 
  1)  comparing strings:

      Error: Strings don't match
      + expected - actual

       div, p {
         color: red;
      +  margin: 0;
      -  margin: 20px;
         padding: 0;
       }
@boneskull
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment