chai-changes is an extension to the chai assertion library that provides a set of change-specific assertions.
All assertions use a
expect-> codeThatYieldsAChangedResultto...when ->executeTheCodeThatCausesTheChange
The code within the
expect section will be executed first, then the
code in the
when section will be executed and then the code in the
expect section will be executed again and the differences will be
Same test using 'should':
-> codeThatYieldsAChangedResultshould...when ->executeTheCodeThatCausesTheChange
Executes the build up pre- and post-conditions. First it executes the pre-conditions, then it will execute the provided callback. And after the callback it will assert the post-conditions. It will change the object in the assertion chain to the result of the callback.
When the callback returns a promise, the post-conditions are executed when the promise is fulfilled.
when statement will return an assertion promise when the result of
when block is a promise. To use in mocha, you can specify an
notify key to trigger
expect-> valuetochangewhen-> promisenotify: done
if you use mocha-as-promised, you don't need to specify this notify method
an alternative would be:
expect-> valuetochangewhen-> promisethen-> donedoneerror
Assert if the 'expect/should' changes its outcome when 'when' is executed
result = 0-> resultshouldchangewhen -> result += 1expect-> resulttochangewhen -> result -= 1expect-> resultnottochangewhen -> result = result * 1
Assert if the change of the 'expect/should' has the provided delta
result = 0-> resultshouldchangeby3when -> result += 3expect-> resultnottochangeby-3when -> result += 1expect-> resulttochangeby-2when -> result -= 2
Assert if the change starts from a certain value. The value is compared using a deep equal.
result = 'a''b'-> resultshouldchangefrom'a''b'when -> resultpush'c'-> resultshouldchangefrom'a''b'to'a''b''c'when -> resultpush'c'
Assert if the change ends in a certain value. The value is compared using a deep equal.
result = 'a''b'-> resultshouldchangeto'a''b''c'when -> resultpush'c'-> resultshouldchangefrom'a''b'to'a''c'when -> result = 'a''c'
Assert if the value increases when an action is performed
result = 0expect-> resulttoincreasewhen -> result += 1expect-> resultnottoincreasewhen -> resultexpect-> resultnottoincreasewhen -> result -= 1
Assert if the value decreases when an action is performed
result = 0expect-> resulttodecreasewhen -> result -= 1expect-> resultnottodecreasewhen -> resultexpect-> resultnottodecreasewhen -> result += 1
Assert if the value has a minimal change of 'amount'
result = 0expect-> resulttochangebyatLeast4when -> result += 5expect-> resulttochangebyatLeast4when -> result -= 10
Assert if the value has a maximum change of 'amount'
result = 0expect-> resulttochangebyatMost7when -> result += 5expect-> resulttochangebyatMost14when -> result -= 10
Installation and Setup
npm install chai-changes to get up and running. Then:
var chai = ;var chaiChanges = ;chai;
You can of course put this code in a common test fixture file; for an example using Mocha
Chai Changes supports being used as an AMD module, registering itself anonymously (just like Chai). So,
assuming you have configured your loader to map the Chai and Chai Changes files to the respective module IDs
"chai-changes", you can use them as follows:
If you include Chai Changes directly with a
<script> tag, after the one for Chai itself, then it will
automatically plug in to Chai and be ready for use:
Copyright (c) 2012 Matthijs Groen
MIT License (see the LICENSE file)