32
loading...
This website collects cookies to deliver better user experience
npm init --y
, this will generate a package json file that will track our dependencies. npm install jasmine
. After that you run jasmine init
this will create a spec
folder that will contain our tests and a support
folder that holds a jasmine.json
which is a configuration file for jasmine. **.spec.js
. Test files should sit in the spec
folder.// We will import a user class we will create later
import User from '../models/user'
// describe function defines a test block,
describe('just testing the user', () => {
// actual test are written in it functions
it('a new user should be offline', () => {
const sam = new User()
// expect something to happen
expect(sam.onlineStatus).toBe(false)
})
})
it
functions are the actual specs, they are also global jasmine functions they are quite similar to describe function in that, they accepts a string and a function as arguments. An it
function will contain a one or more expectations that test the state or behavior of our code, we can expect multiple things in a single it
function or use multiple it
functions, it is down to you to decide. expect
method with one of jasmine's built in matchers
. The value passed to the expect
function is called the actual
matchers are used to make a comparison between the actual
and the expected, which is passed to the matcher
. Above we expected Sam's online status to be false using the toBe()
matcher. This is one of jasmine's built in matchers and there are loads of matchers for almost all scenarios. If still don't find any matcher for your own test case, you can build one your self. npx jasmine
it will fail because; we have not created the file for the user class, which we will proceed to create in the appropriate directory. On the root level of the project create a folder models/user.ts
next to the spec folder. You will have observed that i'm using typescript here but you can easily compile down to javascript.// User
export default class User {
constructor(){},
onlineStatus = false
}
// We will import a user class we will create later
import User from '../models/user'
describe('just testing the user', () => {
let sam;
// beforeEach, is used to set a config before
// each of the spec runs
beforeEach(()=> {
sam = new User();
})
it('a new user should be offline', () => {
expect(sam.onlineStatus).toBe(false)
})
it('sam should login and have a profile', ()=> {
sam.login('[email protected]', 'password')
expect(sam.onlineStatus).toBe(true)
expect(sam.profile.email).toBe('[email protected]')
})
})
npx jasmine
we will see our second spec fail, this spec is called a failing spec. A spec with one or more expectations that are not truthy fall in this category, while the first test where all expectations are met is called a passing spec.describe
block, beforeEach
this function is one of four setup and tear down functions in jasmine that we can use to configure or clean up after each spec, the name of each function says it all. The others are beforeEach, afterAll and beforeAll
. Let's modify the user class to implement these specifications we just defined above.// User
export default class User {
constructor(){},
public profile
onlineStatus = false
login(email: string, password: string){
this.profile = {
email: email
}
return this.profile
}
}