Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ES6-friendly way to register "should" #594

Closed
callumlocke opened this issue Jan 21, 2016 · 7 comments
Closed

Add an ES6-friendly way to register "should" #594

callumlocke opened this issue Jan 21, 2016 · 7 comments

Comments

@callumlocke
Copy link
Contributor

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.

@keithamus
Copy link
Member

Something I'd like to address in the roadmap (#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';

@keithamus
Copy link
Member

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()

@keithamus
Copy link
Member

@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()

@jonaswindey
Copy link

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

when using this syntax (chai 3.5.0)

@keithamus
Copy link
Member

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

@pyrossh
Copy link

pyrossh commented May 13, 2017

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

@pyrossh
Copy link

pyrossh commented May 13, 2017

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

import "chai/register-should"

Maybe the documentation needs to be updated later.

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

No branches or pull requests

4 participants