+3 votes
243 views
JavaScript tests with Jasmine

in JavaScript by (552k points)
reopened | 243 views

1 Answer

+4 votes
Best answer

Now, we've seen what Jasmine is about now let's see how it works.
  • Download the latest version of Jasmine and unzip it.
  • We enter the Jasmine directory and run SpecRunner.html and we will see the following:

image


This file runs some tests in an example code, if we want to run the tests we can refresh the browser and they will run.
Now let's see an example of Jasmine's use:
  • First we create a simple function that contains a string as a return call.
function helloWorld () {
return "Hello world!";
}

We're pretty sure this works, right? But let's try this with Jasmine and see what she thinks of our code.
  • We save the created function in a file that we can call hello.js , open SpecRunner.html to make the inclusion.
<! - this code must go somewhere in <head> ... ->
<script type = "text / javascript" src = "src / hello.js"> </ script>
  • Now let's put Jasmine to work, create a file that contains the following code:
describe ("Hello world", function () {
it ("says hello", function () {
expect (helloWorld ()). toEqual ("Hello world!");
});
});

Let's see what contains this portion of code:

First we have the describe, which usually defines a component of our application, it can be a class, function or maybe something else. In this example, it refers to our helloWorld () function.

Let's continue in the same code and we will have the block of it () , this is called a specification. It is a function in Javascript that tells us what to expect or what our component should do. For each describe we can have any number of specifications.

In this case we are testing if the helloWorld () function returns "Hello world!" And we verify it with the toEqual () that is not more than a matcher , this will basically tell us if the content of the string is the same with what is being testing.
  • We save the code with the name hello.spec.js we place it in the directory where the specifications are contained and we include it in our SpecRunner.html
<! - this code must go somewhere in <head> ... ->
<script type = "text / javascript" src = "spec / hello.spec.js"> </ script>

Finally we run this specification in our browser and we will see an output like the one we have below:


image


We can also use another type of matcher, let's see:

describe ("Hello world", function () {
it ("says world", function () {
expect (helloWorld ()). toContain ("world");
});
});

Instead of expecting the value to be the same, this time we expect the content to be the word "world" regardless of the rest of the content, as long as the word "world" exists in this test Jasmine will interpret it as correct.
If we go to our function we can change what it says and put something different to "Hello World", Jasmine will see this as incorrect because it is not what she is waiting for and that is the main reason why we want Jasmine, so that we tell when something is not expected and this will help us to make our code clean and without errors.

by (3.5m points)
edited

Related questions

+5 votes
1 answer
asked Jun 24, 2019 in JavaScript by backtothefuture (552k points) | 259 views
+4 votes
1 answer
asked Jun 23, 2019 in JavaScript by backtothefuture (552k points) | 308 views
+4 votes
1 answer
asked Jun 23, 2019 in JavaScript by backtothefuture (552k points) | 228 views
+4 votes
1 answer
asked Nov 18, 2021 in Guides by backtothefuture (552k points) | 131 views
+5 votes
1 answer
asked Nov 7, 2019 in JavaScript by backtothefuture (552k points) | 575 views
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users