unit testing - Cannot setup `babel-plugin-rewire` for React tests -


i'm trying setup babel-plugin-rewire in jest tests can generate small test snapshot wrapper component instead of having huge 1 child components in it.

the settings seem correct following error:

referenceerror: wrapper not defined   @ _get_original__ (app\components\wrapper\tests\index.te st.js:22:2168)   @ _get__ (app\components\wrapper\tests\index.test.js:22: 1912)   @ suite.<anonymous> (app\components\wrapper\tests\index. test.js:8:82)   @ object.<anonymous> (app\components\wrapper\tests\index .test.js:6:1)   @ process._tickcallback (internal\process\next_tick.js:103:7) 

package.json

"babel": {     "presets": [       [         "latest",         {           "es2015": {             "modules": false           }         }       ],       "react"     ],     "env": {       "test": {         "plugins": [           "babel-plugin-rewire",           "transform-es2015-modules-commonjs",           "dynamic-import-node"         ]     ...    "devdependencies": {     ...     "babel-plugin-rewire": "^1.1.0", 

webpack.base.babel.js

(common webpack configuration -same in webpack.basetest.babel.js)

module: {     loaders: [{       test: /\.js$/, // transform .js files required somewhere babel       loader: 'babel-loader?plugins=babel-plugin-rewire',       exclude: /node_modules/,       query: options.babelquery,     }, 

my test

for wrapper component contains header child component:

import react 'react'; import { shallow } 'enzyme';  import wrapper '../index';  describe('<wrapper />', () => {    console.log('debugging >>> ' + json.stringify(wrapper)); // wrapper defined blows here @@    // beforeeach(() => {   //   wrapper.__rewire__('header', class extends react.component {   //     render(){ return <div {...this.props}>test wrapper!!</div> }   //   });   // })    // it('renders correctly', () => {   //   const tree = shallow( <wrapper myprops={testdata.myprops} /> );   //   expect(tree.html()).tomatchsnapshot();   // });    ... }); 

thanks

update: if comment out in wrapper jsx header used (even leaving import header is) wrapper.__rewire__ becomes defined , therefore beforeeach doesn't throw errors anymore.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -