Skip to content

Instantly share code, notes, and snippets.

@thieux
Last active March 26, 2019 13:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thieux/ea2f936473bf438072898d34244e5352 to your computer and use it in GitHub Desktop.
Save thieux/ea2f936473bf438072898d34244e5352 to your computer and use it in GitHub Desktop.

Chai Assertion Library

Create a workspace so that chai will be visible by node REPL.

~/workspace/test/node-chai$ npm install chai
~/workspace/test/node-chai$ node

Import Chai:

> var expect = require('chai').expect, foo = 'bar', beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
undefined

Assert on type:

> expect(foo).to.be.a('string')
Assertion {
  __flags: { ssfi: [Function: assert], object: 'bar', message: undefined } }

It's OK.

Let's, see how Chai reports errors.

Bad type:

> expect(foo).to.be.a('int')
AssertionError: expected 'bar' to be an int
    at repl:1:19
    at REPLServer.defaultEval (repl.js:252:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:417:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:211:10)
    at REPLServer.Interface._line (readline.js:550:8)
    at REPLServer.Interface._ttyWrite (readline.js:827:14)

Undefined variable:

> expect(bazbaz).to.be.a('string')
ReferenceError: bazbaz is not defined
    at repl:1:8
    at REPLServer.defaultEval (repl.js:252:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:417:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:211:10)
    at REPLServer.Interface._line (readline.js:550:8)
    at REPLServer.Interface._ttyWrite (readline.js:827:14)

Another syntax (to is optional):

> expect(foo).be.a('string')
Assertion {
  __flags: { ssfi: [Function: assert], object: 'bar', message: undefined } }

But a is required:

> expect(foo).be('string')
TypeError: expect(...).be is not a function
    at repl:1:13
    at REPLServer.defaultEval (repl.js:252:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:417:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:211:10)
    at REPLServer.Interface._line (readline.js:550:8)
    at REPLServer.Interface._ttyWrite (readline.js:827:14)`

Another way with equal:

> expect(foo).to.equal('bar')
Assertion {
  __flags: { ssfi: [Function], object: 'bar', message: undefined } }

Check string length:

> expect(foo).to.have.length(3)
Assertion {
  __flags: 
   { ssfi: [Function: assert],
     object: 'bar',
     message: undefined,
     doLength: true } }

Extract properties:

> expect(beverages).to.have.property('tea').with.length(3)
Assertion {
  __flags: 
   { ssfi: [Function: assert],
     object: [ 'chai', 'matcha', 'oolong' ],
     message: undefined,
     doLength: true } }

When it fails:

> expect(beverages).to.have.property('tea').with.length(2)
AssertionError: expected [ 'chai', 'matcha', 'oolong' ] to have a length of 2 but got 3
    at repl:1:48
    at REPLServer.defaultEval (repl.js:252:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:417:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:211:10)
    at REPLServer.Interface._line (readline.js:550:8)
    at REPLServer.Interface._ttyWrite (readline.js:827:14)
@sam-aldis
Copy link

ping

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