Introduction to Jasmine
Goals
- Understand why we should test our code with specs
- Describe what assertions and expectations are
- Implement Jasmine with a Node application
- Create a suite of tests and individual tests inside of a suite
Getting Started with Jasmine
- Create a new project folder.
- Change directory to that folder.
npm init
a new application. Enter, enter, enter...
- Let's take a look at our folder structure:
tree
. Now, add an app
and a spec
folder. The app folder will contain your application and your spec folder will contain your tests.
- We need to install Jasmine. To do so,
npm install jasmine-node --save
- Now, we need to find where our executable for tests lives...
node_modules/.bin/jasmine-node spec
- Let's run that!
node_modules/.bin/jasmine-node spec
- In your package.json, under the
"scripts"
for test, replace the value with node_modules/.bin/jasmine-node spec
- Run
npm test
- Verify your tests run.
- Wait. what tests?! Let's add some! We'll add them to the spec folder.
- Inside of the spec folder, create a new Javascript file. Such as
testSpec.js
or somethingSpec.js
. For tests to successfully be ran, you must end the file with Spec.
- Now, run
npm test
!
Describe Suites of Tests
- We need to describe a suite, or set, of tests
- A suite includes multiple things to test
- Suites can include other suites as children
- Inside of each suite of tests, we include individual tests
describe('a set of problems to solve', function() {
});
Describe a Test
- Each test is described
- it has a variety of expectations
describe('a set of problems to solve', function() {
describe('a failing test', function() {
it('fails', function() {
});
});
describe('an individual problem to solve', function() {
it('a basic test', function() {
var expected = 'solution';
var assert = expected;
expect(assert).toBe(expected);
});
});
});
Jasmine Matchers
expect(fn).toThrow(e);
expect(instance).toBe(instance);
expect(mixed).toBeDefined();
expect(mixed).toBeFalsy();
expect(number).toBeGreaterThan(number);
expect(number).toBeLessThan(number);
expect(mixed).toBeNull();
expect(mixed).toBeTruthy();
expect(mixed).toBeUndefined();
expect(array).toContain(member);
expect(string).toContain(substring);
expect(mixed).toEqual(mixed);
expect(mixed).toMatch(pattern);