- Download the modules from http://bit.ly/2uNvBOY if you don't already have them
- Unzip the file. Unzip Module 1.
- Create a new project and move all the resources there
- download and copy resources into project from Gist
- Copy
User.js
into your project
- Copy
- create a
user.test.js
file and import theUser.js
module - create a new test:
should return full name
- Create a new user and pass in an object with a
firstName
and alastName
property. - Expect that the user’s
name
property is equal to thefirstName
andlasName
, with a space between.
- Create a new user and pass in an object with a
const user = new User({ firstName: 'John', lastName: 'Rambo'})
user.name === 'John Rambo'
- Create a new test:
should return initials
- Create a new user and pass in an object with a
firstName
and alastName
property - Expect that the user’s
initials
property is equal to the user’s uppercased initials (first character in each name)
- Create a new user and pass in an object with a
const user = new User({ firstName: 'John', lastName: 'Rambo'})
user.initials === 'JR'
In these exercises, we’re going to use common matches to test that our rocket factory works as expected.
started
booleanfuel
NumberstartEngine()
stopEngine()
takeOff()
addFuel(50)
hasSufficientFuel()
Feel free to inspect it further during the exercises.
-
Download
rocket.js
from this Gist and move it to your project directory -
Create a
rocket.test.js
and importcreateRocket
fromrocket.js
import { createRocket } from './rocket'
- create test:
can add fuel
- create a new rocket and use
addFuel()
to add100
tons of rocket fuel - expect that
fuel
is equal to100
- create a new rocket and use
- create test:
must have 40 tonnes of rocket fuel to have sufficient
- Create a new rocket
- Expect
hasSufficientFuel()
to befalse
with 39 tonnes of fuel - Expect
hasSufficientFuel()
to betrue
with 40 tonnes of fuel
- create test:
cannot start engine without fuel
- create a new rocket
- expect
startEngine
to throw error (since no fuel is added)
Tip: When testing exceptions, we must (!) wrap the expected code in a function
- create test:
can start engine with sufficient fuel
- create a new rocket with 200 tonnes of rocket fuel
- expect
startEngine()
to not throw an exception
In the next exercises, we’ll use Jest hooks to DRY up our code, we’ll also create and explore snapshot testing.
- DRY up your code by creating a new rocket in the
beforeEach
hook
- Create test:
matches snapshot
- expect that the rocket matces the snapshot
- Run the test and inspect the snapshot that was created in
__snapshots__
directory
- Introduce a change in
rocket.js
(set started: true) - re-run tests
- update snapshot