Alright, so I wanted to get some testing going on in my project, and I heard about this thing called Jasmine. I’d seen it mentioned a few times, so I figured, “Why not give it a shot?” This is my journey figuring it out, pretty much from scratch.
Getting Started (aka “What the Heck is a ‘gem’?”)
First things first, I needed to actually get Jasmine. Turns out, it’s packaged as something called a “gem”. Now, I’ve used Ruby before, but I’m no expert, so I had to do a little digging. Basically, a gem is like a pre-packaged bundle of code that you can easily install and use. Think of it like a plugin or an add-on.
So, I opened up my terminal (that black box where you type commands) and typed in:
gem install jasmine
I hit enter, and a bunch of text scrolled by. It looked like it was downloading and installing stuff. Fingers crossed, I waited for it to finish.
Setting Up My Project (aka “Making Things Tidy”)
Once the gem was installed, I needed to get my project ready. I already had my project folder, but I needed to tell Jasmine where everything was. I went back to my terminal, navigated to my project’s main folder, and typed:
jasmine init
This command created a new folder called spec, and inside that, there was a support folder, and inside that was a file called . This .yml file is like a configuration file – it tells Jasmine where to find my code and my tests.
Writing My First Test (aka “The Moment of Truth”)
Okay, time to actually write a test. I created a new file inside the spec folder. I named it something like my_first_test_* (the _spec part is important, it’s how Jasmine knows it’s a test file).
I kept the test super simple. Something like:
describe("My First Test Suite", function() {
it("should be true", function() {
expect(true).toBe(true);
I saved the file. Back in the terminal, still in my project’s main folder, I typed:
jasmine
And… it worked! I saw some green text (which is always a good sign in the testing world) telling me that my test passed. Woohoo!
Making it More Useful (aka “Testing Actual Code”)
Of course, testing if true equals true isn’t very helpful. So, I started writing tests for my actual code. This involved:
Creating JavaScript files: I had my regular JavaScript files, the ones I wanted to test.
Creating Spec Files: For each JavaScript file, I created a corresponding file in the spec folder.
Writing Tests: Inside the spec files, I used Jasmine’s functions like describe, it, expect, toBe, toEqual, etc. to check if my code was doing what it was supposed to do.
Running Tests: I’d run jasmine in the terminal to see the results. Green meant good, red meant I had some bugs to fix.
Keeping Things Organized
As I wrote more tests, I realized I needed to keep things organized. I started grouping related tests together using nested describe blocks. This made it easier to read and understand my tests, especially as the project grew.
describe("Calculator", function() {
describe("Addition", function(){
it("should add positve numbers", function() {
expect( add(2,3) ).toBe(5);
Final Thoughts (aka “Was it Worth It?”)
So, after all that, was using Jasmine worth it? Definitely! It took a little bit of setup, but once I got the hang of it, it became a really valuable tool. Being able to run tests and quickly see if my code was working correctly saved me a ton of time and headaches. It’s like having a little robot buddy that constantly checks my work for me. I’m still learning, but I’m definitely going to keep using Jasmine in my projects.