manny
manny

Reputation: 317

How to read a file before runing a set of mocha test

I am trying to read a file which contains all the inputs for a set of mocha test. While trying to read these file i just get that the inputs(a Map) is null.

Scrapper_cheerio.js is written in the async/ await format however, don't believe that has anyting to do with a file not being read.

if I understand before correctly, the stuff in the before block should happen before any test occur. Currently this is what I have tried :

const assert = require('chai').assert;
const fs = require('fs');

const scrapper_cheerio = require('../scrapper/Scrapper_cheerio');
describe('test1', function(){
    var inputs = null;
    before( function(done){
        fs.readFile('./inputs.txt', 'utf8', 
            function(err, fileContents){
                if(err) throw err;
                const string_data = fileContents;
                const data = eval(string_data);
                inputs = new Map(data);
                done();
            });
    });

    describe('is_null_input()', function(){
        const is_null_input = inputs.get('is_null_input');
        const json_result = scrapper_cheerio.is_null_json(is_null_input);
        it('should return a json string', function(){
            assert.isObject(json_result, 'is json object');
       }); 
    });

});

I am just expecting this first test to be true because it happens to be the easiest to test however, I cannot read the file. I am very new to mocha testing hence I may, and probably do have some glaring and obvious errors. Any and all help is greatly appreciated.

Edit 1 :

so it was pointed out before quickly being erased (why it was really helpful, thank you.) that I need to do an async call and i did. I can now read the file and I can see that I'm reading it however I cannot pass the value to the bottom

    describe('test1', function(done){
        var inputs = null;

        before('test1', function() {
            const string_data = fs.readFileSync('./inputs.txt', 'utf8');
            const data = eval(string_data);
            console.log('data uptop')
            inputs = new Map(data);
            console.log(inputs);
            done();
        });

        describe('is_null_input()', function(done){
            const is_null_input = inputs.get('is_null_input');
            var json_result = scrapper_cheerio.is_null_json(is_null_input);
            console.log('these are json results', json_result);
            it('should return a json string', function(){
                assert.isObject(json_result, 'is json object');
        }); 
        });

    });

So I can read the file however it is not reachable in the describe.

Upvotes: 3

Views: 2755

Answers (1)

Brian Adams
Brian Adams

Reputation: 45810

Here is a simplified working test that should get you jumpstarted:

inputs.txt

[[ 'is_null_input', true ]]

code.test.js

const fs = require('fs');
const expect = require('chai').expect;

describe('test1', function () {
  var inputs = null;

  before('test1', function (done) {
    fs.readFile('./inputs.txt', 'utf8', (err, data) => {
      if (err) throw err;
      inputs = new Map(eval(data));
      done();
    });
  });

  describe('is_null_input()', function () {
    it('should return a json string', function () {
      const is_null_input = inputs.get('is_null_input');
      expect(is_null_input).to.equal(true);  // Success!
      // ...
      // var json_result = scrapper_cheerio.is_null_json(is_null_input);
      // console.log('these are json results', json_result);
      // assert.isObject(json_result, 'is json object');
    });
  });

});

Details

The Mocha run cycle runs all describe callbacks first, and describe callbacks always run synchronously (you can't use done, an async function, or return a Promise in a describe callback).

So this line:

const is_null_input = inputs.get('is_null_input');

...was running before the before, meaning inputs was still null.

Moving that code inside of the test means it will run after the before and inputs will be defined.

It is best practice to not block the JavaScript event loop with readFileSync, so the example above uses done in combination with readFile.

Upvotes: 1

Related Questions