我正在导入要测试的模块(称为activity.js)。此模块(activity.js)使用以下命令导入formatAMPM:
import {formatAMPM} from '../config/helper
-> activity.js
我使用https://react-native-community.github.io/upgrade-helper/?from=0.55.4&to=0.58.6
从本机0.55.4更新为0.58.6,这可能引起了问题。但是我不确定。
activity.js
import {
SET_ACTIVITY_DATE,
SET_ACTIVITY_START_TIME,
SET_ACTIVITY_DURATION,
SET_ACTIVITY_SRC,
SET_ACTIVITY_DEST,
SET_ACTIVITY_TYPE,
SET_ACTIVITY_DISTANCE,
SET_ACTIVITY_CO2
} from '../actions/ActivityDetailsAction';
import { formatAMPM } from '../config/helper';
export default function activity(
state = {
date: new Date().toDateString(),
startTime: formatAMPM(new Date()),
duration: 0,
src: { latitude: -1, longitude: -1 },
dest: { latitude: -1, longitude: -1 },
type: 'STILL',
distance: 0,
co2: 0
},
action
) {
switch (action.type) {
case SET_ACTIVITY_DATE:
return Object.assign({}, state, {
date: action.value
});
...
helper.js
/**
* helper function to format date
* @param Date date
* @return {time} formatted time
*/
export function formatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
hours = hours < 10 ? '0' + hours : hours;
minutes = minutes < 10 ? '0' + minutes : minutes;
var time = hours + ':' + minutes + ' ' + ampm;
return time;
}
我运行npm run test
时,此错误消息显示在终端上:
TypeError: Cannot read property 'formatAMPM' of undefined
33 | },
34 | action
> 35 | ) {
36 | switch (action.type) {
37 | case SET_ACTIVITY_DATE:
38 | return Object.assign({}, state, {