Convert C to F challenge (Trying Unit Testing...)

Convert C to F challenge (Trying Unit Testing...)
0.0 0

#1

This is more a question about unit testing.

As I’m going through JS on FCC I want to try and write unit tests because I’m trying to learn that too. I would just appreciate some feedback about my unit test for the Convert Celsius to Fehrenheit exercise. It’s the first test I wrote!

Am I on the right track? Any suggestions?

All the tests passed.

I used Jasmine so I can do browser/client side testing. Here’s my code:

function convertToF(celsius) {
    var fahrenheit;
    // Only change code below this line
    fahrenheit = celsius * 1.8 + 32;
    
    // Only change code above this line
    return fahrenheit;
  }
  
  // Change the inputs below to test your code
convertToF(30);

Here is my unit test. I used the same tests given in the exercise:

describe("Convert Celcius to Farheinheit", () => {
    it("should return a number", () => {
        let n = convertToF(0);
        expect(typeof n).toBe("number");
    }),
    it("should return -22 when given -30", () => {
        expect(convertToF(-30)).toEqual(-22);
    }),
    it("should return 14 when given -10", () => {
        expect(convertToF(-10)).toEqual(14);
    }),
    it("should return 32 when given 0", () => {
        expect(convertToF(0)).toEqual(32);
    }),
    it("should return 68 when given 20", () => {
        expect(convertToF(20)).toEqual(68);
    }),
    it("should return 86 when given 30", () => {
        expect(convertToF(30)).toEqual(86);
    })
})

#2

Sorry man. But it just the first one. and again the algorithm is already provided in the challenge so you just had to implement it to the code . that’s it. There’s nothing to give feedback on. Keep on track the hard ones are coming.:metal:


#3

I know that it will test it in the browser. My post is about this:

I’m trying to also learn how to do unit testing myself. So for this exercise, while it’s trivial, provides a good opportunity to try and write a unit test. I pulled everything into my code editor and did it there. Took a lot longer. But I want to learn as it’s valuable.

I’m just simply asking for those who know about unit tests to give me some feedback.


#4

Hey @dcookwebdev. I figured the post wasn’t about the challenge but more about the unit test you wrote which is good by the way. I haven’t done much testing in jasmine, I use mocha and chai but all of them are similar in a way. So except you’re trying to test some deeper edge cases, your tests are OK.

Keep up the good work and happy coding!:+1:


#5

Thanks for the feedback! Appreciate it!
I was looking to use Mocha, but went with Jasmine as it seems easier somehow to do client side testing!


#6

Oh my bad man. Your title says just "C to F" which misled me to believe that you were talking about the algorithm implementation of this challenge. again sorry mate , my bad :):hugs:


#7

It looks good @dcookwebdev. I would try some more extreme test cases, such as negatives, and really high values. Try some other data types also. Maybe check for NaN? Or a float? You don’t want too many tests repeating the same thing.

Also Jasmine looks cool I want to give it a shot. I used facebooks unit test, but this looks better.


#8

Thanks @Emgo-Dev! That’s helpful feedback. I see what you mean by doing extremes and checking for other types. For now I was just copying what the exercise tests were. It’s a good point of not having too many tests repeating the same thing.

I used the standalone - it’s basically a webpage. Easy.


#9

That’s exactly what I need. Do you need to export modules from your main script? With the node version it requires you export the functions you want to use with the Node command line, which is annoying because if you want to use that script file elsewhere, the modules variable ends up throwing undefined errors.


#10

No exporting!

So if you grab that standalone zip you’ll see the example HTML file. It links to a bunch of stuff in the folder. Take a peek in the HTML file. I think it’s called specrunner.html.

You can leave most of it as is.
Change the source file (your Javascript)
And change the spec files (test scripts)
Easy peasy!