Skip to content

Commit 634ed91

Browse files
committed
Add example project for jasmine
1 parent a631814 commit 634ed91

9 files changed

Lines changed: 214 additions & 0 deletions

File tree

jasmine/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
.launchable
3+
.python-version
4+
*.json
5+
*.txt

jasmine/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Jasmine Example Project
2+
=====================
3+
4+
#### Project Structure
5+
6+
```
7+
jasmine
8+
└── spec
9+
├── helpers
10+
│ └── jasmine-json-test-reporter.js
11+
12+
└── jasmine_examples
13+
├── PlayerSpec.js
14+
└── SongSpec.js
15+
```
16+
17+
NOTE: The tests in SongSpec.js are set to fail intentionally.
18+
19+
#### To Build the Project
20+
21+
```
22+
npm install
23+
```
24+
25+
#### To Record Tests
26+
27+
Create test session:
28+
29+
```
30+
BUILD_NAME=jasmine_build
31+
launchable record build --name ${BUILD_NAME}
32+
launchable record session --build ${BUILD_NAME} > session.txt
33+
```
34+
35+
Find and write all tests to a file:
36+
37+
```
38+
find spec/jasmine_examples -type f > test_list.txt
39+
```
40+
41+
To run all tests:
42+
43+
```
44+
npx jasmine $(cat test_list.txt)
45+
```
46+
47+
This creates `jasmine-report.json` in the project root.
48+
49+
Then to record the tests:
50+
51+
```
52+
launchable record tests --base $(pwd) jasmine jasmine-report.json
53+
```
54+
55+
To request subset:
56+
57+
```
58+
cat test_list.txt | launchable subset --target 25% jasmine > subset.txt
59+
```
60+
61+
To run subset of tests:
62+
63+
```
64+
npx jasmine $(cat subset.txt)
65+
```
66+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Player {
2+
play(song) {
3+
this.currentlyPlayingSong = song;
4+
this.isPlaying = true;
5+
}
6+
7+
pause() {
8+
this.isPlaying = false;
9+
}
10+
11+
resume() {
12+
if (this.isPlaying) {
13+
throw new Error('song is already playing');
14+
}
15+
16+
this.isPlaying = true;
17+
}
18+
19+
makeFavorite() {
20+
this.currentlyPlayingSong.persistFavoriteStatus(true);
21+
}
22+
}
23+
24+
module.exports = Player;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Song {
2+
persistFavoriteStatus(value) {
3+
// something complicated
4+
throw new Error('not yet implemented');
5+
}
6+
}
7+
8+
module.exports = Song;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var JSONReporter = require('jasmine-json-test-reporter');
2+
jasmine.getEnv().addReporter(new JSONReporter({
3+
file: 'jasmine-report.json'
4+
}));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
beforeEach(function () {
2+
jasmine.addMatchers({
3+
toBePlaying: function () {
4+
return {
5+
compare: function (actual, expected) {
6+
const player = actual;
7+
8+
return {
9+
pass: player.currentlyPlayingSong === expected && player.isPlaying
10+
};
11+
}
12+
};
13+
}
14+
});
15+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const Player = require('../../lib/jasmine_examples/Player');
2+
const Song = require('../../lib/jasmine_examples/Song');
3+
4+
describe('Player', function () {
5+
let player;
6+
let song;
7+
8+
beforeEach(function () {
9+
player = new Player();
10+
song = new Song();
11+
});
12+
13+
it('should be able to play a Song', function () {
14+
player.play(song);
15+
expect(player.currentlyPlayingSong).toEqual(song);
16+
17+
// demonstrates use of custom matcher
18+
expect(player).toBePlaying(song);
19+
});
20+
21+
describe('when song has been paused', function () {
22+
beforeEach(function () {
23+
player.play(song);
24+
player.pause();
25+
});
26+
27+
it('should indicate that the song is currently paused', function () {
28+
expect(player.isPlaying).toBeFalsy();
29+
30+
// demonstrates use of 'not' with a custom matcher
31+
expect(player).not.toBePlaying(song);
32+
});
33+
34+
it('should be possible to resume', function () {
35+
player.resume();
36+
expect(player.isPlaying).toBeTruthy();
37+
expect(player.currentlyPlayingSong).toEqual(song);
38+
});
39+
});
40+
41+
// demonstrates use of spies to intercept and test method calls
42+
it('tells the current song if the user has made it a favorite', function () {
43+
spyOn(song, 'persistFavoriteStatus');
44+
45+
player.play(song);
46+
player.makeFavorite();
47+
48+
expect(song.persistFavoriteStatus).toHaveBeenCalledWith(true);
49+
});
50+
51+
//demonstrates use of expected exceptions
52+
describe('#resume', function () {
53+
it('should throw an exception if song is already playing', function () {
54+
player.play(song);
55+
56+
expect(function () {
57+
player.resume();
58+
}).toThrowError('song is already playing');
59+
});
60+
});
61+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const Player = require('../../lib/jasmine_examples/Player');
2+
const Song = require('../../lib/jasmine_examples/Song');
3+
4+
describe('Song', function () {
5+
let player;
6+
let song;
7+
8+
beforeEach(function () {
9+
player = new Player();
10+
song = new Song();
11+
});
12+
13+
it('should be able to play by player (intentionally set to fail)', function () {
14+
player.play(song);
15+
expect(player.isPlaying).toBeFalsy();
16+
});
17+
});

jasmine/spec/support/jasmine.mjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default {
2+
spec_dir: "spec",
3+
spec_files: [
4+
"**/*[sS]pec.?(m)js"
5+
],
6+
helpers: [
7+
"helpers/**/*.?(m)js"
8+
],
9+
env: {
10+
stopSpecOnExpectationFailure: false,
11+
random: true,
12+
forbidDuplicateNames: true
13+
}
14+
}

0 commit comments

Comments
 (0)