1. chai-diff
Adds expectations to chai which compare strings or objects using diff.
chai-diff
Package: chai-diff
Created by: logi
Last modified: Mon, 13 Jun 2022 05:50:54 GMT
Version: 1.0.2
License: LGPL-3.0
Downloads: 23,091
Repository: https://github.com/logi/chai-diff

Install

npm install chai-diff
yarn add chai-diff

#chai-diff

Adds expectations to chai which compare strings or objects using jsdiff.

 expect(value).differentFrom(another, options)
expect(value).not.differentFrom(another, options)
expect(value).to.be.differentFrom(another, options)
expect(value).not.to.be.differentFrom(another, options)

This will compare two strings or convert two objects to JSON strings and then compare them. One oddity is that the base form expects the objects to be different and you will generally use the .not.differentFrom form.

The options object is optional and can have the following options:

  • showSpace [false] Whether to convert whitespace to visible Unicode characters in output.
  • relaxedSpace [false] Whether to normalise whitespace before comparing strings. This:
    • removes all leading whitespace
    • removes all trailing whitespace
    • replaces all sequences of whitespace with a single space
    • removes any empty lines
  • context [10] How many lines of context to show on each side of a difference

Installation in Node (CommonJS)

 npm install chai-diff --save-dev

In your specs, add:

 chai.use(require('chai-diff'));

See chai-diff.spec.js for an example

Installation in RequireJS (AMD)

Download the chai-diff.js file and add it to your main. Then in your test do something like:

 define(['chai', 'chai-diff', ...], function(chai, chaiDiff, ...) {
  chai.use(chaiDiff);

  describe(...);

});

Installation in browsers

Finally, if neither CommonJS nor AMD is detected, a global varialbe chaiDiff is created and can be used with:

 <script src="chai.js"></script>
<script src="diff.js"></script>
<script src="chai-diff.js"></script>
<script>
    chai.use(chaiDiff);
    ...
</script>

See test.html for an example

Why?

This is useful if:

  • You need to ignore whitespace in string comparisons
  • Your test runner doesn't show useful differences on failures.

TODO

Further improvements:

  • More options for which whitespace to ignore

Dependencies

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin