Toggle navigation
TechnoWikis.com
Email or Username
Password
Remember
Login
Register
|
I forgot my password
All Activity
Questions
Hot!
Pending
Ask a Question
Privacy Policy
Contact
JavaScript tests with Jasmine
Home
Tutorials
Development
JavaScript
JavaScript tests with Jasmine
(adsbygoogle = window.adsbygoogle || []).push({});
+3
votes
233
views
JavaScript tests with Jasmine
asked
Jun 24, 2019
in
JavaScript
by
backtothefuture
(
552k
points)
reopened
Jun 24, 2019
|
233
views
answer
Your answer
Your name to display (optional):
Email me at this address if my answer is selected or commented on:
Email me if my answer is selected or commented on
Privacy: Your email address will only be used for sending these notifications.
Add answer
Cancel
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:
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:
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.
answered
Jun 24, 2019
by
stackoverflow
(
3.5m
points)
edited
Jun 24, 2019
ask related question
comment
Your comment on this answer:
Your name to display (optional):
Email me at this address if a comment is added after mine:
Email me if a comment is added after mine
Privacy: Your email address will only be used for sending these notifications.
Add comment
Cancel
Related questions
+5
votes
1
answer
Interactive graphics with JavaScript and Highcharts
asked
Jun 24, 2019
in
JavaScript
by
backtothefuture
(
552k
points)
|
256
views
+4
votes
1
answer
Spacetree with JavaScript
asked
Jun 23, 2019
in
JavaScript
by
backtothefuture
(
552k
points)
|
295
views
+4
votes
1
answer
JavaScript Tutorial - Google Maps
asked
Jun 23, 2019
in
JavaScript
by
backtothefuture
(
552k
points)
|
224
views
+4
votes
1
answer
How to export an HTML table to Excel with JavaScript?
asked
Nov 18, 2021
in
Guides
by
backtothefuture
(
552k
points)
|
122
views
+5
votes
1
answer
How to update NPM with PowerShell in Windows 10
asked
Nov 7, 2019
in
JavaScript
by
backtothefuture
(
552k
points)
|
574
views
Sponsored articles cost $40 per post. You can contact us via
Feedback
All categories
Tutorials
7.5k
Microsoft
1.9k
Android
1.4k
Security
120
Linux / Unix
543
Internet
757
Virtualization
104
Apple
611
Networks
64
Other Devices
321
Other Applications
184
Hardware
19
Development
53
HTML5 / CSS3
12
Web Servers
9
JavaScript
12
Other languages
2
PHP
8
CMS
4
Python
5
WebServers
1
Digital Marketing
47
Databases
14
Graphic Design
30
Guides
794
GraphicDesign
54
Networking
4
PlayStation
186
Gaming
55
Linux/Unix
85
Manzana
33
Otherdevices
38
Otherapps
49
Digitalmarketing
39
Safety
1
Developing
2
Help
685
Social Networks
34
Android Tutorial
549
iPhone Tutorial
267
News
17
Social
6
Phone
11
Telephone
9
Applications
167
Smartphones
3
Cell Phones
11
Applications
25
Travels
6
Photo
21
Education
18
Games
25
Internet
14
Music
8
Technique
10
Video
6
Windows
5
Apple
2
Cell Phones
3
TikTok
216
Trips
2
House
1
Operating System
5
Tips & Tricks
892
Solutions
6
Tutorials
3
FAQ
1
Applications
5
Cell Phones
2
Tutorials
4
Computers
6
Tutoriales
2
8
Technology
2
In Computers
1
In Applications
2
Tutorials Tutorials
9
Applications Applications
25
Aplicaciones Applications
4
Devices Devices
5
Tutoriales Tutorials
1
Tutorials u00a0 Tutorials
2
Applications u00a0 Applications
2
Devices u00a0 Devices
1
OS OS
1
Etc Etc
2
Most popular questions within the last 30 days
Please leave a comment about whether the solution works or not (with device model)
[X]Close
10,659
questions
10,791
answers
510
comments
3
users