2017-07-11 15 views
0

リアクションコンポーネントをモックしようとすると、私はエラー以下になります。リアクションコンポーネントに失敗したテストケース

?テストスイートを実行できませんでした

C:\UBS\Dev\workspace\topcat-ui\node_modules\antd\lib\style\index.css:9 
html { 
    ^
SyntaxError: Unexpected token { 

    at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17) 
    at Object.<anonymous> (node_modules/antd/lib/pagination/style/css.js:3:1) 
    at Object.<anonymous> (src/main/webapp/js/pages/dashboard/Dashboard.jsx:1:176) 

これは私のpackage.jsonの外観です。誰かが私に正しい方向を向けることができますか?

{ 
    "name": "dashboard", 
    "version": "1.0.0", 
    "description": "Realtime dashboard", 
    "main": "index.html", 
    "scripts": { 
    "build": "node_modules\\.bin\\webpack", 
    "dev": "node_modules\\.bin\\webpack --watch", 
    "test": "jest", 
    "test-coverage": "jest --coverage" 
    }, 
    "author": "", 
    "license": "ISC", 
    "devDependencies": { 
    "babel-core": "^6.25.0", 
    "babel-jest": "^20.0.3", 
    "babel-loader": "^7.0.0", 
    "babel-plugin-import": "^1.2.1", 
    "babel-plugin-transform-class-properties": "^6.24.1", 
    "babel-preset-es2015": "^6.24.1", 
    "babel-preset-react": "^6.24.1", 
    "css-loader": "^0.28.4", 
    "enzyme": "^2.9.1", 
    "eslint": "^4.1.1", 
    "eslint-loader": "^1.8.0", 
    "eslint-plugin-react": "^7.1.0", 
    "html-webpack-plugin": "^2.28.0", 
    "jest": "^20.0.4", 
    "moment": "^2.18.1", 
    "path": "^0.12.7", 
    "react-hot-loader": "^3.0.0-beta.7", 
    "react-test-renderer": "^15.6.1", 
    "style-loader": "^0.18.2", 
    "webpack": "^2.6.1" 
    }, 
    "dependencies": { 
    "antd": "^2.11.0", 
    "axios": "^0.16.2", 
    "babel-polyfill": "^6.23.0", 
    "react": "^15.5.4", 
    "react-dom": "^15.6.1", 
    "react-file-download": "^0.3.4", 
    "react-redux": "^5.0.5", 
    "react-router": "^4.1.1", 
    "react-router-dom": "^4.1.1", 
    "redux": "^3.6.0", 
    "redux-thunk": "^2.2.0", 
    "url-pattern": "^1.0.3" 
    } 
} 

これは私のテストケースはおそらく

import React from 'react'; 
import {mount} from 'enzyme'; 
import Dashboard from '../../../main/webapp/js/pages/dashboard/Dashboard'; 

function setup() { 
    const Dashboard = mount(<Dashboard />); 
    return { 
     Dashboard 
    } 
} 

describe('Components',() => { 
    describe('Dashboard',() => { 
     it('should render itself',() => { 
      const { Dashboard } = setup(); 
      expect(Dashboard.find('table').length).toBe(1); 
     }); 
    }); 
}); 
+0

これはReact/Babel/Jestでは問題ありません。これはあなたのCSSの問題です。 –

答えて

1

ウェブパックを使用しているとしますか? docsからこのメモを確認してください。冗談設定のmoduleNameMapperまたはtransformオプションを使用して、cssファイルを偽装/無視することができます。

{ 
"jest": { 
    "moduleNameMapper": { 
     "\\.(css|less)$": "identity-obj-proxy" 
    }, 
    "transform": { 
     "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/fileTransformer.js" 
    } 
    } 
} 


// fileTransformer.js 
const path = require('path'); 

module.exports = { 
    process(src, filename, config, options) { 
    return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';'; 
    }, 
}; 
+0

ありがとうございます 私はこの設定をwebpack.config.jsに入れますか? { "冗談":{ "moduleNameMapper":{ "\\(CSS |以下)$": "アイデンティティ-OBJプロキシ" }、 "変換":{ 「\\( JPG | JPEG | PNG | GIF | EOT | OTF | WEBP | SVG | TTF | WOFF | woff2 | MP4 | WEBM | wavファイル| mp3 | M4A | AAC |男鹿)$ ":" /fileTransformer.js」 }} } – user4900074

+0

@ user4900074いいえ、あなたの 'package.json'に入れてください –

+0

すごい!これは機能します。どうもありがとうございます – user4900074

0

どのように見えるか、スタイルシートをロードすると、いくつかの障害です。コンポーネントとモックプロセスに関する詳細情報を提供する必要があります。おそらく、.cssファイルを.jsファイルとしてロードしています。

+0

インポート '反応'から反応します。 '{酵素}'から{マウント}をインポートします。 '../../../main/webapp/js/pages/dashboard/Dashboard'のダッシュボードをインポートします。 function setup(){ const Dashboard = mount(); リターン{ ダッシュボード } } 記述( 'コンポーネント'、()=> { 記述( 'ダッシュボード'、()=> { それ( '自体をレンダリングするべきである'、()=> { CONST {Dashboard} = setup(); expect(Dashboard.find( 'table')。length).toBe(1); }); }); }); – user4900074

+0

ありがとう、私はReact、Jestを使っている初心者です。だから私と一緒に抱きしめてください。このエラーは、index.css stylesheet inisde node_modulesフォルダから発生しています。 スタイルシート全体を投稿する方法がわかりません。 これは私のモックのように見えます。追加情報が必要な場合はお知らせください。 – user4900074

+0

これを確認してください:https://stackoverflow.com/questions/39418555/syntaxerror-with-jest-and-react-and-importing-css-filesそれが役に立たなければ、私はさらに調査しますが、それはおそらく私が言ったように、現在.cssを.jsとしてロードしています –

関連する問題