/
jezebel_spec.js
141 lines (119 loc) · 4.5 KB
/
jezebel_spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
describe('jezebel', function() {
var childProcess = require('child_process'),
sys = require('util');
var watcher, jezebel, repl, session, settings;
beforeEach(function() {
watcher = require('jezebel/watcher');
jezebel = require('jezebel');
session = {context: {}};
spyOn(jezebel.repl, 'start').andReturn(session);
spyOn(watcher, 'watchFiles');
spyOn(childProcess, 'spawn');
spyOn(process.stdin, 'emit');
spyOn(process, 'nextTick');
spyOn(require('path'), 'exists').andCallFake(function(file, callback) {
if(file == process.cwd() + '/.jezebel')
callback(true);
else
callback(false);
});
require(process.cwd() + '/.jezebel').settings = settings = {};
});
function rootDir() {
var path = require('path')
var fs = require('fs');
return fs.realpathSync(path.dirname(fs.realpathSync(__filename)) + '/..');
}
function expectReplEval(statement) {
expect(process.stdin.emit).toHaveBeenCalled();
expect(process.stdin.emit.argsForCall[0][0]).toEqual('data');
expect(process.stdin.emit.argsForCall[0][1].toString()).toEqual(statement);
}
describe('run', function() {
beforeEach(function() {
childProcess.spawn.andReturn(process);
});
it('watches for all the files in the specified directory', function() {
jezebel.run([], {});
expect(watcher.watchFiles).toHaveBeenCalledWith(process.cwd(), jezebel.fileChanged);
});
it('stars the repl', function() {
jezebel.run([], {});
expect(jezebel.repl.start).toHaveBeenCalledWith('> ');
});
it('calls the onStart hook', function() {
settings.onStart = jasmine.createSpy('onStart');
jezebel.run([], {});
expect(settings.onStart).toHaveBeenCalled();
});
});
describe('fileChanged', function() {
beforeEach(function() {
spyOn(console, 'log');
childProcess.spawn.andReturn(process);
});
it('runs tests if the file has changed', function() {
jezebel.fileChanged(__filename, {mtime: new Date(100)}, {mtime: new Date(0)});
expect(childProcess.spawn).toHaveBeenCalled();
expect(console.log).toHaveBeenCalledWith('Running examples in spec/jezebel_spec.js');
});
it('does not run the tests if the file has not actually changed', function() {
jezebel.fileChanged("", {mtime: new Date(0)}, {mtime: new Date(0)});
expect(process.stdin.emit).not.toHaveBeenCalled();
});
it('invokes the onChange hook to determine the tests to run', function() {
jezebel.settings.onChange = function() {
return ['myspecs'];
};
jezebel.fileChanged(__filename, {mtime: new Date(100)}, {mtime: new Date(0)});
expect(childProcess.spawn.argsForCall[0][1]).toEqual(['myspecs']);
});
});
describe('runTests', function() {
var child;
beforeEach(function() {
childProcess.spawn.andReturn(child = {
stdout: jasmine.createSpyObj('stdout', ['addListener']),
stderr: jasmine.createSpyObj('stderr', ['addListener']),
on: jasmine.createSpy('on')
});
});
it('runs all tests in the spec directory by default', function() {
jezebel.runTests();
expect(childProcess.spawn).toHaveBeenCalledWith(rootDir() + '/node_modules/jessie/bin/jessie', ['spec']);
});
it('runs the specified tests', function() {
jezebel.runTests(['spec']);
expect(childProcess.spawn).toHaveBeenCalledWith(rootDir() + '/node_modules/jessie/bin/jessie', ['spec']);
});
it('writes stdout to sys.print', function() {
spyOn(sys, 'print');
jezebel.runTests(['spec']);
child.stdout.addListener.argsForCall[0][1]('hello');
expect(sys.print).toHaveBeenCalledWith('hello');
});
it('writes stderr to sys.debug', function() {
spyOn(sys, 'debug');
jezebel.runTests(['spec']);
child.stderr.addListener.argsForCall[0][1]('goodbye');
expect(sys.debug).toHaveBeenCalledWith('goodbye');
});
it('adds a line feed after running to re-prompt the repl', function() {
jezebel.runTests(['spec']);
child.on.argsForCall[0][1]();
expect(process.nextTick).toHaveBeenCalled();
});
});
describe('returnInRepl', function() {
it('sets the argument as the return value', function() {
returnInRepl('foo');
pending();
expect(session.context._).toEqual('foo');
});
it('evaluates the return value on the next tick', function() {
returnInRepl('foo');
process.nextTick.argsForCall[0][0]();
expectReplEval("_\n");
});
});
});