Skip to content

Commit d1b5d51

Browse files
author
Gregg Van Hove
committed
Merge branch 'm7r_helper' of https://github.com/m7r/jasmine-npm into m7r-m7r_helper
2 parents 43a219f + eed0eed commit d1b5d51

File tree

4 files changed

+80
-24
lines changed

4 files changed

+80
-24
lines changed

lib/command.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function isFileArg(arg) {
5757

5858
function parseOptions(argv) {
5959
var files = [],
60+
helpers = [],
6061
color = process.stdout.isTTY || false,
6162
filter,
6263
stopOnFailure,
@@ -68,6 +69,8 @@ function parseOptions(argv) {
6869
color = false;
6970
} else if (arg.match("^--filter=")) {
7071
filter = arg.match("^--filter=(.*)")[1];
72+
} else if (arg.match("^--helper=")) {
73+
helpers.push(arg.match("^--helper=(.*)")[1]);
7174
} else if (arg.match("^--stop-on-failure=")) {
7275
stopOnFailure = arg.match("^--stop-on-failure=(.*)")[1] === 'true';
7376
} else if (arg.match("^--random=")) {
@@ -82,6 +85,7 @@ function parseOptions(argv) {
8285
color: color,
8386
filter: filter,
8487
stopOnFailure: stopOnFailure,
88+
helpers: helpers,
8589
files: files,
8690
random: random,
8791
seed: seed
@@ -99,6 +103,9 @@ function runJasmine(jasmine, env) {
99103
if (env.random !== undefined) {
100104
jasmine.randomizeTests(env.random);
101105
}
106+
if (env.helpers !== undefined && env.helpers.length) {
107+
jasmine.addHelperFiles(env.helpers);
108+
}
102109
jasmine.showColors(env.color);
103110
jasmine.execute(env.files, env.filter);
104111
}
@@ -164,6 +171,7 @@ function help(options) {
164171
print('Options:');
165172
print('%s\tturn off color in spec output', lPad('--no-color', 18));
166173
print('%s\tfilter specs to run only those that match the given string', lPad('--filter=', 18));
174+
print('%s\tload helper files that match the given string', lPad('--helper=', 18));
167175
print('%s\t[true|false] stop spec execution on expectation failure', lPad('--stop-on-failure=', 18));
168176
print('');
169177
print('The given arguments take precedence over options in your jasmine.json');

lib/jasmine.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function Jasmine(options) {
1515
this.jasmine = jasmineCore.boot(jasmineCore);
1616
this.projectBaseDir = options.projectBaseDir || path.resolve();
1717
this.printDeprecation = options.printDeprecation || require('./printDeprecation');
18+
this.specDir = '';
1819
this.specFiles = [];
1920
this.helperFiles = [];
2021
this.env = this.jasmine.getEnv();
@@ -102,40 +103,40 @@ Jasmine.prototype.loadConfigFile = function(configFilePath) {
102103
};
103104

104105
Jasmine.prototype.loadConfig = function(config) {
105-
var jasmineRunner = this;
106-
jasmineRunner.specDir = config.spec_dir || '';
106+
this.specDir = config.spec_dir || this.specDir;
107+
this.env.throwOnExpectationFailure(config.stopSpecOnExpectationFailure);
108+
this.env.randomizeTests(config.random);
107109

108110
if(config.helpers) {
109-
config.helpers.forEach(function(helperFile) {
110-
var filePaths = glob.sync(path.join(jasmineRunner.projectBaseDir, jasmineRunner.specDir, helperFile));
111-
filePaths.forEach(function(filePath) {
112-
if(jasmineRunner.helperFiles.indexOf(filePath) === -1) {
113-
jasmineRunner.helperFiles.push(filePath);
114-
}
115-
});
116-
});
111+
this.addHelperFiles(config.helpers);
117112
}
118113

119-
this.env.throwOnExpectationFailure(config.stopSpecOnExpectationFailure);
120-
this.env.randomizeTests(config.random);
121-
122114
if(config.spec_files) {
123-
jasmineRunner.addSpecFiles(config.spec_files);
115+
this.addSpecFiles(config.spec_files);
124116
}
125117
};
126118

127-
Jasmine.prototype.addSpecFiles = function(files) {
128-
var jasmineRunner = this;
119+
Jasmine.prototype.addHelperFiles = addFiles('helperFiles');
120+
Jasmine.prototype.addSpecFiles = addFiles('specFiles');
121+
122+
function addFiles(kind) {
123+
return function (files) {
124+
var jasmineRunner = this;
125+
var fileArr = this[kind];
129126

130-
files.forEach(function(specFile) {
131-
var filePaths = glob.sync(path.join(jasmineRunner.projectBaseDir, jasmineRunner.specDir, specFile));
132-
filePaths.forEach(function(filePath) {
133-
if(jasmineRunner.specFiles.indexOf(filePath) === -1) {
134-
jasmineRunner.specFiles.push(filePath);
127+
files.forEach(function(file) {
128+
if(!(path.isAbsolute && path.isAbsolute(file))) {
129+
file = path.join(jasmineRunner.projectBaseDir, jasmineRunner.specDir, file);
135130
}
131+
var filePaths = glob.sync(file);
132+
filePaths.forEach(function(filePath) {
133+
if(fileArr.indexOf(filePath) === -1) {
134+
fileArr.push(filePath);
135+
}
136+
});
136137
});
137-
});
138-
};
138+
};
139+
}
139140

140141
Jasmine.prototype.onComplete = function(onCompleteCallback) {
141142
this.completionReporter.onComplete(onCompleteCallback);

spec/command_spec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('command', function() {
4242

4343
this.command = new Command(projectBaseDir, examplesDir, this.out.print);
4444

45-
this.fakeJasmine = jasmine.createSpyObj('jasmine', ['loadConfigFile', 'showColors', 'execute', 'stopSpecOnExpectationFailure', 'randomizeTests', 'seed', 'coreVersion']);
45+
this.fakeJasmine = jasmine.createSpyObj('jasmine', ['loadConfigFile', 'addHelperFiles', 'showColors', 'execute', 'stopSpecOnExpectationFailure', 'randomizeTests', 'seed', 'coreVersion']);
4646
});
4747

4848
afterEach(function() {
@@ -197,6 +197,21 @@ describe('command', function() {
197197
expect(this.fakeJasmine.execute).toHaveBeenCalledWith(jasmine.any(Array), 'interesting spec');
198198
});
199199

200+
it('should be able to add one helper pattern', function() {
201+
this.command.run(this.fakeJasmine, ['--helper=helpers/**/*.js']);
202+
expect(this.fakeJasmine.addHelperFiles).toHaveBeenCalledWith(['helpers/**/*.js']);
203+
});
204+
205+
it('should be able to add many helper patterns', function() {
206+
this.command.run(this.fakeJasmine, ['--helper=helpers/**/*.js', '--helper=other.js']);
207+
expect(this.fakeJasmine.addHelperFiles).toHaveBeenCalledWith(['helpers/**/*.js', 'other.js']);
208+
});
209+
210+
it('should not modify helper patterns if no argument given', function() {
211+
this.command.run(this.fakeJasmine, []);
212+
expect(this.fakeJasmine.addHelperFiles).not.toHaveBeenCalled();
213+
});
214+
200215
it('should not configure stopping spec on expectation failure by default', function() {
201216
this.command.run(this.fakeJasmine, []);
202217
expect(this.fakeJasmine.stopSpecOnExpectationFailure).not.toHaveBeenCalled();

spec/jasmine_spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,38 @@ describe('Jasmine', function() {
4040
expect(this.testJasmine.specFiles).toEqual(['some/file/path.js']);
4141
});
4242

43+
describe('file handler', function() {
44+
function basename(name) { return path.basename(name); }
45+
46+
it('add spec files with absolute glob pattern', function() {
47+
if (!path.isAbsolute) { return; }
48+
var aFile = path.join(this.testJasmine.projectBaseDir, this.testJasmine.specDir, 'spec/command_spec.js');
49+
expect(this.testJasmine.specFiles).toEqual([]);
50+
this.testJasmine.addSpecFiles([aFile]);
51+
expect(this.testJasmine.specFiles).toEqual([aFile]);
52+
});
53+
54+
it('add spec files with glob pattern', function() {
55+
expect(this.testJasmine.specFiles).toEqual([]);
56+
this.testJasmine.addSpecFiles(['spec/*.js']);
57+
expect(this.testJasmine.specFiles.map(basename)).toEqual(['command_spec.js', 'exit_spec.js', 'jasmine_spec.js']);
58+
});
59+
60+
it('add spec files with glob pattern to existings files', function() {
61+
var aFile = path.join(this.testJasmine.projectBaseDir, this.testJasmine.specDir, 'spec/command_spec.js');
62+
this.testJasmine.specFiles = [aFile, 'b'];
63+
this.testJasmine.addSpecFiles(['spec/*.js']);
64+
expect(this.testJasmine.specFiles.map(basename)).toEqual(['command_spec.js', 'b', 'exit_spec.js', 'jasmine_spec.js']);
65+
});
66+
67+
it('add helper files with glob pattern to existings files', function() {
68+
var aFile = path.join(this.testJasmine.projectBaseDir, this.testJasmine.specDir, 'spec/command_spec.js');
69+
this.testJasmine.helperFiles = [aFile, 'b'];
70+
this.testJasmine.addHelperFiles(['spec/*.js']);
71+
expect(this.testJasmine.helperFiles.map(basename)).toEqual(['command_spec.js', 'b', 'exit_spec.js', 'jasmine_spec.js']);
72+
});
73+
});
74+
4375
it('delegates #coreVersion to jasmine-core', function() {
4476
this.fakeJasmineCore.version = jasmine.createSpy('coreVersion').and.returnValue('a version');
4577
expect(this.testJasmine.coreVersion()).toEqual('a version');

0 commit comments

Comments
 (0)