dimanche 22 octobre 2017

testing array and function mocha javascript - how to get access to function and write tests

i have this code to write test in index.js file:

'use strict'
const activeModules = [
    { name: 'module 1' },
    { name: 'module 2' },
    { name: 'module 11' },
    { name: 'module 3' },
    { name: 'module 10' }
];
const getCustomModuleNumber = _ =>
    Math.max(...activeModules.map( n =>
        n.name.match("\\d+")
    ));

I want to write test in mocha chai but i don't know how i can have access to function in test/appTest.js

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

const app = require('../index');

activeModulesResult = app.activeModules;
getCustomModuleNumberResult = app.getCustomModuleNumber;

describe('App', function() {
    it('activeModules should be type array', function() {
        assert.typeOf(activeModulesResult, 'array');
    });

    it('getCustomModuleNumber should be type number', function() {
        assert.typeOf(getCustomModuleNumberResult, 'number');
    });
})

The console shows the error with getCustomModuleNumber is undefined or when I add getCustomModuleNumber() shows that is not a function.

1. My project contains only index.js file and files needed for testing. What is the best way to test this and why? Maybe i need to add to index.js file module.exports? which solution will be the most optimal and consistent with the principles of clean code and ES6?

2. What tests should I write? I know I should check if the getCustomModuleNumber function returns 11, the type returned by it, but I completely do not know if I should check also the other components of this function?

Aucun commentaire:

Enregistrer un commentaire