Chai: fn is not a function

Created on 9 Jan 2018  路  4Comments  路  Source: chaijs/chai

I've simple test that is as follows

// abc.test.ts
const Client = require('node-rest-client').Client;
import Nightmare from "nightmare";
import * as chai from "chai";
import chaiString from "chai-string";
chai.use(chaiString);  
//node_modules\chai\lib\chai.js:39
//    fn(exports, util);
//   ^
// TypeError: fn is not a function

The npm command is as follows
mocha --recursive --require jsdom-global/register --require ts-node/register --require babel-core/register tests/domains/*

Most helpful comment

@gary1410 It's difficult to troubleshoot OP's issue because they're using a couple of transpilers. What I can tell you is that "TypeError: fn is not a function" is saying that the value being passed to chai.use() isn't a function, suggesting that the problem is related to import chaiString from "chai-string"; (as opposed to the Chai import). However, the below example (with no transpilers) works fine for me when running node --experimental-modules with [email protected], so I dunno. I'd guess OP's issue is related to transpiling.

import chai from "chai";
import chaiString from "chai-string";

chai.use(chaiString);

chai.expect("blah").to.startWith("bl"); // Passes
chai.expect("blah").to.startWith("xl"); // Throws AssertionError

All 4 comments

closing as chai works with require statement.

What's your solution here - I'm getting the same error too, but there needs to be a better way than to use require if all your code is es6.

@gary1410 It's difficult to troubleshoot OP's issue because they're using a couple of transpilers. What I can tell you is that "TypeError: fn is not a function" is saying that the value being passed to chai.use() isn't a function, suggesting that the problem is related to import chaiString from "chai-string"; (as opposed to the Chai import). However, the below example (with no transpilers) works fine for me when running node --experimental-modules with [email protected], so I dunno. I'd guess OP's issue is related to transpiling.

import chai from "chai";
import chaiString from "chai-string";

chai.use(chaiString);

chai.expect("blah").to.startWith("bl"); // Passes
chai.expect("blah").to.startWith("xl"); // Throws AssertionError

@meeber posts the solution. specifically, if you are using es-module style imports you MUST import chai things like:

//works
import chai from "chai";
import chaiString from "chai-string";

//does not work
import * as chai from "chai";
import * as chaiString from "chai-string";
Was this page helpful?
0 / 5 - 0 ratings