WTF WTF - 7 months ago 25
Javascript Question

Exported Function isn't a Function?

Not sure why it's not viewing this as a function:

impl.js

export default function(callback){
return callback();
};


test.js

import {myModule} from '../../src/impl.js'
import {expect} from 'chai';

const module = myModule;

describe('', () => {

it('should callback when resolve is invoked', () => {
module(resolve => {
resolve('test');
}).then(value => {
expect(value).to.equal('test');
});
});

});


Error:
TypeError: module is not a function

Answer

module isn't a function because where it receives its value, myModule, also isn't a function.

And, that is because you aren't using quite the correct syntax to import the export default. For that, you'll want to remove the braces around myModule:

import myModule from '../../src/impl.js'

From MDN:

Syntax

import *defaultMember* from *"module-name"*;


With the braces, the import will match a particular export by its name.

import { myModule } from '...';

Corresponds to either:

export let myModule = ...;
export function myModule() { ... };

And, impl.js doesn't export anything actually named myModule.