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: A Testing Test

I'm working through JavaScript Unit Testing and am stuck on the challenge called A Testing Test. The question asks you to write a test spec that proves the clone function returns an object where all properties match with the code below. I have no idea where to begin with this. Any help would be appreciated!

var expect = require('chai').expect
describe('clone', function () {
    var clone = require('./clone.js')
    it('some description string', function () {
        // YOUR CODE HERE
    })
})

And here is the function

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

1 Answer

Hi Kelsea,

Given the following function:

function clone(objectForCloning) {
    return Object.assign({}, objectForCloning);
}
module.exports = clone;

We can see that it should return an object that is a clone of the object you pass in as a parameter. Looking at the Chai documentation, can you find a method that would help us test if the object you pass in has the same key and value as our output?

=========SPOILER=========

The property method is one we can use.

// .property(name, [value])
var obj = { foo: 'bar' };
expect(obj).to.have.property('foo', 'bar');

Then we can test like this:

var expect = require('chai').expect
describe('clone', function () {
    var clone = require('./clone.js');
    it('should return an object where all properties match', function () {
        expect(clone({a: 1})).to.have.property('a', 1);
    });
});

Resources:

Chai Docs

MDN - Assign

Thank you so much!!

I know this is bringing back a dead thread but I have tried

expect(JSONOBJECT).to.deep.equal(clone(JSONOBJECT));

and this fails the challenge. IS there a reason for this does this not test if they are the same object?