首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >moment-timezone.js -在Jest测试中运行时出现错误

moment-timezone.js -在Jest测试中运行时出现错误
EN

Stack Overflow用户
提问于 2017-12-12 19:58:06
回答 0查看 1.5K关注 0票数 7

我在使用moment-timezone.js时出错。它在网页上运行得很好,但是当我尝试对它进行测试时,测试结果总是返回如下所示的错误。

这是我在网页上使用的代码:

代码语言:javascript
运行
复制
import moment from 'moment-timezone';

class TimezoneCityItem extends React.Component {

  componentDidMount(){
    this.setState({
      time: moment.tz(this.props.timezone)
    })
  }

  render(){
    return (
      <div>{this.state.time.format('HH:mm')}</div>
    )
  }
}

这是timezoneListDummyData

代码语言:javascript
运行
复制
const timezoneList = [
  { name: 'los-angeles', title: 'Los Angeles', timezone: 'America/Los_Angeles' },
  { name: 'washington', title: 'Washington', timezone: 'America/New_York' },
  { name: 'london', title: 'London', timezone: 'Europe/London' },
  { name: 'dubai', title: 'Dubai', timezone: 'Asia/Dubai' },
  { name: 'hongkong', title: 'Hongkong', timezone: 'Asia/Hong_Kong' },
];

export default timezoneList;

这是我在测试文件中使用的代码

代码语言:javascript
运行
复制
import React from 'react';
import { shallow } from 'enzyme';
import TimezoneCityItem from '../TimezoneCity.item';
import timezoneList from '/lib/timezoneListDummyData'; // It just an array list of timezone

describe('<TimezoneCityItem />', () => {
   test('Should render TimezoneCityItem correctly', () => {
       const wrapper = shallow(<TimezoneCityItem {...timezoneList[0]} />);
       expect(wrapper).toMatchSnapshot();
   });
});

这是软件包的版本:

代码语言:javascript
运行
复制
"moment": "~2.18.1",
"moment-timezone": "~0.5.13",

以下是错误消息:

代码语言:javascript
运行
复制
Test suite failed to run
TypeError: Cannot read property 'split' of undefined

  at node_modules/moment-timezone/moment-timezone.js:36:34
  at Object.<anonymous>.moment (node_modules/moment-timezone/moment-timezone.js:14:20)
  at Object.<anonymous> (node_modules/moment-timezone/moment-timezone.js:18:2)
  at Object.<anonymous> (node_modules/moment-timezone/index.js:1:120)
  at Object.<anonymous> (imports/ui/components/mainLayout/TimezoneCity.item.jsx:3:49)
  at Object.<anonymous> (imports/ui/components/mainLayout/TimezoneCity.jsx:3:47)
  at Object.<anonymous> (imports/ui/components/mainLayout/MainLayout.jsx:6:47)
  at Object.<anonymous> (imports/ui/components/mainLayout/__tests__/MainLayout.test.js:3:19)
      at Generator.next (<anonymous>)
      at new Promise (<anonymous>)
      at Generator.next (<anonymous>)
      at <anonymous>
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47772080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档