代码之家  ›  专栏  ›  技术社区  ›  Rakesh Makluri

如何调试React-jest酶测试用例

  •  3
  • Rakesh Makluri  · 技术社区  · 6 年前

    我使用create react app创建了应用程序,以下是我的配置:

    .babelrc

    {
      "env": {
        "test": {
          "sourceMaps": "inline",
          "presets": ["es2015", "react-app"],
          "plugins": ["transform-export-extensions"],
          "only": [
            "./**/*.js",
            "node_modules/jest-runtime"
          ]
        }
      }
    }
    

    Jest配置自 package.json

    "jest": {
        "testMatch": [
          "**/src/**/?(*.)(spec|test).js?(x)"
        ],
        "snapshotSerializers": [
          "enzyme-to-json/serializer"
        ],
        "moduleFileExetnsions": [
          "ts",
          "txx",
          "js",
          "json"
        ],
        "transform": {
          "^.+\\.jsx$": "babel-jest",
          "^.+\\.js$": "babel-jest"
        }
    }
    

    测试用例:

    import { shallow } from 'enzyme';
    import App from './App';
    describe('App', () => {
      it('should render app', () => {
    
        const component = shallow(<App />);
    
        expect(component).toMatchSnapshot();
      });
    });
    

    获取以下错误:

    Test suite failed to run
    
        ReferenceError: [BABEL] .../src/containers/App.spec.js: Unknown option: /node_modules/babel-preset-react-app/index.js.overrides. Check out http://babeljs.io/docs/usage/options/ for more information about options.
    
        A common cause of this error is the presence of a configuration options object without the corresponding preset name. Example:
    
        Invalid:
          `{ presets: [{option: value}] }`
        Valid:
          `{ presets: [['presetName', {option: value}]] }`
    
        For more detailed information on preset configuration, please see http://babeljs.io/docs/plugins/#pluginpresets-options. (While processing preset: "/node_modules/babel-preset-react-app/index.js")
    
          at Logger.error (node_modules/babel-core/lib/transformation/file/logger.js:41:11)
          at OptionManager.mergeOptions (node_modules/babel-core/lib/transformation/file/options/option-manager.js:226:20)
          at node_modules/babel-core/lib/transformation/file/options/option-manager.js:265:14
          at node_modules/babel-core/lib/transformation/file/options/option-manager.js:323:22
              at Array.map (<anonymous>)
          at OptionManager.resolvePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
          at OptionManager.mergePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
          at OptionManager.mergeOptions (node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
          at OptionManager.init (node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
          at File.initOptions (node_modules/babel-core/lib/transformation/file/index.js:212:65)
    

    在.babelrc中更改“test”-->“dev”后出现上述错误,但得到另一个错误:

    1 回复  |  直到 6 年前
        1
  •  0
  •   Sviat Kuzhelev    6 年前

    试着用这个 .babel 配置:

    {
       "presets": ["es2015", "react-app"],
       "plugins": ["transform-export-extensions"],
       "only": [
         "./**/*.js",
         "node_modules/jest-runtime"
       ]
    }
    

    它应该对你有帮助。