Rooster Regulation (Mocha Testing) - Rooster not declared as a variable

Hi,

I’m currently working through the Rooster Regulation exercise:
https://www.codecademy.com/paths/web-development/tracks/test-driven-development-javascript/modules/learn-mocha-and-assert/projects/rooster-regulation)
In the file index.js provided by the exercise we have an object Rooster defined. Instead of using object literal, Rooster is defined as an empty object. Then its two methods are created and defined one by one.
Finally, at the bottom of the file the Rooster module is exported.

My question:
Why is Rooster not declared as a variable with ‘const’? Like this…

const Rooster = {};

Instead, in the first line of code it goes like this…

// Define a rooster
Rooster = {};

// Return a morning rooster call
Rooster.announceDawn = () => {
  return 'moo!';
}

// Return hour as string
// Throws Error if hour is not between 0 and 23 inclusive
Rooster.timeAtDawn = (hour) => {
  if (hour < 0 || hour > 23) {
    throw new RangeError;
  } else {
    return hour.toString();
  };
}

module.exports = Rooster;

I though that perhaps it’s part of the exercise (find and correct errors in the code provided, etc.) but then the first test I was supposed to write for one of Rooster methods actually finds it and executes it. I know it’s a fail but why is the test even being conducted? Shouldn’t I be presented with ReferenceError stating that Rooster is undefined?

const assert = require('assert');
const Rooster = require('../index');

describe('Rooster', () => {
  describe('.announceDawn', () => {
    it('returns a rooster call', () => {
      const expected = 'cock-a-doodle-doo!';
      const actual = Rooster.announceDawn();
      assert.strictEqual(actual, expected);

    });
  });
});

Mocha test result:

 1) Rooster .announceDawn returns a rooster call:

      AssertionError: 'moo!' === 'cock-a-doodle-doo!'
      + expected - actual

      -moo!
      +cock-a-doodle-doo!
      
      at Context.it (test/index_test.js:9:14)

Hello @michal3926405177.

Defining Rooster without using const or even let (much less desirable IMO) is allowed when not in strict mode. If you add 'use strict' to the top of the code, you’ll see the error you were expecting, and you can fix it by adding const in front of Rooster.

'use strict';
// Define a rooster
const Rooster = {}; //const, let or var must now be used

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode for more information.

Hi midlindner,

Thanks for the reply. That would explain it, however I thought that module scripts are always in ‘strict mode’ by default…

Depends on the environment. In the CC Learning Environment, strict mode is not invoked by default, as you’ve discovered.

1 Like

I see. Thanks for the explanation.

1 Like