i've met trouble when trying test react js code using jest framework.
let mine component:
# coffee/global_widget.coffee @globalwidget = react.createclass render: -> <div classname='row'> <div classname='col-md-12'> <terminalwidget /> </div> </div> # coffee/terminal_widget.coffee @terminalwidget = react.createclass render: -> <div>hey! terminal!</div> so, want test using jest.
jest.dontmock '../coffee/global_widget' describe 'globalwidget', -> global.react = require('react/addons') globalwidget = require('../coffee/global_widget') testutils = react.addons.testutils globalwidgetfortest = testutils.renderintodocument(<globalwidget />) # body of test i have trouble:
npm test > terminal-ui@0.0.2 test /home/alex/my_project > jest using jest cli v0.4.5 fail __tests__/global_widget-test.coffee (0.276s) ● globalwidget › encountered declaration exception - referenceerror: globalwidget not defined if append module.exports = @globalwidget coffee/global_widget.coffee, terminalwidget not defined. module.exports= , why need puts them every component in code?
it looks jest doesnt have access global variables, need export each file , require them necessary.
need use module.exports because youre using requirejs syntax line globalwidget = require('../coffee/global_widget'). isolate code prevent having bunch of globally available code. way, import (using require) , export (using module.exports = ...) code need. additionally, in case, allows processes such jest gain access files otherwise available.
if absolutely dont want use requirejs (which recommended use) can try adding them globals or fiddling jests config make these available prior test suite, though more difficult exporting modules
Comments
Post a Comment