Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7650759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:20:08+00:00 2026-05-31T11:20:08+00:00

I’m having problems testing a flatiron cli app with Mocha . The command-line command

  • 0

I’m having problems testing a flatiron cli app with Mocha.

The command-line command I’d like to test creates a directory and logs success with app.log.info.

This is the code to be tested (./lib/commands/create.js):

var flatiron = require('flatiron'),
  app = flatiron.app,
  fs = require('fs'),
  path = require('path');

module.exports = function create(name, callback) {
  "use strict";
  fs.mkdir('./' + name);
  app.log.info('Directory created!');
}

This is the test (./test/create.js):

var create = require('../lib/commands/create');

describe('Flatiron command', function () {
  "use strict";
  describe('#create()', function () {
    it('should create a directory ', function () {
      create('someDirectory');
      // check if the directory was created,
      // then remove the directory
    });
  });
});

mocha test/log -R spec gives me

  Flatiron command
    #log()
      1) should log something


  ✖ 1 of 1 tests failed:

  1) Flatiron command #create() should create a directory :
     TypeError: Cannot call method 'info' of undefined

Why is app.lognot available to Mocha?

Is this because of how function logis exported?

Or has this something to do with how flatiron sets up the application? I tried requiring flatiron.app and starting it from the test like this

var create = require('../lib/commands/create'),
  flatiron = require('flatiron'),
  app = flatiron.app;

describe('Flatiron command', function () {
  "use strict";
  describe('#create()', function () {
    it('should create a directory ', function () {
      app.start();
      create('someDirectory');
    }); 
  }); 
});

– but with no success, just a different error:

  Flatiron command
    #create()
      1) should create a directory


  ✖ 1 of 1 tests failed:

  1) Flatiron command #create() should create a directory :
     TypeError: Object [object Object] has no method 'start'

Or is this a case where you would use spies/stubs/mocks with something like sinon.js to simulate the behavior of app.log somehow? Because I’m not really interested if the logging works, but if the directory is created.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:20:09+00:00Added an answer on May 31, 2026 at 11:20 am

    Ok, I’ve got it.

    Using app.start() was not quite it – but it works with app.init().
    In flatiron, app.init() is normally called from within the main file by plugging flatiron.plugins.cliinto app.use() like this:

    var flatiron = require('flatiron'), 
        path = require('path'), 
        app = flatiron.app; 
    
    app.config.file({ file: path.join(__dirname, '..', 'config', 'config.json') }); 
    
    app.use(flatiron.plugins.cli, { 
      dir: path.join(__dirname, '..', 'lib', 'commands'), 
      usage: 'Empty Flatiron Application, please fill out commands' 
    }); 
    app.start(); 
    

    Calling app.init() sets up logging with winston, the flatiron logging plugin.

    But you can call app.init()from within the test without calling app.start() after it.
    So this works:

    var create = require('../lib/commands/create'),
      flatiron = require('flatiron'),
      app = flatiron.app;
    
    describe('Flatiron command', function () {
      "use strict";
      describe('#create()', function () {
        it('should create a directory ', function () {
          app.init();
          create('someDirectory');
        });
      });
    });
    

    Mocha even takes care of the logging:

      Flatiron command
        #create()
          ◦ should create a directory : info: Directory created!
          ✓ should create a directory  (48ms)
    
    
      ✔ 1 tests complete (50ms)
    

    If you want to stop the logging, you can use app.log.loggers.default.remove(winston.transports.Console)after you’ve called app.init(). You have to require winston to do this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.