2015-12-04 16 views
7

Tôi có một dự án ES6 trong đó tôi sử dụng webpack để chuyển đổi mã máy khách thành ES5. Dự án sử dụng ReactJS ở phía máy khách, do đó, tự nhiên, các xét nghiệm đơn vị sử dụng React TestUtils. Trong bản cập nhật mới nhất (0,14), React changed how several modules are imported (bao gồm cả TestUtils). Tôi muốn cập nhật, nhưng tôi không thể tìm ra cách làm cho các bài kiểm tra đơn vị hoạt động sau khi thay đổi nhập khẩu.Mô-đun không tìm thấy: Lỗi: Không thể giải quyết mô-đun 'phản ứng-addons-test-utils'

Khi tôi cố gắng để chạy các bài kiểm tra, tôi nhận được thông báo sau:

04 12 2015 12:40:48.038:ERROR [karma]: { [Error: no such file or directory] 
    code: 'ENOENT', 
    errno: 34, 
    message: 'no such file or directory', 
    path: '/_karma_webpack_/public/test/main.js' } 
Error: no such file or directory 
    at MemoryFileSystem.readFileSync (<project>/node_modules/karma-webpack/node_modules/webpack-dev-middleware/node_modules/memory-fs/lib/MemoryFileSystem.js:107:10) 
    at MemoryFileSystem.readFile (<project>/node_modules/karma-webpack/node_modules/webpack-dev-middleware/node_modules/memory-fs/lib/MemoryFileSystem.js:297:21) 
    at doRead (<project>/node_modules/karma-webpack/index.js:156:26) 
    at Plugin.readFile (<project>/node_modules/karma-webpack/index.js:160:3) 
    at doNTCallback0 (node.js:419:9) 
    at process._tickCallback (node.js:348:13) 
Hash: dee787e2bee8303745db 
Version: webpack 1.12.9 
Time: 3657ms 
           Asset  Size Chunks  Chunk Names 
       public/test/main.js 1.71 MB  0  public/test/main.js 
5044d04bf14a5de57d08.hot-update.json 35 bytes    
chunk {0} public/test/main.js (public/test/main.js) 1.6 MB [rendered] 
    [0] ./public/test/main.js 176 bytes {0} [built] 
    [1] ... 

ERROR in ./public/test/common/test_modal.jsx 
Module not found: Error: Cannot resolve module 'react-addons-test-utils' in <project>/public/test/common 
@ ./public/test/common/test_modal.jsx 13:28-62 

ERROR in ./public/test/pages/test_auth.jsx 
Module not found: Error: Cannot resolve module 'react-addons-test-utils' in <project>/public/test/pages 
@ ./public/test/pages/test_auth.jsx 17:28-62 

Trong tests_auth.jsx tôi và các tập tin test_modal.jsx, tôi bắt đầu nhập khẩu sau:

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import TestUtils from 'react-addons-test-utils'; 

Các tập lệnh thử nghiệm trong package.json của tôi là:

PHANTOMJS_BIN=./node_modules/.bin/phantomjs && ./node_modules/karma/bin/karma start karma.config.js 

Các phụ thuộc có liên quan của tôi là:

"devDependencies": { 
    "chai": "^3.4.1", 
    "chai-as-promised": "^5.1.0", 
    "coveralls": "^2.11.4", 
    "karma": "^0.13.9", 
    "karma-chai": "^0.1.0", 
    "karma-chai-as-promised": "^0.1.2", 
    "karma-chai-plugins": "^0.6.1", 
    "karma-coverage": "^0.5.2", 
    "karma-mocha": "^0.2.0", 
    "karma-phantomjs-launcher": "^0.2.1", 
    "karma-sinon-chai": "^1.0.0", 
    "karma-webpack": "^1.7.0", 
    "mocha": "^2.3.3", 
    "phantomjs": "^1.9.18", 
    "react-hot-loader": "^1.3.0", 
    "webpack-dev-server": "^1.10.1" 
    }, 
    "dependencies": { 
    "react": "0.14.3", 
    "react-dom": "0.14.3", 
    "webpack": "^1.12.1" 
    } 

karma.config.js của tôi là:

var webpack = require('webpack'); 

module.exports = function(config) { 
    'use strict'; 

    config.set({ 
     files: [ 
      './public/test/polyfill.js', 
      './public/test/main.js' 
     ], 
     preprocessors: { 
      './public/test/main.js': [ 
       'webpack' 
      ] 
     }, 
     browsers: [ 
      'PhantomJS' 
     ], 
     frameworks: [ 
      'mocha', 
      'sinon-chai', 
      'chai-as-promised', 
      'chai' 
     ], 
     reporters: [ 
      'progress', 
      'coverage' 
     ], 
     coverageReporter: { 
      dir: 'coverage/', 
      reporters: [ 
       { 
        type: 'lcovonly', 
        subdir: '.', 
        file: 'lcov.info' 
       }, { 
        type: 'html', 
        subdir: 'html' 
       } 
      ] 
     }, 
     webpack: { 
      plugins: [ 
       new webpack.HotModuleReplacementPlugin(), 
       new webpack.NoErrorsPlugin() 
      ], 
      module: { 
       loaders: [ 
        { 
         test: /\.jsx?$/, 
         exclude: /node_modules/, 
         loader: 'babel-loader?stage=0' 
        } 
       ] 
      }, 
      resolve: { 
       extensions: [ 
        '', 
        '.js', 
        '.jsx' 
       ], 
       modulesDirectories: [ 
        'node_modules', 
        'public/src', 
        'public/test' 
       ] 
      } 
     }, 
     webpackMiddleware: { 
      noInfo: true 
     }, 
     singleRun: true, 
     plugins: [ 
      'karma-mocha', 
      'karma-webpack', 
      'karma-coverage', 
      'karma-sinon-chai', 
      'karma-chai', 
      'karma-chai-plugins', 
      'karma-chai-as-promised', 
      'karma-phantomjs-launcher' 
     ] 
    }); 
}; 

Cuối cùng, cấu trúc tập tin của tôi trông giống như sau:

karma.config.js 
package.json 
node_modules/ 
public/ 
    app.js 
    index.html 
    src/ 
     main.jsx 
    test/ 
     main.js 
     polyfill.js 
     pages/ 
      test_auth.jsx 
     common/ 
      test_modal.jsx 

tôi rõ ràng đã điều gì đó cấu hình sai, nhưng tôi đang gặp khó rất nhiều khó khăn trong việc tìm ra nó là gì. Có ai gặp phải vấn đề tương tự không? Có ai quen thuộc hơn với Karma, Webpack, hay React 0.14 biết tôi sai gì không?

Trả lời

15

Tôi cảm thấy khá câm ngay lúc này. Tôi quên thêm 'react-addons-test-utils' vào package.json của mình.

Các vấn đề liên quan