Skip to content

Releases: chaijs/chai

4.0.0-canary.1

24 Oct 22:09
Compare
Choose a tag to compare
4.0.0-canary.1 Pre-release
Pre-release

4.0.0-canary.1

Breaking Changes

  • Instead of allowing the user to write the path of a property, now the deep flag performs a deep equality comparison when used with the .property assertion.
    If you want the old behavior of using the dot or bracket notation to denote the property you want to assert against you can use the new .nested flag. (Related Issues: #745, #743, PRs: #758, #757)

    const obj = {a: 1};
    
    // The `.deep` flag now does deep equality comparisons
    expect({foo: obj}).to.have.deep.property('foo', {a: 1});
    
    // Use the `nested` flag if you want to assert against a nested property using the bracket or dot notation
    expect({foo: obj}).to.have.nested.property('foo.a', 1);
    
    // You can also use both flags combined
    const obj2 = {a: {b: 2}};
    expect({foo: obj2}).to.have.deep.nested.property('foo.a', {b: 2});

    Please notice that the old methods which used the old behavior of the deep flag on the assert interface have been renamed. They all have had the deep word changed by the nested word. If you want to know more about this please take a look at #757.

  • Previously, expect(obj).not.property(name, val) would throw an Error if obj didn't have a property named name. This change causes the assertion to pass instead.
    The assert.propertyNotVal and assert.deepPropertyNotVal assertions were renamed to assert.notPropertyVal and assert.notDeepPropertyVal, respectively. (Related Issues: #16, #743, #758)

  • You can now use the deep flag for the .include assertion in order to perform a deep equality check to see if something is included on the target.
    Previously, .include was using strict equality (===) for non-negated property inclusion, but deep equality for negated property inclusion and array inclusion.
    This change causes the .include assertion to always use strict equality unless the deep flag is set.
    Please take a look at this comment if you want to know more about it. (Related Issues: #743, PRs: #760, #761)

    const obj = {a: 1};
    expect([obj]).to.deep.include({a:1});
    expect({foo: obj}).to.deep.include({foo: {a:1}});
  • Fix unstable behavior of the NaN assertion. Now we use the suggested ES6 implementation.
    The new implementation is now more correct, strict and simple. While the old one threw false positives, the new implementation only checks if something is NaN (or not if the .not flag is used) and nothing else. (Related Issues: #498, #682, #681, PRs: #508)

    // Only `NaN` will be considered to be `NaN` and nothing else
    expect(NaN).to.be.NaN;
    
    // Anything that is not `NaN` cannot be considered as `NaN`
    expect('randomString').not.to.be.NaN;
    expect(true).not.to.be.NaN;
    expect({}).not.to.be.NaN;
    expect(4).not.to.be.NaN;
  • Throw when calling _superon overwriteMethodif the method being overwritten is undefined.
    Currently if the method you are trying to overwrite is not defined and your new method calls _super it will throw an Error.(Related Issues: #467, PRs: #528)
    Before this change, calling _super would simply return this.

    // Considering the method `imaginaryMethod` does not exist, this would throw an error for example:
    chai.use(function (chai, utilities) {
      chai.Assertion.overwriteMethod('imaginaryMethod', function (_super) {
        return function () {
          _super.apply(this, arguments);
        }
      });
    });
    
    // This will throw an error since you are calling `_super` which should be a method (in this case, the overwritten assertion) that does not exist
    expect('anything').to.imaginaryMethod(); 
  • Now showDiff is turned on by default whenever the showDiff flag is anything other than false.
    This issue will mostly affect plugin creators or anyone that made extensions to the core, since this affects the Assertion.assert method. (Related Issues: #574, PRs: #515)

    // Now whenever you call `Assertion.assert` with anything that is not false for the `showDiff` flag it will be true
    // The assertion error that was thrown will have the `showDiff` flag turned on since it was not passed to the `assert` method
    try {
      new chai.Assertion().assert(
          'one' === 'two'
        , 'expected #{this} to equal #{exp}'
        , 'expected #{this} to not equal #{act}'
        , 'one'
        , 'two'
      );
    } catch(e) {
      assert.isTrue(e.showDiff);
    }
    
    // The assertion error that was thrown will have the `showDiff` flag turned off since here we passed `false` explicitly
    try {
      new chai.Assertion().assert(
          'one' === 'two'
        , 'expected #{this} to equal #{exp}'
        , 'expected #{this} to not equal #{act}'
        , 'one'
        , 'two'
        , false
      );
    } catch(e) {
      assert.isFalse(e.showDiff);
    }
  • The Typed Array types are now truncated if they're too long (in this case, if they exceed the truncateThreshold value on the config). (Related Issues: #441, PRs: #576)

    var arr = [];
    for (var i = 1; i <= 1000; i++) {
      arr.push(i);
    }
    
    // The assertion below will truncate the diff shown and the enourmous typed array will be shown as:
    // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...  ] instead of printing the whole typed array
    chai.expect(new Float32Array(100)).to.equal(1);
  • The assertions: within, above, least, below, most, increase, decrease will throw an error if the assertion's target or arguments are not numbers. (Related Issues: #691, PRs: #692, #796)

    // These will throw errors, for example:
    expect(null).to.be.within(0, 1);
    expect(null).to.be.above(10);
    expect(null).to.be.at.least(20);
    expect(null).to.be.below(20);
    expect(null).to.be.at.most(20);
    expect(null).to.increase.by(20);
    expect(null).to.decrease.by(20);
    
    // This will not:
    expect('string').to.have.a.length.of.at.least(3);
  • Previously, expect(obj).not.ownProperty(name, val) would throw an Error if obj didn't have an own property (non-inherited) named name. This change causes the assertion to pass instead. (Related Issues: #795, #, PRs: #744, #810)*

    expect({ foo: 'baz' }).to.not.have.own.property('quux', 'baz');
  • The .empty assertion will now throw when it is passed non-string primitives and functions (PRs: #763, #812)

    // These will throw TypeErrors:
    expect(Symbol()).to.be.empty;
    expect(function() {}).to.be.empty;
    expect(true).to.be.empty;
    expect(1).to.be.empty
  • Assertion subject (obj) changes when using ownProperty or own.property and thus enables chaining. (Related Issues: #281, PRs: #641)

    expect({val: 20}).to.have.own.property('val').above(10);
  • The utils (second argument passed to the chai.use callback function) no longer exports the getPathValue function. If you want to use that please use the pathval module, which is what chai uses internally now. (Related Issues: #457, #737, PRs: #830)

New Features

  • Throw when non-existent property is read. (Related Issues: #407, #766 PRs: #721, #770)
    This is a potentially breaking change. Your build will fail if you have typos in your property assertions
    Before 4.x.x when using property assertions they would not throw an error if you wrote it incorrectly.
    The example below, for example, would pass:

    expect(true).to.be.ture; // Oops, typo, now Chai will throw an Error

    Since this implementation depends on ES6 Proxies it will only work on platforms that support it.

    This property can be enabled (default) or disabled through the config.useProxy property, for example:

    chai.config.useProxy = false;  // disable use of Proxy
  • Add fix suggestions when accessing a nonexistent property in proxy mode. (Related Issues: #771, PRs: #782)
    When a nonexistent property is accessed in proxy mode, Chai will compute the levenshtein distance to all possible properties in order to suggest the best fix to the user.

    expect(false).to.be.fals; // Error: Invalid Chai property: fals. Did you mean "false"?
    expect('foo').to.be.undefind; // Error: Invalid Chai property: undefind. Did you mean "undefined"?
    
    // If the Levenshtein distance between the word and any Chai property is greater than 4, no fix will be suggested
    expect('foo').to.be.fdsakfdsafsagsadgagsdfasf // error thrown, no fix suggested
  • When non-chainable methods (including overwritten non-chainable methods) are used incorrectly an error will be thrown with a helpful error message. (PRs: #789)

    expect(true).to.equal.true;  // Invalid Chai property: equal.true. See docs for proper usage of "equal".
  • Add a new configuration setting that describes which keys will be ignored when checking for non-existing properties on an assertion before throwing an error.
    Since this implementation depends on ES6 Proxies it will only work on platforms that support it. Also, if you disable config.useProxy, this setting will have no effect. *(Related Issues: #765, PRs: #774)

    chai.config.proxyExcludedKeys.push('nonExistingProp');
    
    expect('my string').to.nonExistingProp; // This won't throw an error now
  • Add script that registers should as a side-effect. (Related Issues: #594, #693 PRs: #604)

    // You can now write:
    import 'chai/should';
    
    // as opposed to:
    import {should} from 'chai';
    should();

    You can also register should via a mocha option: mocha --require chai/should.

  • The change assertion accepts a function as object. (Related Issues: #544, PRs: #607)

    // Now you can also check if the return value of a function changes, for example
    assert.increases(
      someOperation,
      () => getSomething().length
    )
  • You can ...

Read more

3.5.0 / 2016-01-28

28 Jan 12:02
Compare
Choose a tag to compare

For assert fans: you now have assert.includeDeepMembers() which matches expect().to.include.deep.members() and .should.include.deep.members()!

This release also includes a variety of small bugfixes and documentation fixes. Most notably, we are now governed by a Code Of Conduct - which gives Chai contributors (including those who files issues, work on code, or documentation, or even just hang out on our Slack & Gitter channels) safety from harassment and discrimination.

Full changes below:

Community Contributions

Documentation fixes

3.4.2 / 2015-11-15

15 Nov 22:50
Compare
Choose a tag to compare

This is a small documentation bug fix release - it adds extra metatags to each public method to allow us to generate the docs easier for the website.

Community Contributions

Documentation fixes

3.4.1 / 2015-11-07

07 Nov 23:22
Compare
Choose a tag to compare

This is a small documentation bug fix release - it just fixes a couple of issues with the documentation.

Community Contributions

Documentation fixes

3.4.0 / 2015-10-21

21 Oct 11:12
Compare
Choose a tag to compare

This release improves some confusing error messages, and adds some new assertions. Key points:

  • Feature: New assertion: expect(1).oneOf([1,2,3]) - for asserting that a given value is one of a set.
  • Feature: .include() (and variants) will now give better error messages for bad object types. Before expect(undefined).to.include(1) would say "expected undefined to include 1", now says "object tested must be an array, an object, or a string but undefined given"
  • Feature: .throw() (and variants) can now better determine the Error types, for example expect(foo).to.throw(node_assert.AssertionError) now works.
  • Feature: .closeTo is now aliased as .approximately
  • BugFix: .empty changes from 3.3.0 have been reverted, as they caused breaking changes to arrays which manually set keys.

Community Contributions

Code Features & Fixes

  • #503 Checking that argument given to expect is of the right type when using with include. By @astorije
  • #446 Make chai able to cope with AssertionErrors raised from node's assert. By @danielbprice
  • #527 Added approximately alias to close to. By @danielbprice
  • #534 expect(inList).to.be.oneOf assertion. By @Droogans
  • #538 Revert .empty assertion change from PR #499. By @tusbar

Documentation fixes

3.3.0 / 2015-09-08

11 Sep 21:42
Compare
Choose a tag to compare

This release adds some new assertions and fixes some quite important, long standing bugs. Here are the cliff notes:

  • Bugfix: Property assertions that fail now show a stack trace!
  • Bugfix: If you've used the frozen, sealed or extensible assertions to test primitives (e.g. expect(1).to.be.frozen), you may have noticed that in older browsers (ES5) these fail, and in new ones (ES6) they pass. They have now been fixed to consistently pass
  • The assert interface has been given the following new methods, to align better with other interfaces:, assert.isAtMost, assert.isAtLeast, assert.isNotTrue, assert.isNotFalse.

Community Contributions

Code Features & Fixes

Documentation fixes

3.2.0 / 2015-07-19

19 Jul 17:02
Compare
Choose a tag to compare

This release fixes a bug with the previous additions in 3.1.0. assert.frozen/expect().to.be.frozen/.should.be.frozen all accidentally called Object.isSealed() instead. Now they correctly call Object.isFrozen().

If you're using these features, please upgrade immediately.

It also adds aliases for a lot of assert methods:

  • expect/should..respondTo: respondsTo
  • expect/should..satisfy: satisfies
  • assert.ok: assert.isOk
  • assert.notOk: assert.isNotOk
  • assert.extensible: assert.isExtensible
  • assert.notExtensible: assert.isNotExtensible
  • assert.sealed: assert.isSealed
  • assert.notSealed: assert.isNotSealed
  • assert.frozen: assert.isFrozen
  • assert.notFrozen: assert.isNotFrozen

Community Contributions

Code Features & Fixes

3.1.0 / 2015-07-16

16 Jul 21:06
Compare
Choose a tag to compare

This release adds assertions for extensibility/freezing/sealing on objects:

assert.extensible({});
assert.notExtensible(Object.preventExtensions({}));
expect(Object.preventExtensions({})).to.not.be.extensible;
Object.preventExtensions({}).should.not.be.extensible;

assert.notSealed({});
assert.sealed(Object.seal({}));
expect(Object.seal({})).to.be.sealed;
Object.seal({}).should.be.sealed;

assert.notFrozen({});
assert.frozen(Object.freeze({}));
expect(Object.freeze({})).to.be.frozen;
Object.freeze({}).should.be.frozen;

It also adds assertions for checking if a number is NaN:

assert.isNaN(NaN);
assert.isNotNaN(5);
expect(NaN).to.be.NaN;
expect(5).to.not.be.NaN;
NaN.should.be.NaN;
5.should.not.be.NaN;

Community Contributions

Code Features & Fixes

3.0.0 / 2015-06-04

03 Jun 23:27
Compare
Choose a tag to compare

This release contains some small breaking changes. Most people are unlikely to
notice them - but here are the breaking changes:

  • Switched from "Component" builds to "Browserify" builds for Browsers. If
    you're using RequireJS with Chai, you might notice a change (chai used to
    be a global, now it won't be - instead you'll have to require it).
  • .has.property('foo', undefined) will now specifically test to make sure
    that the value 'foo' is actually undefined - before it simply tested that
    the key existed (but could have a value).
  • .to.be.a('type') has changed to support more types, including ES6 types
    such as 'promise', 'symbol', 'float32array' etc, this also means using
    ES6's Symbol.toStringTag affects the behaviour .to.be.a(). In addition to
    this, Errors have the type of 'error'.
  • assert.ifError() now follows Node's assert.ifError() behaviour - in other
    words, if the first argument is truthy, it'll throw it.
  • ReleaseNotes.md is no longer maintained, see the Github Releases Page instead.
  • History.md is no longer maintained, see the Github Commit Log instead.

Community Contributions

Code Features & Fixes

Documentation fixes

2.3.0 / 2015-04-26

26 Apr 16:21
Compare
Choose a tag to compare

Added ownPropertyDescriptor assertion:

expect('test').to.have.ownPropertyDescriptor('length');
expect('test').to.have.ownPropertyDescriptor('length', { enumerable: false, configurable: false, writable: false, value: 4 });
expect('test').not.to.have.ownPropertyDescriptor('length', { enumerable: false, configurable: false, writable: false, value: 3 });
expect('test').ownPropertyDescriptor('length').to.have.property('enumerable', false);
expect('test').ownPropertyDescriptor('length').to.have.keys('value');

Community Contributions

Code Features & Fixes

Documentation fixes