pocesar/grunt-mocha-istanbul

Task is not finding source files to check

Closed this issue · 9 comments

Hi!

I am using a generator, fullstack-angular, and it come with this configuration:

mocha_istanbul
  unit: {
    src: '<%= yeoman.server %>',
    options: {
      excludes: ['**/*.{spec,mock,integration}.js'],
      mask: '**/*.spec.js',
      require: 'mocha.conf.js',
      reporter: 'spec',
      coverageFolder: 'coverage/server/unit'
    }
  }
}

But when running, this is the result:

...
50 passing (9s)
...
=============================== Coverage summary ===============================
Statements   : 100% ( 9/9 )
Branches     : 100% ( 0/0 )
Functions    : 100% ( 0/0 )
Lines        : 100% ( 9/9 )
================================================================================

Looking for Html with result, I've noticed that only mocha.conf.js was covered, event with all testing running on the process.

I've tried many options, but didn't worked. Someone Know what is the problem.

My folder structure is this one:

server - All files divided in *.j, *.spec.js (Tests with source).
 - api
 -- api1 (.js, .spec.js, .mock.js)
 -- api2 (.js, .spec.js, .mock.js)
...

Tks!

that folder structure is a bit tricky TBH. what is the output for the yeoman.server variable?
by default, mocha won't try to recursively execute test files unless you pass recursive: true to the options.
can you give me the output for grunt mocha_istanbul:unit --verbose --debug?

Hi,

yeoman.server = server

I've copied this projeto from Angular Fullstack Generator, that come with this organization. Even with recursive = true, no file is mapper.

I will post now the output with debug and verbose mode.

Before Tests:

Initializing
Command-line options: --verbose, --debug=1

Reading "Gruntfile.js" Gruntfile...OK

Registering Gruntfile tasks.
Reading package.json...OK
Parsing package.json...OK
Initializing config...OK
Loading "Gruntfile.js" tasks...OK
+ build, default, express-keepalive, serve, server, test, wait

Running tasks: mocha_istanbul:unit

Loading "grunt-mocha-istanbul" plugin

Registering "/home/laucsen/Projects/fortknox/node_modules/grunt-mocha-istanbul/tasks" tasks.
Loading "index.js" tasks...OK
+ istanbul_check_coverage, mocha_istanbul

Running "mocha_istanbul:unit" (mocha_istanbul) task
[D] Task source: /home/laucsen/Projects/fortknox/node_modules/grunt-mocha-istanbul/tasks/index.js
Verifying property mocha_istanbul.unit exists in config...OK
Files: server
Options: require="mocha.conf.js", ui=false, globals=[], reporter="spec", timeout=false, coverage=false, slow=false, grep=false, dryRun=false, quiet=false, recursive, mask="**/*.spec.js", root=false, print=false, noColors=false, harmony=false, coverageFolder="coverage/server/unit", reportFormats=["lcov"], check={"statements":false,"lines":false,"functions":false,"branches":false}, excludes=["**/*.{spec,mock,integration}.js"], mochaOptions=false, istanbulOptions=false
>> Will execute: node /home/laucsen/Projects/fortknox/node_modules/istanbul/lib/cli.js cover -x */*.{spec,mock,integration}.js --dir=/home/laucsen/Projects/fortknox/coverage/server/unit --report=lcov /home/laucsen/Projects/fortknox/node_modules/mocha/bin/_mocha -- --require mocha.conf.js --reporter spec --recursive server/**/.spec.js

After Tests:

Express server listening on 9000, in development mode
  Dynamito Basic Schema Types
 - Migration completed
    Values Conversion
      ✓ should check if saved String is returned from gets (1368ms)
 = Finished populating users
      ✓ should check if saved Number is returned from gets (353ms)
      ✓ should check if saved Object is returned from gets (173ms)
      ✓ should check if saved Boolean is returned from gets (188ms)
      ✓ should check if saved Date is returned from gets (210ms)


  5 passing (3s)

=============================================================================
Writing coverage object [/home/laucsen/Projects/fortknox/coverage/server/unit/coverage.json]
Writing coverage reports at [/home/laucsen/Projects/fortknox/coverage/server/unit]
=============================================================================

=============================== Coverage summary ===============================
Statements   : 100% ( 9/9 )
Branches     : 100% ( 0/0 )
Functions    : 100% ( 0/0 )
Lines        : 100% ( 9/9 )
================================================================================
>> Done. Check coverage folder.

Done, without errors.

I couldn't find any relevant information about the format in */*.{spec,mock,integration}.js, it seems it's seeing this as */*..js. although the mocha tests are running as you point out. if you change the excludes to:

{
excludes: ['**/*.spec.js','**/*.mock.js','**/*.integration.js']
}

does it help?

also, what is in mocha.conf.js? if this file creates another process and forks, it won't be able to be covered by istanbul.
the configuration file for mocha shouldn't go in require options (trying to eliminate all possible noise from the problem). also, any options that exists in mocha.opts file, if any, will be used

Ok, I'm checking the sources for https://github.com/angular-fullstack/generator-angular-fullstack/ trying to understand what's wrong. It seems that it tries to use Babel / ES6, which isn't supported out of the box in the master version of this library, and I tried to address on the issue 49 branch
the problem you describe happened to other people running ES6 code in #49 where the source files weren't being able to be picked up

EDIT: Try using the 5.0.0 version. Notice that you'll need to follow the README.md instructions for using es6 tests and src files

Hi!

On 5.0.0 it take the same result, but take more time.

This is my mocha.conf.js file:

'use strict';

// Register the Babel require hook
require('babel-core/register');

var chai = require('chai');

// Load Chai assertions
global.expect = chai.expect;
global.assert = chai.assert;
chai.should();

// Load Sinon
global.sinon = require('sinon');

// Initialize Chai plugins
chai.use(require('sinon-chai'));
chai.use(require('chai-as-promised'));
chai.use(require('chai-things'))

This line: require('babel-core/register'); is supposed to do the trick.

I will try to do like docs says about ES6 and will post results here.

the require('babel-core/register') will only work for mocha, istanbul will still try to instrument as ES5. that's the reason I'm afraid. the babel-node must be called before executing istanbul (and it only works with istanbul 1.0.0-alpha.2, it won't work with 0.4.x), hence the need to adjust your project with the ES2015 part of the README

@pocesar I've updated my Istanbul to 1.0.0-alpha.2 and it worked, with my initial configuration.

My only note is that, when using: excludes: ['**/*.spec.js','**/*.mock.js','**/*.integration.js'] it is taken almost 5 minutes to start to run all my tests.

But when i remove **/ from my rules:

excludes: [
            '*.spec.js',
            '*.mock.js',
            '*.integration.js'
          ]

on this case, it take only 1m to prepare and run all test, what is a good time to my tests.

Thanks for helping!

good to know it worked for you. the **/* glob is pretty expensive, and depending on the size of the project, will indeed take a lot of time, since everything gets expanded to the command line