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

Problems with using chai-as-promised in es6 #133

Closed
sztanko opened this issue Dec 10, 2015 · 9 comments

Comments

Projects
None yet
5 participants
@sztanko
Copy link

commented Dec 10, 2015

Hello! Thank you for creating this wonderful library!

I am trying to use it in my tests in ES6 and I am running into the following error:

Consider the following ES6 code:

import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';

chai.use(chaiAsPromised);

With the help of ./node_modules/babel-cli/bin/babel.js test/t.js that translates to the following code:

'use strict';

var _chai = require('chai');

var chai = _interopRequireWildcard(_chai);

var _chaiAsPromised = require('chai-as-promised');

var chaiAsPromised = _interopRequireWildcard(_chaiAsPromised);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

chai.use(chaiAsPromised);

But piping it to node ./node_modules/babel-cli/bin/babel.js test/feeds/t.js | node results in error:

./node_modules/chai/lib/chai.js:40
    fn(this, util);
    ^

TypeError: fn is not a function
    at Object.exports.use (./node_modules/chai/lib/chai.js:40:5)
    at [stdin]:13:6
    at Object.exports.runInThisContext (vm.js:54:17)
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:425:26)
    at node.js:587:27
    at doNTCallback0 (node.js:428:9)
    at process._tickCallback (node.js:357:13)

Can you please tell me what am I doing wrong?

@domenic

This comment has been minimized.

Copy link
Owner

commented Dec 10, 2015

Use require() instead of import. Chai as Promised does not support Babel modules, although it will support ES6 modules once they become a standard. If you want to make it work with Babel modules in the meantime, you'll need to ask the Babel folks for help, since it isn't a supported scenario here.

@domenic domenic closed this Dec 10, 2015

@sztanko

This comment has been minimized.

Copy link
Author

commented Dec 10, 2015

Actually it looks like I got it working in the following way:

import chaiAsPromised from 'chai-as-promised';
chai.use(chaiAsPromised);

Are you fine for me to add this to the documentation?

@domenic

This comment has been minimized.

Copy link
Owner

commented Dec 10, 2015

No, as I said, Babel modules are not a supported scenario.

@c0b

This comment has been minimized.

Copy link

commented Jul 31, 2016

I can verify that what @sztanko said works, why not telling more people to know?

import chaiAsPromised from 'chai-as-promised';
chai.use(chaiAsPromised);
@domenic

This comment has been minimized.

Copy link
Owner

commented Jul 31, 2016

Because it will likely be inaccurate when real ES6 modules land in Node. If you want documentation on how to use Babel, you need to go to the Babel website.

@c0b

This comment has been minimized.

Copy link

commented Jul 31, 2016

I'm using a convenience module

https://gitlab.com/c0b/promise-queue/blob/master/use-chai-as-promised.js

import chai from 'chai';
chai.should();
import chaiAsPromised from 'chai-as-promised';
chai.use(chaiAsPromised);

then in the actual testing code, I can call as import 'use-chai-as-promised.js', this is inspired from http://chaijs.com/guide/styles/#using-should-in-es2015 says can use import 'chai/should'; for a cleaner look, would you like a similar cleaner call for chai-as-promised ?

@domenic

This comment has been minimized.

Copy link
Owner

commented Jul 31, 2016

No; I find that cleaner looks that are not standards-compliant, but instead just what some transpiler decided on, have a habbit of causing problems in the future.

@djKianoosh

This comment has been minimized.

Copy link

commented Apr 6, 2017

How's this:

import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
chai.use(chaiAsPromised);
const should = chai.should();
const {expect, assert} = chai;

// then later on:
    it('should eventually return data', (done) => {
      somethingThatAsyncFetches().should.eventually.have.property('list').notify(done);;
    });
@colinskow

This comment has been minimized.

Copy link

commented Oct 20, 2017

With TypeScript 2.54 I had to do it like this to get it working:

import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';

chai.use(chaiAsPromised);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.