Jest: Cannot use export default in module

Created on 22 Jun 2017  路  5Comments  路  Source: facebook/jest

Apologies if this is a setup question, and not a bug, but it feels like a bug so...

I'm having a weird issue where Jest does not seem to work when using export default on a module. I've created a sample repo to reproduce the issue: https://github.com/syropian/jest-test

jest version: ^20.0.4
babel-jest version: ^20.0.3

The gist of it is, given this module:

const a = {
  greet () {
    return 'Hello World'
  }
}

const b = {
  foo () {
    return 'bar'
  }
}

export default {
  a,
  b
}

This works:

import myModule from '../src'

describe('My module', () => {
  it('works if I import the whole module', () => {
    const greeting = myModule.a.greet()
    const foo = myModule.b.foo()
    expect(greeting).toBe('Hello World')
    expect(foo).toBe('bar')
  })
})

but this results in a and b being undefined:

import { a, b } from '../src'

describe('My module', () => {
  it('works if I import individual exports', () => {
    const greeting = a.greet()
    const foo = b.foo()
    expect(greeting).toBe('Hello World')
    expect(foo).toBe('bar')
  })
})

Using node 7.3.0 and npm 5.0.0. Also tried node 8.0.0 with no luck.

Most helpful comment

It's not valid ESM. You export an object with two props, not different exports.

If it works in AVA, that's a bug there.

All 5 comments

export const a = {
  greet: () => 'hello world',
};

export const b = {
  foo: () => 'bar',
};

export default function throwThat(that) {
  throw new Error(that);
}


// other file
import throwMessage, { a, b } from '../';

@etrnljg I know that's a workaround, but it does not explain why I can't pull in individual pieces from the default export. It works fine in other testing frameworks like Ava.

It's not valid ESM. You export an object with two props, not different exports.

If it works in AVA, that's a bug there.

I'm not sure you can destructure an object on an import.

Edit: yeah, exactly what @SimenB said.

@etrnljg @SimenB Oh you're right, I had to check the MDN on exports. I can optionally do:

export { a, b }

if I know I only want to pull in the individual exports. I did not realize it didn't work with default exports, since it had always worked with AVA. TIL!

Was this page helpful?
0 / 5 - 0 ratings