composeWith sub-generator not mocking as expected?
Asked Answered
I

1

6

I'm trying to test my generator, which, using composeWith(), relies on the sub-generator generator-express:

this.composeWith(require.resolve('generator-express/app'), {dirname: this.props.app_name, createDirectory: 'n'});

This is an excerpt from my test with the mock dummy generator:

var deps = [
      [helpers.createDummyGenerator(), require.resolve('generator-express/app')]
];

describe('generator-cesium/app Test', () => {
    beforeEach(() => {
        return helpers.run(path.join(__dirname, '../generators/app'))
            .inDir(path.join(__dirname, 'tmp'))
            .withPrompts({
                viewerContext: '/',  // eslint-disable-line camelcase
                appName: 'app'       // eslint-disable-line camelcase
            })
            .withGenerators(deps)
    });
...

But, I keep getting the following error:

Error [ERR_UNHANDLED_ERROR]: Unhandled error. (TypeError: Cannot read property 'match' of undefined)

      at Immediate.setImmediate (node_modules/generator-express/node_modules/yeoman-generator/lib/index.js:406:18)

The error, indicating that the sub-generator is still being executed, leads me to believe that the mock is not actually working?

Infest answered 23/2, 2019 at 8:30 Comment(0)
F
0

I think the deps array only works for legacy style generators using names alias instead of path names.

Using require.resolve(), I think the easiest way to mock a generator would be use a tool like jest.mock, proxyquire or mockery.

For example:

jest.mock('../generators/app', () => {
    const helpers = require('yeoman-test');
    helpers.createDummyGenerator();
});
Freesia answered 27/2, 2019 at 8:21 Comment(1)
I've had no success using the example method you've shown; my generator test fails with: TypeError: Cannot set property 'resolved' of undefined when my generator calls composeWith(...), thereby proving this method still is unable to mock the underlying generator dependency.Infest

© 2022 - 2024 — McMap. All rights reserved.