Skip to content

Commit

Permalink
Exit nonzero when a spec fails to load
Browse files Browse the repository at this point in the history
* Fixes #167
  • Loading branch information
sgravrock committed Oct 24, 2020
1 parent e5172d2 commit 555e69a
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 42 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spec/fixtures/cjs-syntax-error/syntax_error.js
6 changes: 5 additions & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ function parseOptions(argv) {
function runJasmine(jasmine, env, print) {
var loadConfig = require('./loadConfig');
loadConfig(jasmine, env, print);
jasmine.execute(env.files, env.filter);
jasmine.execute(env.files, env.filter)
.catch(function(error) {
console.error(error);
process.exit(1);
});
}

function initJasmine(options) {
Expand Down
1 change: 1 addition & 0 deletions spec/command_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('command', function() {

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

afterEach(function() {
Expand Down
40 changes: 0 additions & 40 deletions spec/esm_integration_spec.js

This file was deleted.

7 changes: 7 additions & 0 deletions spec/fixtures/cjs-load-exception/jasmine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"spec_dir": ".",
"spec_files": [
"throws_on_load.js"
],
"helpers": []
}
1 change: 1 addition & 0 deletions spec/fixtures/cjs-load-exception/throws_on_load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error("nope");
7 changes: 7 additions & 0 deletions spec/fixtures/cjs-syntax-error/jasmine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"spec_dir": ".",
"spec_files": [
"syntax_error.js"
],
"helpers": []
}
1 change: 1 addition & 0 deletions spec/fixtures/cjs-syntax-error/syntax_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function(
Empty file added spec/fixtures/jasmine_spec/d.js
Empty file.
Empty file added spec/fixtures/jasmine_spec/e.js
Empty file.
Empty file added spec/fixtures/jasmine_spec/f.js
Empty file.
59 changes: 59 additions & 0 deletions spec/integration_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const child_process = require('child_process');

describe('Integration', function () {
it('supports ES modules', async function () {
let {exitCode, output} = await runJasmine('spec/fixtures/esm');
expect(exitCode).toEqual(0);
// Node < 14 outputs a warning when ES modules are used, e.g.:
// (node:5258) ExperimentalWarning: The ESM module loader is experimental.
// The position of this warning in the output varies. Sometimes it
// occurs before the lines we're interested in but sometimes it's in
// the middle of them.
output = output.replace(/^.*ExperimentalWarning.*$\n/m, '');
expect(output).toContain(
'name_reporter\n' +
'commonjs_helper\n' +
'esm_helper\n' +
'Started\n' +
'Spec: A spec file ending in .js is required as a commonjs module\n' +
'.Spec: A spec file ending in .mjs is imported as an es module\n'
);
});

it('handles load-time exceptions from CommonJS specs properly', async function () {
const {exitCode, output} = await runJasmine('spec/fixtures/cjs-load-exception');
expect(exitCode).toEqual(1);
expect(output).toContain('Error: nope');
expect(output).toMatch(/at .*throws_on_load.js/);
});

it('handles syntax errors in CommonJS specs properly', async function () {
const {exitCode, output} = await runJasmine('spec/fixtures/cjs-syntax-error');
expect(exitCode).toEqual(1);
expect(output).toContain('SyntaxError');
expect(output).toContain('syntax_error.js');
});
});

async function runJasmine(cwd) {
return new Promise(function(resolve) {
const child = child_process.spawn(
'node',
['--experimental-modules', '../../../bin/jasmine.js', '--config=jasmine.json'],
{
cwd,
shell: false
}
);
let output = '';
child.stdout.on('data', function (data) {
output += data;
});
child.stderr.on('data', function (data) {
output += data;
});
child.on('close', function (exitCode) {
resolve({exitCode, output});
});
});
}
2 changes: 1 addition & 1 deletion spec/support/jasmine.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"spec_dir": "spec",
"spec_files": [
"command_spec.js",
"esm_integration_spec.js",
"integration_spec.js",
"jasmine_spec.js",
"loader_spec.js",
"manager_spec.js",
Expand Down

0 comments on commit 555e69a

Please sign in to comment.