Quality Assurance with Chai Test if a Value Falls within a Specific Range

Hello.

I can’t pass the “Test if a Value Falls within a Specific Range” Challenge on Glitch.

This is the right solution, but it doesn’t pass the test:

test('#approximately', function() {
  assert.approximately(weirdNumbers(0.5), 1, /*edit this*/ 0.5);
  assert.approximately(weirdNumbers(0.2), 1, /*edit this*/ 0.8);
});

Could you please help. I can’t move on to the next challenges without passing this one.

Thank you very much in advance for your help.

Elena

you can go to any challenge you want by going to the list of challenges and choosing the one you want to do

but, anyway, please provide the challenge link, and a link to your code

Hello there,

You are likely having issues as the fCC test is:

assert.equal(data.assertions[0].args[2], 0.5...

So, you having this in your function call:

/*edit this*/

Is likely causing the tests to fail. Remove the comment.

Hope this helps.

Thank you for your answer, @ieahleen.

Surprisingly, the next challenges fail too.

Challenge link https://www.freecodecamp.org/learn/information-security-and-quality-assurance/quality-assurance-and-testing-with-chai/test-if-a-value-falls-within-a-specific-range

This is my code. It passed all the previous tests:

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

suite('Unit Tests', function(){
  
  // Make ALL tests pass
  // !! Don't scramble the Assertions. We rely on their order to check the results !!
  suite('Basic Assertions', function() {
    /** assert.fail() will always fail. Change it into something more useful... **/
  
   
    /** 1 - Use assert.isNull() or assert.isNotNull() to make the tests pass. **/
test('#isNull, #isNotNull', function() {
  assert.isNull(
    null,
    'this is an optional error description - e.g. null is null'
  );
  assert.isNotNull(1, '1 is not null');
});
  
    /** 2 - Use assert.isDefined() or assert.isUndefined() to make the tests pass. **/
    test('#isDefined, #isUndefined', function() {
    assert.isDefined(null, 'null is not undefined');
    assert.isUndefined(undefined, 'undefined IS undefined');
    assert.isDefined('hello', 'a string is not undefined');
});
    /** 3 - Use assert.isOk() or assert.isNotOk() to make the tests pass. **/
    // .isOk(truthy) and .isNotOk(falsey) will pass
   test('#isOk, #isNotOk', function() {
  assert.isNotOk(null, 'null is falsey');
  assert.isOk("I'm truthy", 'a string is truthy');
  assert.isOk(true, 'true is truthy');
});
  
    /** 4 - Use assert.isTrue() or assert.isNotTrue() to make the tests pass. **/
    // .isTrue(true) and .isNotTrue(everything else) will pass.
    // .isFalse() and .isNotFalse() also exist.
// .isFalse() and .isNotFalse() also exist.
  test('#isTrue, #isNotTrue', function() {
  assert.isTrue(true, 'true is true');
  assert.isTrue(!!'double negation', 'double negation of a truthy is true');
  assert.isNotTrue(
    { value: 'truthy' },
    'A truthy object is NOT TRUE (neither is false...)'
  );
});
  
    // There are more assertions like these: .isNaN(), .isBoolean(), 
    // and many others. Almost all the assertions in the chai library
    // have their negative counterpart - e.g. .isNotBoolean(), ...
  });
  
  // -----------------------------------------------------------------------------
  
  suite('Equality', function() {
    /** 5 - .equal(), .notEqual() **/
    // .equal() compares objects using '=='
    test('#equal, #notEqual', function() {
    assert.equal(12, '12', 'numbers are coerced into strings with == ');
    assert.notEqual({ value: 1 }, { value: 1 }, '== compares object references');
    assert.equal(6 * '2', '12', 'no more hints...');
    assert.notEqual(6 + '2', '12', 'type your error message if you want');
});
    /** 6 - .strictEqual(), .notStrictEqual() **/
    // .strictEqual() compares objects using '==='
    test('#strictEqual, #notStrictEqual', function() {
    assert.notStrictEqual(6, '6');
    assert.strictEqual(6, 3 * 2);
    assert.strictEqual(6 * '2', 12);
    assert.notStrictEqual([1, 'a', {}], [1, 'a', {}]);
});
  /** 7 - .deepEqual(), .notDeepEqual() **/
// .deepEqual() asserts that two object are deep equal
      test('#deepEqual, #notDeepEqual', function(){
      assert.deepEqual( { a: '1', b: 5 } , { b: 5, a: '1' }, "keys order doesn't matter" );
      assert.notDeepEqual( { a: [5, 6] }, { a: [6, 5] }, "array elements position does matter !!" );
    });
  });

// -----------------------------------------------------------------------------
  

  // This function is used in the tests. Don't Edit it.
  function weirdNumbers(delta) {
    return (1 + delta - Math.random());
  }

  suite('Comparisons', function() {
    
    /** 8 - .isAbove() => a > b , .isAtMost() => a <= b **/
 test('#isAbove, #isAtMost', function() {
  assert.isAtMost('hello'.length, 5);
  assert.isAbove(1, 0);
  assert.isAbove(Math.PI, 3);
  assert.isAtMost(1 - Math.random(), 1);
});

    /** 9 - .isBelow() => a < b , .isAtLeast =>  a >= b **/
 test('#isBelow, #isAtLeast', function() {
  assert.isAtLeast('hi'.length, 2);
  assert.isAtLeast(2 * Math.random(), 0);
  assert.isBelow(5 % 2, 2);
  assert.isBelow(2 / 3, 1);
});

  /** 10 - .approximately **/
// .approximately(actual, expected, range, [message])
// actual = expected +/- range
// Choose the minimum range (3rd parameter) to make the test always pass
// it should be less than 1
    test('#approximately', function() {
    assert.approximately(weirdNumbers(0.5), 1, 0.5);
    assert.approximately(weirdNumbers(0.2), 1, 0.8);
});

// -----------------------------------------------------------------------------

Thank you for your answer, @Sky020

Unfortunately, it doesn’t help :slightly_frowning_face:

Well, could you please provide a link to your project?

Thank you for willing to help, @Sky020

Here is the link https://glitch.com/edit/#!/ablaze-planet-dirigible?path=tests%2F1_unit-tests.js%3A1%3A0

When I run the test URL: https://ablaze-planet-dirigible.glitch.me/_api/get-tests?type=unit&n=9

I get this response:
{"status":"unavailable"}

Which is not what is needed. So, unless you are getting a useful output in your console, it seems the issue is with Glitch.

Thank you, @Sky020

This is what I get:

// running tests
All tests should pass.
You should use approximately(actual, expected, range) - You should choose the correct range.
You should use approximately(actual, expected, range) - You should choose the correct range.
// tests completed

Not from freeCodeCamp. I mean in your Glitch console. There should be a button on the bottom-left allowing you to view the logs.

Sorry, I see.

I have theese messages:

Blockquote
Could not find node 4.4.5, using 10

Blockquote
Tests are not valid:

Jump To/app/tests/1_unit-tests.js:229

});

SyntaxError: Unexpected end of input

at new Script (vm.js:80:7)

at createScript (vm.js:274:10)

at Object.runInThisContext (vm.js:326:10)

at Module._compile (internal/modules/cjs/loader.js:664:28)

at Object.Module._extensions…js (internal/modules/cjs/loader.js:712:10)

at Module.load (internal/modules/cjs/loader.js:600:32)

at tryModuleLoad (internal/modules/cjs/loader.js:539:12)

at Function.Module._load (internal/modules/cjs/loader.js:531:3)

at Module.require (internal/modules/cjs/loader.js:637:17)

at require (internal/modules/cjs/helpers.js:22:18)

You’re missing a closing set of parenthesis that is causing this issue.

  1. Go to file /tests/1_unit-tests.js
  2. Go to line #223
  3. Insert a closing curly brace and a parenthesis: });

It’s just formatting issue which is overlooked here probably.

Incredible ! :upside_down_face:

It did work!

Thank you very much, @husseyexplores

Thank you all for the help.

Elena

1 Like