March 19, 2020
Estimated Post Reading Time ~

Introduction on Jasmine UNIT testing code coverage

It is a level of software testing where individual units/ components of the software are tested. The purpose is to validate that each unit of the software performs as designed. A unit is the smallest testable part of any software. It usually has one or a few inputs and usually a single output. In procedural programming, a unit may be an individual program, function, procedure, etc. In object-oriented programming, the smallest unit is a method, which may belong to a base/ super class, abstract class or derived/ child class. Unit testing frameworks, drivers, stubs, and mock/ fake objects are used to assist in unit testing.
Jasmine is a unit test framework, used for testing javascript code. It’s an open-source technology, easy to implement in any javascript-based application.
Jasmine follows the BDD procedure, to make sure each line of the javascript component is unit tested.

Jasmine set up in local
Download standalone plugin from Jasmine Documentation


Now create a web application folder, and include jasmine lib and SpecRuner.html into it.
make sure you remove spec and src reference from SpecRunner.html page, because we are going to write our own test case.
create helloworld.js
1.    var greet = function(){
2.     return "Hello ";
3.    }
now create a testcase file (helloworld.spec.js)
1.    describe('Hello',function(){
2.     it("should return Hello",function(){
3.                 expect(greet()).toEqual('Hello');
4.     })
5.    })
Add reference to the output file(i.e SpecRunner.html)
1.     <script src='helloworld.js'></script>
2.     <script src='helloworld.spec.js'></script>
Execute SpecRunner.html file
Hurrah it got successful, and you will see the output like below.

Suite/Test Suite: Collection of similar types of test cases written for a specific file or function known as one Suite.
Each suite will have one describe() and one/more it() init.
List of methods in jasmine.
for a complete list of methods, one can refer the documentation Global
1)describe() used as a test suite.
2)it() is used as a test case
3)expect() used as assert, like the result comparison

Documentation on Jasmine

Global

Methods

afterAll(functionopt, timeoutopt)

Run some shared teardown once after all of the specs in the describe are run.
Note: Be careful, sharing the teardown from a afterAll makes it easy to accidentally leak state between your specs so that they erroneously pass or fail.
Parameters:
Name
Type
Attributes
Default
Description
function
<optional>
Function that contains the code to teardown your specs.
timeout
Int
<optional>
Custom timeout for an async afterAll.
Since:
·       2.1.0
See:
·       async

afterEach(functionopt, timeoutopt)

Run some shared teardown after each of the specs in the describe in which it is called.
Parameters:
Name
Type
Attributes
Default
Description
function
<optional>
Function that contains the code to teardown your specs.
timeout
Int
<optional>
Custom timeout for an async afterEach.
Since:
·       1.3.0
See:
·       async

beforeAll(functionopt, timeoutopt)

Run some shared setup once before all of the specs in the describe are run.
Note: Be careful, sharing the setup from a beforeAll makes it easy to accidentally leak state between your specs so that they erroneously pass or fail.
Parameters:
Name
Type
Attributes
Default
Description
function
<optional>
Function that contains the code to setup your specs.
timeout
Int
<optional>
Custom timeout for an async beforeAll.
Since:
·       2.1.0
See:
·       async

beforeEach(functionopt, timeoutopt)

Run some shared setup before each of the specs in the describe in which it is called.
Parameters:
Name
Type
Attributes
Default
Description
function
<optional>
Function that contains the code to setup your specs.
timeout
Int
<optional>
Custom timeout for an async beforeEach.
Since:
·       1.3.0
See:
·       async

describe(description, specDefinitions)

Create a group of specs (often called a suite).
Calls to describe can be nested within other calls to compose your suite as a tree.
Parameters:
Name
Type
Description
description
String
Textual description of the group
specDefinitions
function
Function for Jasmine to invoke that will define inner suites and specs
Since:
·       1.3.0

expect(actual)  {matchers}

Create an expectation for a spec.
Parameters:
Name
Type
Description
actual
Object
Actual computed value to test expectations against.
Since:
·       1.3.0
Returns:
Type

expectAsync(actual)  {async-matchers}

Create an asynchronous expectation for a spec. Note that the matchers that are provided by an asynchronous expectation all return promises which must be either returned from the spec or waited for using await in order for Jasmine to associate them with the correct spec.
Parameters:
Name
Type
Description
actual
Object
Actual computed value to test expectations against.
Since:
·       3.3.0
Returns:
Type
Examples
await expectAsync(somePromise).toBeResolved();
return expectAsync(somePromise).toBeResolved();

fail(erroropt)

Explicitly mark a spec as failed.
Parameters:
Name
Type
Attributes
Description
error
String | Error
<optional>
Reason for the failure.
Since:
·       2.1.0

fdescribe(description, specDefinitions)

A focused describe
If suites or specs are focused, only those that are focused will be executed
Parameters:
Name
Type
Description
description
String
Textual description of the group
specDefinitions
function
Function for Jasmine to invoke that will define inner suites and specs
Since:
·       2.1.0
See:
·       fit

fit(description, testFunction, timeoutopt)

A focused it
If suites or specs are focused, only those that are focused will be executed.
Parameters:
Name
Type
Attributes
Default
Description
description
String
Textual description of what this spec is checking.
testFunction
Function that contains the code of your test.
timeout
Int
<optional>
Custom timeout for an async spec.
Since:
·       2.1.0
See:
·       async

it(description, testFunctionopt, timeoutopt)

Define a single spec. A spec should contain one or more expectations that test the state of the code.
A spec whose expectations all succeed will be passing and a spec with any failures will fail. The name it is a pronoun for the test target, not an abbreviation of anything. It makes the spec more readable by connecting the function name it and the argument description as a complete sentence.
Parameters:
Name
Type
Attributes
Default
Description
description
String
Textual description of what this spec is checking
testFunction
<optional>
Function that contains the code of your test. If not provided the test will be pending.
timeout
Int
<optional>
Custom timeout for an async spec.
Since:
·       1.3.0
See:
·       async

pending(messageopt)

Mark a spec as pending, expectation results will be ignored.
Parameters:
Name
Type
Attributes
Description
message
String
<optional>
Reason the spec is pending.
Since:
·       2.0.0

spyOn(obj, methodName)  {Spy}

Install a spy onto an existing object.
Parameters:
Name
Type
Description
obj
Object
The object upon which to install the Spy.
methodName
String
The name of the method to replace with a Spy.
Since:
·       1.3.0
Returns:
Type

spyOnAllFunctions(obj)  {Object}

Installs spies on all writable and configurable properties of an object.
Parameters:
Name
Type
Description
obj
Object
The object upon which to install the Spys
Since:
·       3.2.1
Returns:
the spied object
Type
Object

spyOnProperty(obj, propertyName, accessTypeopt)  {Spy}

Install a spy on a property installed with Object.defineProperty onto an existing object.
Parameters:
Name
Type
Attributes
Default
Description
obj
Object
The object upon which to install the Spy
propertyName
String
The name of the property to replace with a Spy.
accessType
String
<optional>
get
The access type (get|set) of the property to Spy on.
Since:
·       2.6.0
Returns:
Type

xdescribe(description, specDefinitions)

A temporarily disabled describe
Specs within an xdescribe will be marked pending and not executed
Parameters:
Name
Type
Description
description
String
Textual description of the group
specDefinitions
function
Function for Jasmine to invoke that will define inner suites and specs
Since:
·       1.3.0

xit(description, testFunctionopt)

A temporarily disabled it
The spec will report as pending and will not be executed.
Parameters:
Name
Type
Attributes
Description
description
String
Textual description of what this spec is checking.
testFunction
<optional>
Function that contains the code of your test. Will not be executed.
Since:
·       1.3.0

Type Definitions

Expectation

Properties:
Name
Type
Description
matcherName
String
The name of the matcher that was executed for this expectation.
message
String
The failure message for the expectation.
stack
String
The stack trace for the failure if available.
passed
Boolean
Whether the expectation passed or failed.
expected
Object
If the expectation failed, what was the expected value.
actual
Object
If the expectation failed, what actual value was produced.

implementationCallback(doneopt)

Callback passed to parts of the Jasmine base interface.
By default Jasmine assumes this function completes synchronously. If you have code that you need to test asynchronously, you can declare that you receive a done callback, return a Promise, or use the async keyword if it is supported in your environment.
Parameters:
Name
Type
Attributes
Description
done
function
<optional>
Used to specify to Jasmine that this callback is asynchronous and Jasmine should wait until it has been called before moving on.
Returns:
Optionally return a Promise instead of using done to cause Jasmine to wait for completion.

JasmineDoneInfo

Information passed to the Reporter#jasmineDone event.
Properties:
Name
Type
Description
overallStatus
OverallStatus
The overall result of the suite: 'passed', 'failed', or 'incomplete'.
totalTime
Int
The total time (in ms) that it took to execute the suite
incompleteReason
IncompleteReason
Explanation of why the suite was incomplete.
order
Order
Information about the ordering (random or not) of this execution of the suite.
failedExpectations
Array.<Expectation>
List of expectations that failed in an afterAll at the global level.
deprecationWarnings
Array.<Expectation>
List of deprecation warnings that occurred at the global level.

JasmineStartedInfo

Information passed to the Reporter#jasmineStarted event.
Properties:
Name
Type
Description
totalSpecsDefined
Int
The total number of specs defined in this suite.
order
Order
Information about the ordering (random or not) of this execution of the suite.

SpecResult

Properties:
Name
Type
Description
id
Int
The unique id of this spec.
description
String
The description passed to the it that created this spec.
fullName
String
The full description including all ancestors of this spec.
failedExpectations
Array.<Expectation>
The list of expectations that failed during execution of this spec.
passedExpectations
Array.<Expectation>
The list of expectations that passed during execution of this spec.
deprecationWarnings
Array.<Expectation>
The list of deprecation warnings that occurred during execution this spec.
pendingReason
String
If the spec is pending, this will be the reason.
status
String
Once the spec has completed, this string represents the pass/fail status of this spec.
duration
number
The time in ms used by the spec execution, including any before/afterEach.

SuiteResult

Properties:
Name
Type
Description
id
Int
The unique id of this suite.
description
String
The description text passed to the describe that made this suite.
fullName
String
The full description including all ancestors of this suite.
failedExpectations
Array.<Expectation>
The list of expectations that failed in an afterAll for this suite.
deprecationWarnings
Array.<Expectation>
The list of deprecation warnings that occurred on this suite.
status
String
Once the suite has completed, this string represents the pass/fail status of this suite.
duration
number
The time in ms for Suite execution, including any before/afterAll, before/afterEach.



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.