yeoman / yo

CLI tool for running Yeoman generators
http://yeoman.io
BSD 2-Clause "Simplified" License
3.85k stars 399 forks source link

Jest coverage errors when composing #553

Open li3 opened 6 years ago

li3 commented 6 years ago

When using jest to collect code coverage, and a generator that calls this.composeWith, code coverage fails for some reason. Any ideas? Thanks in advanced.

On Windows 10 x64 Node: 6.11.2

Dependencies:

{
 "dependencies": {
    "yeoman-generator": "2.0.1"
  },
  "devDependencies": {
    "jest": "21.2.1",
    "yeoman-test": "1.7.0",
  }
}

Minimal Example:

/generators/app/index.js:

const Generator = require('yeoman-generator');

module.exports = class extends Generator {
  initializing() {
    this.composeWith(require.resolve('../sub'));
  }
};

/generators/sub/index.js:

const Generator = require('yeoman-generator');

module.exports = class extends Generator {
  noOp() {}
};

/test/app/index.test.js:

const helpers = require('yeoman-test');
const Generator = require('yeoman-generator');

describe(':app', () => {
  it('is a generator', (done) => {
    helpers
      .run(require.resolve('../../generators/app'))
      .on('ready', (generator) => {
        expect(generator instanceof Generator).toBe(true);
        done();
      })
  });
});

Output:

> generator@1.0.0 test C:\code\generator
> jest

 PASS  test\app\index.test.js

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.036s
Ran all test suites.
Running coverage on untested files...events.js:165
      throw err;
      ^

Error: Uncaught, unspecified "error" event. (TypeError: Generator is not a constructor)
    at RunContext.emit (events.js:163:17)
    at RunContext.<anonymous> (C:\code\generator\node_modules\yeoman-test\lib\run-context.js:102:15)
    at emitOne (events.js:96:13)
    at emit (events.js:188:7)
    at Immediate.setImmediate (C:\code\generator\node_modules\yeoman-generator\lib\index.js:406:18)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)
npm ERR! Test failed.  See above for more details.
SBoudrias commented 6 years ago

Looks like require.resolve('../lambda') isn't defined.

The result come after the test results because you're calling done() when the generator is ready, not when it's done running.

li3 commented 6 years ago

That was my mistake I was trying to cleanup the example so it didn't include stuff I was actually working on. require.resolve('../lambda') should have been require.resolve('../sub')

The test itself works fine as long as coverage isn't turned on.

SBoudrias commented 6 years ago

The async test is still broken. The coverage might slow down your test, so the error appears before jest exit.