1

I'm developing a simple Javascript Client API for my unit tests, as I'm studying TDD and learning better by doing things.

I'm following a model where they will only be a CHECK by test, I have my TEST function, which is where all the tests of a given file will be, and each test will be called by TEST_F with only one CHECK function each, thus not needing a description for each TEST_F, since with just a CHECK, it is simple and easy to understand with just a good "nameTest".

The problem I'm having is to use the Javascript spread, I actually know well how to solve my problem without it, but I would like to understand if it could help me simplify things here. The TEST function, as I said, might get several TEST_F functions as arguments, so I thought I'd do something like const TEST = (... f) => {};, but I'm not sure how to use each "f" argument since each TEST_F function returns me an object, which I want to use to accuse the TEST_Fs that fail. I will try to explain what I try to do with the code below that we know will not work, but only to understand where I am trying to get:

/* --------------------------------------------------------------- */
/* ------------------- Test API ---------------------------------- */
/* --------------------------------------------------------------- */

const TEST = (fileName, ...f) => {
    const passing = [];
    const failing = [];
    console.log('Running unit tests in '+fileName+':');

    const tStart = performance.now();

    const result = ...f(); // I know it's not possible, but you understand what I'm trying to do?
    result.resultTest==='passed'?passing.push(result):failing.push(result);

    const tEnd = performance.now();
    const duration = tEnd - tStart;

    const lenPassing = passing.length;
    const lenFailing = failing.length;
    console.log('Passing: '+lenPassing+' ('+duration+'ms)');
    console.log('Failing: '+lenFailing);

    if(lenFailing > 0){
        let stg = '';
        for(let i = 0; i < lenFailing; i++){
           stg += (i + ') ' + failing[i].nameTest + ' (' + failing[i].durationTest + ')' + '\n');
        }
        console.log(stg);
    }
};

const TEST_F = (nameTest, f) => {
    const tStart = performance.now();
    const resultTest = f();
    const tEnd = performance.now();
    const durationTest = tEnd - tStart;
    return { nameTest: nameTest, durationTest: durationTest, resultTest: resultTest }; 
};

const CHECK_EQUAL = (value, expected) => {
    return ((value === expected)?'passed':'failed');
};

export {
    TEST,
    TEST_F,
    CHECK_EQUAL
};

Up 1:

How would I solve my problem without using the spread? creating a TEST object that contains an array of TEST_F and then would create a function to run the tests, something like EXECUTE_TEST, but what I want to avoid is having to call a function always in my test files, I want something simple like:

TEST("main.js",
    TEST_F("test1", () => {
        return CHECK_EQUAL(3, 3);
    }),
    TEST_F("test2", () => {
        return CHECK_EQUAL(7, 3);
    })

);

which I was able to solve with the @TJ Crowder answer:

 for(let fn of f) {
        const result = fn;
        result.resultTest==='passed'?passing.push(result):failing.push(result);
    }
PerduGames
  • 1,108
  • 8
  • 19
  • *"I know it's not possible, but you understand what I'm trying to do?"* No, I'm afraid not. The text in the question doesn't seem to say, either. What are you trying to do? Call each function? Passing it what? Expecting what back? It looks like you're expecting an object, but... Why would you try to use spread notation to do that? (Note: `...` isn't, and can't be, an operator, neither when used for spread nor rest. It does things operators cannot do. [Details](https://stackoverflow.com/questions/44934828/is-foo-an-operator-or-syntax/44934830#44934830).) – T.J. Crowder Sep 11 '18 at 15:53
  • You're saying : "I actually know well how to solve my problem without it". Please give an example of this, so we can have a better view of what you are trying to do – Logar Sep 11 '18 at 15:57
  • I have edited the question. – PerduGames Sep 11 '18 at 16:30

1 Answers1

1

If your goal is to call each function in the f array and get the results from them into the passing or failing arrays, I'd probably use a simple for-of:

for (const fn of f) {
    const result = fn();
    result.resultTest==='passed'?passing.push(result):failing.push(result);
}

or forEach or similar, any of the array looping mechanisms would do the job.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875