Global beforeEach
Answer to SO question
HTML
<script src="http://jasmine.github.io/2.0/lib/jasmine.js"></script>
<script src="http://jasmine.github.io/2.0/lib/jasmine-html.js"></script>
<script src="http://jasmine.github.io/2.0/lib/boot.js"></script>
<link rel="stylesheet" href="http://jasmine.github.io/2.0/lib/jasmine.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular-mocks.js"></script>
JavaScript
// --- SPECS -------------------------
var myGlobal;
beforeEach(function() {
// This will run before any it function.
// Resetting a global state so the change in this function is testable
myGlobal = 10
process.stdout.write("hello: ");
});
describe('first suite', function(){
it('is a test', function(){
expect(myGlobal).toBe(10);
// Set the value to show that beforeEach is executed for each it function
myGlobal = 20;
expect(myGlobal).toBe(20);
});
it('is another test', function(){
expect(myGlobal).toBe(10);
myGlobal = 30;
expect(myGlobal).toBe(30);
});
});
describe('second suite', function(){
it('is a test', function(){
expect(myGlobal).toBe(10);
});
});