简体   繁体   中英

export { default } from module does not work with Babel 7 and preset-env in Jest

After migrating to Babel 7 when I launch my tests I get this error

TypeError: Cannot read property 'default' of undefined

for this line (eg index.js file):

export { default } from './SearchInput';

Where SearchInput.jsx file is in the same folder and is using

export class SearchInput {}

syntax.

This happens only in Jest tests (in app runtime it works).

How can I fix it?

EDIT:

This is my babel config babel.config.js :

module.exports = function(api) {
  api.cache.forever();
  return {
    presets: ['@babel/preset-env', '@babel/preset-react'],
    plugins: ['@babel/plugin-proposal-class-properties'],
  };
};

It seems that jest does not respect plugins , cos when I removed static defaultProps and static propTypes and moved them to SearchInput.defaultProps and SearchInput.propTypes , tests started to work.

You probably forgot to upgrade babel-jest as well.

Note: If you are using babel version 7 you have to install babel-jest with

 yarn add --dev babel-jest 'babel-core@^7.0.0-bridge' @babel/core 

https://github.com/facebook/jest/tree/master/packages/babel-jest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM