Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add better error checking and message #5911 #5311 #6794

Closed
wants to merge 5 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 61 additions & 49 deletions packages/jest-jasmine2/src/jasmine/Env.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import checkIsError from '../is_error';
// eslint-disable-next-line no-unused-vars
const Promise = global[Symbol.for('jest-native-promise')] || global.Promise;

export default function(j$) {
export default function (j$) {
function Env(options) {
options = options || {};

Expand All @@ -60,11 +60,11 @@ export default function(j$) {
let random = false;
let seed = null;

const currentSuite = function() {
const currentSuite = function () {
return currentlyExecutingSuites[currentlyExecutingSuites.length - 1];
};

const currentRunnable = function() {
const currentRunnable = function () {
return currentSpec || currentSuite();
};

Expand All @@ -77,33 +77,35 @@ export default function(j$) {
'specDone',
]);

this.specFilter = function() {
this.specFilter = function () {
return true;
};

let nextSpecId = 0;
const getNextSpecId = function() {
const getNextSpecId = function () {
return 'spec' + nextSpecId++;
};

let nextSuiteId = 0;
const getNextSuiteId = function() {
const getNextSuiteId = function () {
return 'suite' + nextSuiteId++;
};

const defaultResourcesForRunnable = function(id, parentRunnableId) {
const resources = {spies: []};
const defaultResourcesForRunnable = function (id, parentRunnableId) {
const resources = {
spies: []
};

runnableResources[id] = resources;
};

const clearResourcesForRunnable = function(id) {
const clearResourcesForRunnable = function (id) {
spyRegistry.clearSpies();
delete runnableResources[id];
};

const beforeAndAfterFns = function(suite) {
return function() {
const beforeAndAfterFns = function (suite) {
return function () {
let afters = [];
let befores = [];

Expand All @@ -121,7 +123,7 @@ export default function(j$) {
};
};

const getSpecName = function(spec, suite) {
const getSpecName = function (spec, suite) {
const fullName = [spec.description];
const suiteFullName = suite.getFullName();

Expand All @@ -132,32 +134,32 @@ export default function(j$) {
return fullName.join(' ');
};

this.catchExceptions = function(value) {
this.catchExceptions = function (value) {
catchExceptions = !!value;
return catchExceptions;
};

this.catchingExceptions = function() {
this.catchingExceptions = function () {
return catchExceptions;
};

this.throwOnExpectationFailure = function(value) {
this.throwOnExpectationFailure = function (value) {
throwOnExpectationFailure = !!value;
};

this.throwingExpectationFailures = function() {
this.throwingExpectationFailures = function () {
return throwOnExpectationFailure;
};

this.randomizeTests = function(value) {
this.randomizeTests = function (value) {
random = !!value;
};

this.randomTests = function() {
this.randomTests = function () {
return random;
};

this.seed = function(value) {
this.seed = function (value) {
if (value) {
seed = value;
}
Expand All @@ -180,7 +182,7 @@ export default function(j$) {

currentDeclarationSuite = topSuite;

this.topSuite = function() {
this.topSuite = function () {
return topSuite;
};

Expand All @@ -196,7 +198,7 @@ export default function(j$) {

let oldListenersException;
let oldListenersRejection;
const executionSetup = function() {
const executionSetup = function () {
// Need to ensure we are the only ones handling these exceptions.
oldListenersException = process.listeners('uncaughtException').slice();
oldListenersRejection = process.listeners('unhandledRejection').slice();
Expand All @@ -208,7 +210,7 @@ export default function(j$) {
j$.process.on('unhandledRejection', uncaught);
};

const executionTeardown = function() {
const executionTeardown = function () {
j$.process.removeListener('uncaughtException', uncaught);
j$.process.removeListener('unhandledRejection', uncaught);

Expand All @@ -222,7 +224,7 @@ export default function(j$) {
});
};

this.execute = async function(runnablesToRun, suiteTree = topSuite) {
this.execute = async function (runnablesToRun, suiteTree = topSuite) {
if (!runnablesToRun) {
if (focusedRunnables.length) {
runnablesToRun = focusedRunnables;
Expand Down Expand Up @@ -258,7 +260,9 @@ export default function(j$) {
suite.parentSuite && suite.parentSuite.id,
);
if (suite === topSuite) {
reporter.jasmineStarted({totalSpecsDefined});
reporter.jasmineStarted({
totalSpecsDefined
});
} else {
reporter.suiteStarted(suite.result);
}
Expand All @@ -275,15 +279,15 @@ export default function(j$) {
}
};

this.addReporter = function(reporterToAdd) {
this.addReporter = function (reporterToAdd) {
reporter.addReporter(reporterToAdd);
};

this.provideFallbackReporter = function(reporterToAdd) {
this.provideFallbackReporter = function (reporterToAdd) {
reporter.provideFallbackReporter(reporterToAdd);
};

this.clearReporters = function() {
this.clearReporters = function () {
reporter.clearReporters();
};

Expand All @@ -298,15 +302,15 @@ export default function(j$) {
},
});

this.allowRespy = function(allow) {
this.allowRespy = function (allow) {
spyRegistry.allowRespy(allow);
};

this.spyOn = function() {
this.spyOn = function () {
return spyRegistry.spyOn.apply(spyRegistry, arguments);
};

const suiteFactory = function(description) {
const suiteFactory = function (description) {
const suite = new j$.Suite({
id: getNextSuiteId(),
description,
Expand All @@ -320,7 +324,7 @@ export default function(j$) {
return suite;
};

this.describe = function(description, specDefinitions) {
this.describe = function (description, specDefinitions) {
const suite = suiteFactory(description);
if (specDefinitions.length > 0) {
throw new Error('describe does not expect any arguments');
Expand All @@ -332,7 +336,7 @@ export default function(j$) {
return suite;
};

this.xdescribe = function(description, specDefinitions) {
this.xdescribe = function (description, specDefinitions) {
const suite = suiteFactory(description);
suite.pend();
addSpecsToSuite(suite, specDefinitions);
Expand All @@ -341,7 +345,7 @@ export default function(j$) {

const focusedRunnables = [];

this.fdescribe = function(description, specDefinitions) {
this.fdescribe = function (description, specDefinitions) {
const suite = suiteFactory(description);
suite.isFocused = true;

Expand Down Expand Up @@ -396,7 +400,7 @@ export default function(j$) {
}
}

const specFactory = function(description, fn, suite, timeout) {
const specFactory = function (description, fn, suite, timeout) {
totalSpecsDefined++;
const spec = new j$.Spec({
id: getNextSpecId(),
Expand Down Expand Up @@ -442,7 +446,7 @@ export default function(j$) {
}
};

this.it = function(description, fn, timeout) {
this.it = function (description, fn, timeout) {
if (typeof description !== 'string') {
throw new Error(
`Invalid first argument, ${description}. It must be a string.`,
Expand Down Expand Up @@ -473,23 +477,23 @@ export default function(j$) {
if (currentSpec !== null) {
throw new Error(
'Tests cannot be nested. Test `' +
spec.description +
'` cannot run because it is nested within `' +
currentSpec.description +
'`.',
spec.description +
'` cannot run because it is nested within `' +
currentSpec.description +
'`.',
);
}
currentDeclarationSuite.addChild(spec);
return spec;
};

this.xit = function() {
this.xit = function () {
const spec = this.it.apply(this, arguments);
spec.pend('Temporarily disabled with xit');
return spec;
};

this.fit = function(description, fn, timeout) {
this.fit = function (description, fn, timeout) {
const spec = specFactory(
description,
fn,
Expand All @@ -502,7 +506,7 @@ export default function(j$) {
return spec;
};

this.beforeEach = function(beforeEachFunction, timeout) {
this.beforeEach = function (beforeEachFunction, timeout) {
currentDeclarationSuite.beforeEach({
fn: beforeEachFunction,
timeout() {
Expand All @@ -511,7 +515,7 @@ export default function(j$) {
});
};

this.beforeAll = function(beforeAllFunction, timeout) {
this.beforeAll = function (beforeAllFunction, timeout) {
currentDeclarationSuite.beforeAll({
fn: beforeAllFunction,
timeout() {
Expand All @@ -520,7 +524,7 @@ export default function(j$) {
});
};

this.afterEach = function(afterEachFunction, timeout) {
this.afterEach = function (afterEachFunction, timeout) {
currentDeclarationSuite.afterEach({
fn: afterEachFunction,
timeout() {
Expand All @@ -529,7 +533,7 @@ export default function(j$) {
});
};

this.afterAll = function(afterAllFunction, timeout) {
this.afterAll = function (afterAllFunction, timeout) {
currentDeclarationSuite.afterAll({
fn: afterAllFunction,
timeout() {
Expand All @@ -538,18 +542,26 @@ export default function(j$) {
});
};

this.pending = function(message) {
this.pending = function (message) {
let fullMessage = j$.Spec.pendingSpecExceptionMessage;
if (message) {
fullMessage += message;
}
throw fullMessage;
};

this.fail = function(error) {
const {isError, message} = checkIsError(error);
this.fail = function (error) {
const {
isError,
message
} = checkIsError(error);

const runnable = currentRunnable();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can move this above checkIsError as that's not necessary if we throw here.

if (!runnable) {
throw new Error('Caught error after test environment was torn down');
}

currentRunnable().addExpectationResult(false, {
runnable.addExpectationResult(false, {
matcherName: '',
passed: false,
expected: '',
Expand Down