Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Unit Testing Behavior Driven Development with Mocha & Chai A Testing Test!

Arthur Berg
Arthur Berg
11,350 Points

Chai & Mocha, help!

I'm a bit lost in this course. I think it's moving to rapidly and that the ship game example is a bit too much for a starter project. Would like some more slow in depth talking about the theory. Anyways, I need help with this challenge. Can anyone show me the correct code and a little bit of explanation, would be awesome.


var expect = require('chai').expect

describe('clone', function () {
    var clone = require('./clone.js').clone
    it('some description string', function () {
        var obj = {firstname: "Arthur"}

        expect(clone(obj)).to.have.deep.property("firstname", "Arthur")

function clone (objectForCloning) {
    return Object.assign({}, objectForCloning)

module.exports = clone

1 Answer

Philip Cox
Philip Cox
14,818 Points

Does this code work? What exactly are you confused about?

Unit testing is an important part of software engineering, it helps you to prove your code works and you can also rerun the tests when you make changes for regression testing purposes (to make sure previous code still works).

When testing a unit of code you are trying to understand if the piece of code does as you expect, with your function you're expecting to return a new object with the same structure as the current object. Object.assign will return a new object, it takes two arguments, the first the object to return, the second the object with properties to copy to the new object. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

However, you are not expecting it to return the same object reference, but the returned object will 'look' the same. expect.clone({a: 'a'}).toBe({a: 'a'}) <-- something along these lines maybe