vendredi 5 mai 2017

Testing for file generation

I am writing a test suite for a file generation module. For example,

calling generator('test') will create a file in a specific directory named test. Here is how I set up mocha/chai tests.

let chai = require('chai');
let expect = chai.expect;
chai.use(require('chai-fs'));
let Promise = require('bluebird');
let rm = require('shelljs').rm;

let generator = Promise.promisify(require('../../commands/generator.js'));


describe('Generator', () => {
  afterEach(() => {
    rm('-rf', 'frontend');
  });

  it('should generate a file', () => {
    Promise.resolve(generator('test', 'test', [])).then(() => {
      expect('src/test.js').to.be.a.file();
    });
  });
});

I am using shelljs to remove the file after each test. However, I am running into an error

1) Generator "after each" hook:

Uncaught Error: EBADF: bad file descriptor, write at Error (native)

I think this is caused by the fact that rm is an async function, but I'm not sure how to fix this issue.

There are multiple individual tests for the generator, and I want to remove the generated files after each test. Is there a better way to do this?

Aucun commentaire:

Enregistrer un commentaire