Chai: Add an ES6-friendly way to register "should"

Created on 21 Jan 2016  路  7Comments  路  Source: chaijs/chai

I suggest adding a file named should.js at the root of this project that auto-registers 'should', i.e. require('./index').should();.

/* CURRENT SITUATION */

  // commonjs style looks nice
  require('chai').should();

  // but es6 isn't so nice
  import chai from 'chai';
  chai.should();

  // or you can do this, but it's still 2 lines
  import {should} from 'chai';
  should();

/* PROPOSAL */

  // single-line way to register 'should' in ES6
  import `chai/should`;

  // nb. other libraries use this convention too
  import 'babel-core/register';
  import 'coffee-script/register';

Note: people sometimes think side-effecting imports feel dirty, but it's arguably less dirty than a globally side-effecting function call like .should(). When you have a 'fromless' import that doesn't assign any variables, the syntax is explicitly intended for side-effects.

feature-requests fun-size pull-request-wanted

Most helpful comment

After combing the pr's I found this and it worked.

import "chai/register-should"

Maybe the documentation needs to be updated later.

All 7 comments

Something I'd like to address in the roadmap (https://github.com/chaijs/chai/issues/457). Not sure what the right way to address this is though, as each has its downsides, especially when using plugins:

/* CURRENT SITUATION */

  // commonjs style looks nice
  require('chai').use(require('some-plugin')).should();

  // but es6 isn't so nice
  import chai from 'chai';
  import somePlugin from 'some-plugin';
  chai.use(somePlugin).should();

  // or you can do this, but it's still 2 lines
  import chai, {should} from 'chai';
  import somePlugin from 'some-plugin';
  chai.use(somePlugin);
  should();

/* PROPOSAL */

  // single-line way to register 'should' in ES6
  import chai from 'chai';
  import somePlugin from 'some-plugin';
  chai.use(somePlugin);
  import `chai/should`;

  // nb. other libraries use this convention too
  import 'babel-core/register';
  import 'coffee-script/register';

I suppose keeping chai.should(), chai.expect, chai.assert would be fine, but adding import 'chai/should' would help for expressiveness for those who don't use plugins. So the trifecta becomes:

// Plugin-less
import { expect } from 'chai'
import { assert } from 'chai'
import 'chai/should'
// if you need should.exist/should.fail etc:
import should from 'chai/should'

// With plugins
import chai from 'chai';
import somePlugin from 'some-plugin';
const expect = chai.use(somePlugin).expect

import chai from 'chai';
import somePlugin from 'some-plugin';
const assert = chai.use(somePlugin).assert

import chai from 'chai';
import somePlugin from 'some-plugin';
const should = chai.use(somePlugin).should()

@callumlocke how do you feel about making a PR for this? It should simply be a case of creating a file named should.js at the top level, which has these contents:

module.exports = require('./').should()

I'm getting
Error: Cannot find module 'chai/should'

when using this syntax (chai 3.5.0)

@jonaswindey this should be released as part of 4.0.0, which will be out soon 馃槃

Well the documentation seems to be updated to the latest syntax. Will need to wait for 4.0.0 release then.
http://chaijs.com/guide/styles/#should

After combing the pr's I found this and it worked.

import "chai/register-should"

Maybe the documentation needs to be updated later.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danthegoodman picture danthegoodman  路  3Comments

AnAppAMonth picture AnAppAMonth  路  3Comments

xareelee picture xareelee  路  3Comments

JuHwon picture JuHwon  路  5Comments

endymion00 picture endymion00  路  3Comments