简体   繁体   English

Jest、Babel 和 Storybook,export 与 module.exports

[英]Jest, Babel and Storybook, export vs module.exports

I am using Jest to test my services and Storybook and Babel to develop my React components.我使用 Jest 来测试我的服务,使用 Storybook 和 Babel 来开发我的 React 组件。

I have a service which my component needs to use.我有我的组件需要使用的服务。 I currently export with module.exports = someFunction;我目前使用module.exports = someFunction;导出module.exports = someFunction; and it passes my Jest unit tests, but storybook tells me Cannot assign to read only property 'exports' of object '#<Object>' in the browser console.它通过了我的 Jest 单元测试,但故事书告诉我Cannot assign to read only property 'exports' of object '#<Object>'浏览器控制台Cannot assign to read only property 'exports' of object '#<Object>'

If I change the service to export default someFunction , the component works but the Jest unit test fails with SyntaxError: Unexpected token export .如果我将服务更改为export default someFunction ,则该组件可以工作,但 Jest 单元测试失败并显示SyntaxError: Unexpected token export

What do I do to make both work?我该怎么做才能使两者都起作用?

Notes:笔记:

  • I don't care whether I consistently export with module.exports or export .我不在乎我是否始终使用module.exportsexport I have no real preference at this time.目前我没有真正的偏好。
  • I'm prepared to use a different unit-testing library, but I wasn't happy with Ava last time, and I don't remember having any luck with unit testing software and babel.我准备使用不同的单元测试库,但上次我对 Ava 不满意,而且我不记得在单元测试软件和 babel 上有任何运气。
  • Last time I was using Babel I didn't have a problem with module.exports .上次我使用 Babel 时,我没有module.exports的问题。 Would a downgrade help?降级会有帮助吗? If so, what would I downgrade to?如果是这样,我会降级到什么?

Solved by adding a file called babel.config.js with the contents:通过添加一个名为babel.config.js的文件来解决:

module.exports = {
  presets: ['@babel/env']
};

If anyone has a better answer or believes they can educate me in the numerous areas I'm sure I'm shortcoming, they are welcome to do so.如果有人有更好的答案或相信他们可以在我确信我有缺点的众多领域对我进行教育,欢迎他们这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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