2016-03-03 48 views
0

我正在嘗試使用酶和笑話爲反應原生組件編寫基本測試。我得到一個錯誤影射酶不被正確導入:酶導入參考錯誤

● SearchPage › it starts spinner when page is loading 
    - ReferenceError: _enzyme is not defined 
     at Spec.eval (__tests__/basic-test.js:12:16) 
1 test failed, 0 tests passed (1 total in 1 test suite, run time 1.113s) 
npm ERR! Test failed. See above for more details. 

我試圖重新安裝包酶與代碼盲目擺弄因爲我是一個新手。基本測試不使用來自酵素工作的淺度,即期望(真).toBe(真)。我已經瀏覽了很多關於JS中引用錯誤的問題,但在這種情況下似乎都沒有適用(無論如何我都沒有經驗)。

這裏是測試文件。相當裸露,限制錯誤可能來自哪裏。

jest.dontMock('../SearchPage'); 

import React from 'react'; 
import { shallow } from 'enzyme'; 
import ReactDOM from 'react-dom'; 
import TestUtils from 'react-addons-test-utils'; 

const SearchPage = require('../SearchPage'); 

describe('SearchPage',() => { 
    it('starts spinner when page is loading',() => { 
    const wrapper = shallow(<SearchPage />); 

    // Check that it is false initially i.e. not loading. 
    expect(wrapper.state('isLoading')).toBe(false); 

    // Simulate a touch on 'Go' button and verify loading is now true 
    }); 
}); 

據我所知,我的package.json具有正確的依賴關係。我試圖重新安裝酶而沒有成功。它是:

{ 
    "name": "PropertyFinder", 
    "version": "0.0.1", 
    "private": true, 
    "scripts": { 
    "start": "node node_modules/react-native/local-cli/cli.js start", 
    "test": "jest" 
    }, 
    "dependencies": { 
    "react-native": "^0.20.0", 
    "react-dom": "~0.14.7" 
    }, 
    "devDependencies": { 
    "babel-jest": "*", 
    "enzyme": "^2.0.0", 
    "jest-cli": "^0.8.2", 
    "react": "^0.14.7", 
    "react-addons-test-utils": "~0.14.0", 
    "react-native-mock": "0.0.6" 
    }, 
    "jest": { 
    "scriptPreprocessor": "node_modules/react-native/jestSupport/preprocessor.js", 
    "setupEnvScriptFile": "node_modules/react-native/jestSupport/env.js", 
    "testPathIgnorePatterns": [ 
     "/node_modules/", 
     "packager/react-packager/src/Activity/" 
    ], 
    "testFileExtensions": [ 
     "js" 
    ], 
    "unmockedModulePathPatterns": [ 
     "promise", 
     "source-map", 
     "react", 
     "react-dom", 
     "react-addons-test-utils", 
     "fbjs", 
     "enzyme", 
     "cheerio", 
     "htmlparser2", 
     "lodash", 
     "domhandler", 
     "object.assign", 
     "define-properties", 
     "function-bind", 
     "object-keys", 
     "object.values", 
     "es-abstract" 
    ] 
    } 
} 

什麼可以/將在此上下文中引發此ReferenceError?如果我嘗試用摩卡咖啡/柴咖啡來看看它是否有效,我可能會很快發表評論,但我寧願留在這裏開玩笑。

回答

0

這個問題從ES6 explained in more detail here的進口量的提升上升。使用babel-jest作爲預處理器可以解決此問題,但保留「setupEnvScriptFile」非常重要。

什麼例子爲我工作

"jest": { 
    "preprocessorIgnorePatterns": [ 
     "node_modules/enzyme" 
    ], 
    "scriptPreprocessor": "<rootDir>/node_modules/babel-jest", 
    "setupEnvScriptFile": "node_modules/react-native/jestSupport/env.js", 
    "testPathIgnorePatterns": [ 
     "/node_modules/", 
     "packager/react-packager/src/Activity/" 
    ], 
    "unmockedModulePathPatterns": [ 
     "react", 
     "react-dom", 
     "react-native", 
     "react-addons-test-utils", 
     "promise", 
     "source-map", 
     "enzyme" 
    ] 
    },