Skip to content

Commit

Permalink
Add option "require(s)" to command line and config file
Browse files Browse the repository at this point in the history
- Merges #136 from @liuxh0
- Fixes #135
  • Loading branch information
liuxh0 authored and Gregg Van Hove committed Jul 24, 2018
1 parent 1f60cbc commit 869ad0c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function isFileArg(arg) {
function parseOptions(argv) {
var files = [],
helpers = [],
requires = [],
unknownOptions = [],
color = process.stdout.isTTY || false,
reporter,
Expand All @@ -84,6 +85,8 @@ function parseOptions(argv) {
filter = arg.match("^--filter=(.*)")[1];
} else if (arg.match("^--helper=")) {
helpers.push(arg.match("^--helper=(.*)")[1]);
} else if (arg.match("^--require=")) {
requires.push(arg.match("^--require=(.*)")[1]);
} else if (arg.match("^--stop-on-failure=")) {
stopOnFailure = arg.match("^--stop-on-failure=(.*)")[1] === 'true';
} else if (arg.match("^--fail-fast=")) {
Expand All @@ -109,6 +112,7 @@ function parseOptions(argv) {
stopOnFailure: stopOnFailure,
failFast: failFast,
helpers: helpers,
requires: requires,
reporter: reporter,
files: files,
random: random,
Expand All @@ -134,6 +138,9 @@ function runJasmine(jasmine, env, print) {
if (env.helpers !== undefined && env.helpers.length) {
jasmine.addHelperFiles(env.helpers);
}
if (env.requires !== undefined && env.requires.length) {
jasmine.addRequires(env.requires);
}
if (env.reporter !== undefined) {
try {
var Report = require(env.reporter);
Expand Down Expand Up @@ -213,6 +220,7 @@ function help(options) {
print('%s\tforce turn on color in spec output', lPad('--color', 18));
print('%s\tfilter specs to run only those that match the given string', lPad('--filter=', 18));
print('%s\tload helper files that match the given string', lPad('--helper=', 18));
print('%s\tload module that match the given string', lPad('--require=', 18));
print('%s\t[true|false] stop spec execution on expectation failure', lPad('--stop-on-failure=', 18));
print('%s\t[true|false] stop Jasmine execution on spec failure', lPad('--fail-fast=', 18));
print('%s\tpath to your optional jasmine.json', lPad('--config=', 18));
Expand Down
19 changes: 19 additions & 0 deletions lib/jasmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function Jasmine(options) {
this.specDir = '';
this.specFiles = [];
this.helperFiles = [];
this.requires = [];
this.env = this.jasmine.getEnv({suppressLoadErrors: true});
this.reportersCount = 0;
this.completionReporter = new CompletionReporter();
Expand Down Expand Up @@ -95,6 +96,12 @@ Jasmine.prototype.loadHelpers = function() {
});
};

Jasmine.prototype.loadRequires = function() {
this.requires.forEach(function(r) {
require(r);
});
};

Jasmine.prototype.loadConfigFile = function(configFilePath) {
try {
var absoluteConfigFilePath = path.resolve(this.projectBaseDir, configFilePath || 'spec/support/jasmine.json');
Expand Down Expand Up @@ -124,6 +131,10 @@ Jasmine.prototype.loadConfig = function(config) {
this.addHelperFiles(config.helpers);
}

if(config.requires) {
this.addRequires(config.requires);
}

if(config.spec_files) {
this.addSpecFiles(config.spec_files);
}
Expand All @@ -132,6 +143,13 @@ Jasmine.prototype.loadConfig = function(config) {
Jasmine.prototype.addHelperFiles = addFiles('helperFiles');
Jasmine.prototype.addSpecFiles = addFiles('specFiles');

Jasmine.prototype.addRequires = function(requires) {
var jasmineRunner = this;
requires.forEach(function(r) {
jasmineRunner.requires.push(r);
});
};

function addFiles(kind) {
return function (files) {
var jasmineRunner = this;
Expand Down Expand Up @@ -183,6 +201,7 @@ var checkExit = function(jasmineRunner) {
Jasmine.prototype.execute = function(files, filterString) {
process.on('exit', this.checkExit);

this.loadRequires();
this.loadHelpers();
if (!this.defaultReporterConfigured) {
this.configureDefaultReporter({ showColors: this.showingColors });
Expand Down
12 changes: 11 additions & 1 deletion spec/command_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('command', function() {

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

this.fakeJasmine = jasmine.createSpyObj('jasmine', ['loadConfigFile', 'addHelperFiles', 'showColors', 'execute', 'stopSpecOnExpectationFailure',
this.fakeJasmine = jasmine.createSpyObj('jasmine', ['loadConfigFile', 'addHelperFiles', 'addRequires', 'showColors', 'execute', 'stopSpecOnExpectationFailure',
'stopOnSpecFailure', 'randomizeTests', 'seed', 'coreVersion', 'clearReporters', 'addReporter']);
});

Expand Down Expand Up @@ -233,6 +233,16 @@ describe('command', function() {
expect(this.fakeJasmine.addHelperFiles).not.toHaveBeenCalled();
});

it('should be able to add one require', function() {
this.command.run(this.fakeJasmine, ['--require=ts-node/require']);
expect(this.fakeJasmine.addRequires).toHaveBeenCalledWith(['ts-node/require']);
});

it('should be able to add multiple requires', function() {
this.command.run(this.fakeJasmine, ['--require=ts-node/require', '--require=@babel/register']);
expect(this.fakeJasmine.addRequires).toHaveBeenCalledWith(['ts-node/require', '@babel/register']);
});

it('can specify a reporter', function() {
var reporterPath = path.resolve(path.join(__dirname, 'fixtures', 'customReporter.js'));
var Reporter = require(reporterPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
],
"helpers": [
"helper.js"
],
"requires": [
"ts-node/register"
]
}
6 changes: 6 additions & 0 deletions spec/jasmine_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,17 @@ describe('Jasmine', function() {
],
helpers: [
"helper.js"
],
requires: [
"ts-node/register"
]
};
});

it('adds unique specs to the jasmine runner', function() {
this.fixtureJasmine.loadConfig(this.configObject);
expect(this.fixtureJasmine.helperFiles).toEqual(['spec/fixtures/sample_project/spec/helper.js']);
expect(this.fixtureJasmine.requires).toEqual(['ts-node/register']);
expect(this.fixtureJasmine.specFiles).toEqual([
'spec/fixtures/sample_project/spec/fixture_spec.js',
'spec/fixtures/sample_project/spec/other_fixture_spec.js'
Expand Down Expand Up @@ -241,6 +245,7 @@ describe('Jasmine', function() {
it('adds unique specs to the jasmine runner', function() {
this.fixtureJasmine.loadConfigFile('spec/support/jasmine_alternate.json');
expect(this.fixtureJasmine.helperFiles).toEqual(['spec/fixtures/sample_project/spec/helper.js']);
expect(this.fixtureJasmine.requires).toEqual(['ts-node/register']);
expect(this.fixtureJasmine.specFiles).toEqual([
'spec/fixtures/sample_project/spec/fixture_spec.js',
'spec/fixtures/sample_project/spec/other_fixture_spec.js'
Expand All @@ -251,6 +256,7 @@ describe('Jasmine', function() {
var absoluteConfigPath = path.join(__dirname, 'fixtures/sample_project/spec/support/jasmine_alternate.json');
this.fixtureJasmine.loadConfigFile(absoluteConfigPath);
expect(this.fixtureJasmine.helperFiles).toEqual(['spec/fixtures/sample_project/spec/helper.js']);
expect(this.fixtureJasmine.requires).toEqual(['ts-node/register']);
expect(this.fixtureJasmine.specFiles).toEqual([
'spec/fixtures/sample_project/spec/fixture_spec.js',
'spec/fixtures/sample_project/spec/other_fixture_spec.js'
Expand Down

0 comments on commit 869ad0c

Please sign in to comment.