Search code examples
javascriptmocha.jschai

How to correct a function error, TypeError 'flattenArray' is not a function in Mocha


I am learning basic testing in with Mocha and Chai. My test keeps failing with the error, "TypeError: flattenArray is not a function". Here is the function code...flattenArray.js

 export function flattenArray(arr){
 let result = [];
   arr.forEach(function(element) {
      if(!Array.isArray(element)) {
       result.push(element);
      } else {
        result = result.concat(flattenArrays(element));
      }
    });
    return result;
  }

and here is my test file, flattenArray.spec.js

var chai = require('chai');
var assert = require('chai').assert;
var expect = require('chai').expect;
var describe = require('mocha').describe;
import * as flattenArray from '../src/flattenArray';


describe('Array', function() {
  describe('#flattenArray()', function() {
    it('should return a single, flat array', function(){
      expect(flattenArray([1,2,3])).to.be.equal([1,2,3])
   })
  })
})



describe('Array', function() {
  it('should start empty', function() {
    var arr = [];
    assert.equal(arr.length, 0);
  });
});

What am I doing wrong? I have one passing and one failing. How do I get the flattenArray test to work?


Solution

  • The function flattenArray() is a named export. You should import it like this:

    import { flattenArray } from '../src/flattenArray';