Generator custom test: creating files

I have a very simple youma generator watchjs

that has a subgenerator speaker

. Below is used hos:

$ yo watchjs:speaker You called the watch.js speaker subgenerator. ? Speaker file: data/speakers/speakers.json ? Speaker name: abc { file: 'data/speakers/speakers.json', name: 'abc' } Generated slug is: abc Trying to add: { "id": "abc", "name": "abc" }

Basically, there are two requests: file

- which defines the json file to which the data should be added, and name

- which defines the actual data to be added to the file (slightly modified). I am trying to write a simple exam for this. I am trying to follow the docs , but I am failing all the time:

$ npm test

> generator-watchjs@0.0.2 test c:\Users\tomasz.ducin\Documents\GitHub\generator-watchjs
> mocha

  Watchjs:speaker
{ file: 'speakers.json', name: 'John Doe' } // <- this is my console.log
    1) "before all" hook

  0 passing (59ms)
  1 failing

  1) Watchjs:speaker "before all" hook:
     Uncaught Error: ENOENT, no such file or directory 'C:\Users\TOMASZ~1.DUC\AppData\Local\Temp\53dac48785ddecb6dabba402eeb04f91e322f844\speakers.json'
      at Object.fs.openSync (fs.js:439:18)
      at Object.fs.readFileSync (fs.js:290:15)
      at module.exports.yeoman.generators.Base.extend.writing (c:\Users\tomasz.ducin\Documents\GitHub\generator-watchjs\speaker\index.js:43:33)

npm ERR! Test failed.  See above for more details.

      

I can't figure out where the file was actually created and where the tests that look for it are located ... It seems that the temporary location of the windows is used, but in any case, if everything works correctly relative to the path, the file should have been found, and it is not ... Can't figure out what to do to pass the tests.

Best content of my test file:

'use strict';

var path = require('path');
var assert = require('yeoman-generator').assert;
var helpers = require('yeoman-generator').test;

describe('watchjs:speaker', function () {
  before(function (done) {
    helpers.run(path.join(__dirname, '../speaker'))
      .withOptions({ 'skip-install': true })
      .withPrompts({ 'file': 'speakers.json', 'name': "John Doe" })
      .on('end', done);
  });

  it('creates files', function () {
    assert.file([
      'speakers.json'
    ]);
  });
});

      

I pass the specific name name

and file

through the prompt.

I figured out what npm test

the package.json is calling mocha

(and this). But I'm not a mocha expert.

I am using node v0.10.35 on Windows7.

+3


source to share


1 answer


First, you must use absolute paths in your test, so the file location is predictable.

My test will look something like this:

'use strict';

var fs = require('fs');
var path = require('path');
var assert = require('yeoman-generator').assert;
var helpers = require('yeoman-generator').test;

describe('watchjs:speaker', function () {
  before(function (done) {
    var self = this;
    var name = 'John Doe';
    var testPath = path.join(__dirname, 'temp');

    // store in test obejct for later use
    this.filePath = path.join(testPath, 'speaker.json');

    helpers.run(path.join(__dirname, '../speaker'))
      .inDir(testPath)
      .withPrompts({ 'file': self.filePath, 'name': name })
      .withOptions({ 'skip-install': true })
      .on('end', done);
  });

  it('creates files', function () {
    assert.file(this.filePath);
    assert.fileContent(this.filePath, /\"id\":.*\"john-doe\"/);
    assert.fileContent(this.filePath, /\"name\":.*\"John Doe\"/);
  });

});

      

Second, and not directly related to your question, the test above will be on the code in the repo that you shared. As I mentioned in my comment, it throws an error here if the file doesn't exist yet.

I would change:



var content = JSON.parse(fs.readFileSync(this.options.file, 'utf8'));

      

in

try {
  var content = JSON.parse(fs.readFileSync(this.options.file, 'utf8'));
} catch(e) {
  content = [];
}

      

With the change above, the test will pass.

0


source







All Articles