最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - module.exports multiple functions in Jest testing - Stack Overflow

programmeradmin0浏览0评论

After reading the Jest documentation, when it's mentioned that to export a single function from a tested file they show the following example:

function sum(a, b) {
  return a + b;
}
module.exports = sum;

Now, if I have multiple specific functions I want to export on my tested file, like this:

function sum(a, b) {
  return a + b;
}
function multiply(a, b) {
  return a * b;
}
function subtract(a, b) {
  return a - b;
}
module.exports = sum;
module.exports = multiply;

The multiply function is the only one being exported. How can I make these function be exported? Or only part of my file?

After reading the Jest documentation, when it's mentioned that to export a single function from a tested file they show the following example:

function sum(a, b) {
  return a + b;
}
module.exports = sum;

Now, if I have multiple specific functions I want to export on my tested file, like this:

function sum(a, b) {
  return a + b;
}
function multiply(a, b) {
  return a * b;
}
function subtract(a, b) {
  return a - b;
}
module.exports = sum;
module.exports = multiply;

The multiply function is the only one being exported. How can I make these function be exported? Or only part of my file?

Share Improve this question asked Jun 1, 2017 at 8:40 gespinhagespinha 8,48717 gold badges59 silver badges95 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 8

You can do something like this :

module.exports = {};
module.exports.sum = function sum(a, b) {
  return a + b;
}
module.exports.multiply = function multiply(a, b) {
  return a * b;
}
module.exports.subtract = function subtract(a, b) {
  return a - b;
}

And you use it like this:

var MyMathModule = require('./my_math_module');
MyMathModule.sum(a, b);
MyMathModule.multiply(a, b);
MyMathModule.subtract(a, b);

First, in your example, all you are doing there is overriding the exports object with a function ( which is totally fine )

The exports and module.exports are an object and are actually the same object ( i.e. module.exports === exports // true )

To do what you want you can do this a couple ways:

exports.sum = sum
exports.multiply = multiply

or

module.exports = { sum: sum, multiply: multiply } // etc

or

module.exports.sum = sum
module.exports.multiply = multiply

Having in mind the answer to this question, i'll paste here 2 ways to do the same thing.

For example, you have the JS file called exercise5, like this:


//You can create an object with functions, as follows:

const wordAnalysis = {
    type: (word) => typeof (word),
    whiteSpaces: (word) => {
        let wordAnalysis = word.includes(' ')

        if (wordAnalysis) {
            return 'It has spaces'
        } else {
            return "It doesn't has spaces"
        }
    }
}

//Or you can create several single functions, like the following:

function numberAnalysis(word) {
    let isANumber = typeof (word) === 'number' ? true : false
    return isANumber
}


// în order to avoid overwriting the module.exports, it is needed to do one of the following (I chose the first one):

// 1)
module.exports.firstPlace = wordAnalysis
module.exports.secondPlace = numberAnalysis
// 2)

// module.exports = {
    // functions: functions,
    // isANumber: isANumber
// }

// 3)
// exports.functions = functions
// exports.isANumber = isANumber

// 4)

// exports = {
//     functions: functions,
//     isANumber: isANumber
// }

Now the file test named exercise5.test.js:

const wordAnalysis = require('./exercise5')
const numberAnalysis = require('./exercise5')


test('It should give me the type of what was typed', () => {
    expect(wordAnalysis.firstPlace.type('teste')).toEqual('string')
})

test('It should give me the type of what was typed', () => {
    expect(wordAnalysis.firstPlace.type(22)).toEqual('number')
})


test("It should give true if what is typed has at least a space or false if it doesn't", () => {
    expect(wordAnalysis.firstPlace.whiteSpaces('Jon is cool')).toEqual('It has spaces');
})
test("It should give true if what is typed has at least a space or false if it doesn't", () => {
    expect(wordAnalysis.firstPlace.whiteSpaces('AllTogetherNow')).toBe("It doesn't has spaces");
})

test('it should analyse if the given expression is a number or not', () => {
    expect(numberAnalysis.secondPlace(2)).toBeTruthy()
})

test('it should analyse if the given expression is a number or not', () => {
    expect(numberAnalysis.secondPlace('jon')).toBeFalsy()
})


The only thing you need to be aware is to export/import the correct object/function, and of course call it when your are developing the test.

发布评论

评论列表(0)

  1. 暂无评论