zordius / gulp-jsx-coverage

deprecated
https://www.npmjs.com/package/gulp-jsx-coverage
Other
34 stars 10 forks source link

Requiring relative modules breaks `moduleLoader` #20

Closed thenickcox closed 8 years ago

thenickcox commented 8 years ago

Here's my app.cjsx component:

Router        = ReactRouter.Router
Notifications = require './common/notifications'

App = React.createClass(
  displayName: 'App'
  render: ->
    <div>
      <Notifications />
      {@props.children}
    </div>
)

module.exports = App

When I run the gulp task, it fails with this output:

$ gulp js_coverage                                               !20102
[17:45:20] Requiring external module coffee-script/register
[17:45:22] Using gulpfile ~/projects/dashboard/gulpfile.coffee
[17:45:22] Starting 'js_coverage'...
[17:45:22] 'js_coverage' errored after 809 ms
[17:45:22] Error in plugin 'gulp-mocha'
Message:
    Cannot find module './common/notifications'
Details:
    code: MODULE_NOT_FOUND
Stack:
Error: Cannot find module './common/notifications'
  at Function.Module._resolveFilename (module.js:336:15)
  at Function.Module._load (module.js:286:25)
  at Module.require (module.js:365:17)
  at require (module.js:384:17)
  at Object.<anonymous> (/Users/nickcox/projects/dashboard/app/assets/javascripts/components/app.cjsx:3:29)
  at Object.<anonymous> (/Users/nickcox/projects/dashboard/app/assets/javascripts/components/app.cjsx:3:29)
  at Module._compile (module.js:434:26)
  at Object.moduleLoader [as .js] (/Users/nickcox/projects/dashboard/node_modules/gulp-jsx-coverage/index.js:160:16)
  at Module.load (/Users/nickcox/projects/dashboard/node_modules/coffee-script/lib/coffee-script/register.js:45:36)
  at Function.Module._load (module.js:310:12)
  at Module.require (module.js:365:17)
  at require (module.js:384:17)
  at /Users/nickcox/projects/dashboard/node_modules/mocha/lib/mocha.js:216:27
  at Array.forEach (native)
  at Mocha.loadFiles (/Users/nickcox/projects/dashboard/node_modules/mocha/lib/mocha.js:213:14)
  at Mocha.run (/Users/nickcox/projects/dashboard/node_modules/mocha/lib/mocha.js:453:10)
  at Domain.<anonymous> (/Users/nickcox/projects/dashboard/node_modules/gulp-jsx-coverage/node_modules/gulp-mocha/index.js:56:20)
  at Domain.run (domain.js:191:14)
  at Stream.<anonymous> (/Users/nickcox/projects/dashboard/node_modules/gulp-jsx-coverage/node_modules/gulp-mocha/index.js:54:5)
  at _end (/Users/nickcox/projects/dashboard/node_modules/gulp-jsx-coverage/node_modules/gulp-mocha/node_modules/through/index.js:65:9)
  at Stream.stream.end (/Users/nickcox/projects/dashboard/node_modules/gulp-jsx-coverage/node_modules/gulp-mocha/node_modules/through/index.js:74:5)
  at DestroyableTransform.onend (/Users/nickcox/projects/dashboard/node_modules/gulp/node_modules/vinyl-fs/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:523:10)
  at DestroyableTransform.g (events.js:260:16)
  at emitNone (events.js:72:20)
  at DestroyableTransform.emit (events.js:166:7)
  at /Users/nickcox/projects/dashboard/node_modules/gulp/node_modules/vinyl-fs/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:965:16
  at doNTCallback0 (node.js:407:9)
  at process._tickDomainCallback (node.js:377:13)

Looks like the moduleLoader function in index.js isn't finding it. For reference, here is my gulp task:

#--------------------------------------------------------
# Requirements
#--------------------------------------------------------

gulp      = require 'gulp'
plugins   = require('gulp-load-plugins')()

#--------------------------------------------------------
# Coverage
#--------------------------------------------------------

gulp.task("js_coverage", require('gulp-jsx-coverage').createTask(
  src: [
    'app/assets/javascripts/components/**/*.cjsx',
    'app/assets/javascripts/actions/*.coffee',
    'app/assets/javascripts/stores/*.coffee',
    'app/assets/javascripts/training/compontents/*.cjsx',
    'app/assets/javascripts/training/stores/*.cjsx',
    'app/assets/javascripts/training/actions/*.cjsx',
  ]
  isparta: false
  istanbul:
    preserveComments: true
    coverageVariable: '__MY_TEST_COVERAGE__',
    exclude: /node_modules|test[0-9]/
  transpile:
    cjsx: /\.csjx$/,
    omitExt: ['.cjsx']
  coverage:
    reporters: ['text-summary', 'json', 'lcov']
    directory: 'coverage'
  mocha:
    reporter: 'spec'
  coffee:
    sourceMap: true
))

I get the error regardless of what I have under in the transpile hash. I also get this error for any other modules that are required relatively.

zordius commented 8 years ago

Can not ensure where is the major issue, but here are some points:

  1. app.cjsx: The Notification seems to be a react component, it will be better to change Notifications = require './common/notifications' into Notifications = require './common/notifications.jsx' or .cjsx .
  2. index.js: the transpile config seems to be wrong, it will be better to change cjsx: /\.csjx$/ into cjsx: /\.cjsx$/
thenickcox commented 8 years ago

Thanks for the quick response! I fixed the transpile hash, but it's still saying Cannot find module './common/notifications'. Regarding the adding of the extension to the require statement, isn't that what omitExt allows me to do? To leave the extension off in the require statement? Adding the extension to the require statement makes it work, but I have about 60 components in my app that work currently without the extension in the require statement, and the docs made it seem like that was possible: if you wanna omit file ext when require(), put an array of file exts here.

This also leads to the question, why is this not recommended as you say in the comment on the omitExt line?

zordius commented 8 years ago

it should be a bug, I will test more on it then try to fix it.

zordius commented 8 years ago

Now the bug should be fixed and the new version 0.3.7 released for this, please try it, thanks.

thenickcox commented 8 years ago

@zordius Thanks a lot for the update. I updated to 0.3.7 and am still getting the error that it cannot find a module relatively.

thenickcox commented 8 years ago

For reference, here are my configs:

#--------------------------------------------------------
# Requirements
#--------------------------------------------------------

gulp      = require 'gulp'
plugins   = require('gulp-load-plugins')()

#--------------------------------------------------------
# Coverage
#--------------------------------------------------------

gulp.task("js_coverage", require('gulp-jsx-coverage').createTask(
  src: [
    'test/components/**/*.coffee',

    'app/assets/javascripts/*.coffee',
    'app/assets/javascripts/utils/*.coffee',

    'app/assets/javascripts/components/*.cjsx',
    'app/assets/javascripts/components/**/*.cjsx',

    'app/assets/javascripts/stores/*.coffee',
    'app/assets/javascripts/actions/*.coffee',

    'app/assets/javascripts/training/components/*.cjsx',
    'app/assets/javascripts/training/actions/*.coffee',
    'app/assets/javascripts/training/stores/*.coffee',
  ]
  isparta: false
  istanbul:
    preserveComments: true
    coverageVariable: '__MY_TEST_COVERAGE__',
    exclude: /node_modules|test[0-9]/
  transpile:
    cjsx: /\.cjsx$/
    omitExt: ['.cjsx']
  coverage:
    reporters: ['text-summary', 'json', 'lcov']
    directory: 'js_coverage'
  mocha:
    reporter: 'spec'
  coffee:
    sourceMap: true
))

The first test in the test directory is this:

require '../../testHelper'
ActivityTableRow = require '../../../app/assets/javascripts/components/activity/activity_table_row.cjsx'
ActivityTable = require '../../../app/assets/javascripts/components/activity/activity_table.cjsx'
CustomUtils = require '../../customUtils'
click = CustomUtils.click

describe 'ActivtyTableRow', ->
  TestRow = ReactTestUtils.renderIntoDocument(
    <ActivityTable
      loading=false
      activity={[{ key: '1', revision_score: 0, title: 'Foobar', courses: [{slug: 'cat'}, {slug: 'dog'}]}]}
      headers={[{key: '1' }]}
      noActivityMessage='Hello world'>
      <ActivityTableRow
        key={23948}
        rowId=675818536
        title='Selfie'
        articleUrl='https://en.wikipedia.org/wiki/Selfie'
        author='Wavelength'
        talkPageLink='https://en.wikipedia.org/wiki/User_talk:Wavelength'
        diffUrl='https://en.wikipedia.org/w/index.php?title=Selfie&diff=675818536&oldid=675437996'
        revisionDateTime='2015/08/012 9:43 pm'
        revisionScore=61
      />
    </ActivityTable>
  )

  it 'renders a table row with activity-table-row class and closed class', ->
    rows = ReactTestUtils.scryRenderedDOMComponentsWithTag(TestRow, 'tr')
    # rows[0] is header row
    expect(rows[1].className).to.eq 'activity-table-row closed'

  it 'changes class open to class closed when state is_open', ->
    row = ReactTestUtils.scryRenderedDOMComponentsWithClass(TestRow, 'activity-table-row')[0]
    expect(row.className).to.eq('activity-table-row closed')
    click(row).then (row) -> expect(row.className).to.eq('activity-table-row open')

That first require statement requires the ActivityTableRow component. Here's the top of it, the require statements, where this fails:

React            = require 'react'
ActivityTableRow = require './activity_table_row'
TransitionGroup  = require 'react-addons-css-transition-group'
Loading          = require '../common/loading'

So when I run gulp js_coverage, I get:

$ ./jscoverage                                                                                  !20058
[11:02:05] Requiring external module coffee-script/register
[11:02:06] Using gulpfile ~/projects/dashboard/gulpfile.coffee
[11:02:06] Starting 'js_coverage'...
[11:02:08] 'js_coverage' errored after 1.81 s
[11:02:08] Error in plugin 'gulp-mocha'
Message:
    Cannot find module './activity_table_row'
Details:
    code: MODULE_NOT_FOUND
Stack:
Error: Cannot find module './activity_table_row'
thenickcox commented 8 years ago

Sorry, it looks like my config was wrong. I changed

  transpile:
    cjsx: /\.cjsx$/
    omitExt: ['.cjsx']

to

  transpile:
    cjsx:
      include: /\.cjsx$/
      omitExt: ['.cjsx']

Still working out some other errors, and I'll report back.

thenickcox commented 8 years ago

I can confirm that it gulp-jsx-coverage now allows extension-less require statements, and that it includes coverage for files that don't have tests.

Thanks a lot! :tada: