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/*
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";
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 toimport chaiString from "chai-string";(as opposed to the Chai import). However, the below example (with no transpilers) works fine for me when runningnode --experimental-moduleswith [email protected], so I dunno. I'd guess OP's issue is related to transpiling.