在Jest中导入时遇到问题

时间:2018-08-22 15:28:39

标签: react-native jestjs es6-modules babel-jest

我正在尝试为我们的本机应用程序中的任何复杂业务逻辑编写单元测试。但是,由于以下原因,我的测试不断失败,因此我无法运行它们:

 /Users/hcondron/developer/MyApp/node_modules/react-native-extended-stylesheet/src/index.js:5
import Api from './api';
^^^^^^
SyntaxError: Unexpected token import

我已经研究此问题两天了,找不到解决方案。我相信这与我配置Jest的方式有关,但是我迷失在哪里看或确切地更改什么。

这是我的Package.JSON:

{
  "name": "MyApp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "flow start": "flow start",
    "flow stop": "flow stop",
    "flow status": "flow status",
    "flow coverage": "flow coverage",
    "remotedev": "remotedev --hostname=localhost --port=5678",
    "test": "jest platform/__tests__/OnDemand-Test.js",
  },
  "jest": {
    "preset": "react-native",
    "transformIgnorePatterns": [
      "<rootDir>/node_modules/"
    ],
    "transform": {
      "^.+\\.jsx?$": "babel-jest"
    }
  },
  "dependencies": {
    "@mapbox/geo-viewport": "^0.2.2",
    "axios": "^0.16.2",
    "base-64": "^0.1.0",
    "currency-formatter": "^1.4.1",
    "es6-symbol": "^3.1.1",
    "firebase": "^4.8.2",
    "flow": "^0.2.3",
    "geojson-utils": "1.1.0",
    "lodash.groupby": "^4.6.0",
    "luxon": "^1.3.0",
    "polyline": "0.2.0",
    "prop-types": "^15.5.10",
    "qs": "^6.5.0",
    "raven-for-redux": "^1.0.0",
    "react": "16.3.1",
    "react-native": "0.55.4",
    "react-native-android-settings-library": "^1.0.4",
    "react-native-blur": "^3.2.0",
    "react-native-cli": "^2.0.1",
    "react-native-code-push": "5.3.2",
    "react-native-collapsible": "^0.9.0",
    ...
  },
  "devDependencies": {
    "@storybook/addon-actions": "^3.2.13",
    "@storybook/addon-links": "^3.2.13",
    "@storybook/react-native": "^3.2.13",
    "babel-cli": "^6.26.0",
    "babel-jest": "23.4.2",
    "babel-plugin-module-resolver": "^2.7.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-flow": "^6.23.0",
    "babel-preset-react-native": "3.0.1",
    "flow-bin": "^0.77.0",
    "jest": "23.3",
    "react-dom": "16.0.0-alpha.12",
    "react-test-renderer": "16.0.0-alpha.12",
    "reactotron-react-native": "^1.14.0",
    "reactotron-redux": "^1.13.0",
    "remotedev-server": "^0.2.3"
  }
}

这是我的babelrc文件:

{
  "env": {
    "test": {
      "presets": [["es2015", { "modules": false }], "react-native"],
      "plugins": ["transform-es2015-modules-commonjs"]
    }
  }
}

这是我的考试:

import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import * as odActions from '../modules/onDemand/OnDemandActions';
import { getAvailabilitiesRequestThunk } from '../modules/onDemand/OnDemandThunks';

const middlewares = [thunk];
const mockStore = configureStore(middlewares);

describe('test action', () => {
  it('should add two numbers', () => {
    expect(odActions.testJestFunc(1, 2)).toBe(3);
  });
});

describe('switch price between miles and currency', () => {
  const store = mockStore([]);
  it('should dispatch the switchPriceDisplayBetweenMilesAndCurrency action', () => {
    const expectedAction = { type: odActions.SWITCH_PRICE_DISPLAY_BETWEEN_MILES_AND_CURRENCY };
    expect(odActions.switchPriceDisplayBetweenMilesAndCurrency()).toEqual(expectedAction);
  });
});

describe('init booking flow thunk', () => {
  it('should dispatch init booking flow and a series of other actions', () => {
    // const expectedActions = [
    //   { type: odActions.GET_AVAILABILITIES_REQUEST },
    //   { type: odActions.COMPLETE_ORDER_SUCCESSGET_AVAILABILITIES_SUCCESS }
    // ];
    // const store = mockStore([]);
    // return store.dispatch(odActions.getAvailabilitiesRequestThunk()).then(() => {
    //   expect(store.getActions()).toBe(expectedActions);
    // });
  });
});

非常感谢任何帮助!

0 个答案:

没有答案