Convert Spec to an es6 class

This commit is contained in:
Steve Gravrock
2025-08-30 15:47:28 -07:00
parent fa9939ae94
commit 54465f6f6a
2 changed files with 484 additions and 494 deletions

View File

@@ -771,7 +771,8 @@ getJasmineRequireObj().util = function(j$) {
};
getJasmineRequireObj().Spec = function(j$) {
function Spec(attrs) {
class Spec {
constructor(attrs) {
this.expectationFactory = attrs.expectationFactory;
this.asyncExpectationFactory = attrs.asyncExpectationFactory;
this.id = attrs.id;
@@ -790,7 +791,9 @@ getJasmineRequireObj().Spec = function(j$) {
return {};
};
this.autoCleanClosures =
attrs.autoCleanClosures === undefined ? true : !!attrs.autoCleanClosures;
attrs.autoCleanClosures === undefined
? true
: !!attrs.autoCleanClosures;
this.getPath = function() {
return attrs.getPath ? attrs.getPath(this) : [];
@@ -812,7 +815,7 @@ getJasmineRequireObj().Spec = function(j$) {
this.reset();
}
Spec.prototype.addExpectationResult = function(passed, data, isError) {
addExpectationResult(passed, data, isError) {
const expectationResult = j$.buildExpectationResult(data);
if (passed) {
@@ -833,23 +836,23 @@ getJasmineRequireObj().Spec = function(j$) {
throw new j$.errors.ExpectationFailed();
}
}
};
}
Spec.prototype.getSpecProperty = function(key) {
getSpecProperty(key) {
this.result.properties = this.result.properties || {};
return this.result.properties[key];
};
}
Spec.prototype.setSpecProperty = function(key, value) {
setSpecProperty(key, value) {
this.result.properties = this.result.properties || {};
this.result.properties[key] = value;
};
}
Spec.prototype.executionStarted = function() {
executionStarted() {
this.timer.start();
};
}
Spec.prototype.executionFinished = function(excluded, failSpecWithNoExp) {
executionFinished(excluded, failSpecWithNoExp) {
if (this.autoCleanClosures) {
this.queueableFn.fn = null;
}
@@ -860,9 +863,9 @@ getJasmineRequireObj().Spec = function(j$) {
if (this.result.status !== 'failed') {
this.result.debugLogs = null;
}
};
}
Spec.prototype.reset = function() {
reset() {
/**
* @typedef SpecResult
* @property {String} id - The unique id of this spec.
@@ -901,9 +904,9 @@ getJasmineRequireObj().Spec = function(j$) {
};
this.markedPending = this.markedExcluding;
this.reportedDone = false;
};
}
Spec.prototype.handleException = function handleException(e) {
handleException(e) {
if (Spec.isPendingSpecException(e)) {
this.pend(extractCustomPendingMessage(e));
return;
@@ -924,40 +927,33 @@ getJasmineRequireObj().Spec = function(j$) {
},
true
);
};
}
/*
* Marks state as pending
* @param {string} [message] An optional reason message
*/
Spec.prototype.pend = function(message) {
pend(message) {
this.markedPending = true;
if (message) {
this.result.pendingReason = message;
}
};
}
/*
* Like {@link Spec#pend}, but pending state will survive {@link Spec#reset}
* Useful for fit, xit, where pending state remains.
* @param {string} [message] An optional reason message
*/
Spec.prototype.exclude = function(message) {
// Like pend(), but pending state will survive reset().
// Useful for fit, xit, where pending state remains.
exclude(message) {
this.markedExcluding = true;
if (this.message) {
this.excludeMessage = message;
}
this.pend(message);
};
}
// TODO: ensure that all access to result goes through .getResult()
// so that the status is correct.
Spec.prototype.getResult = function() {
getResult() {
this.result.status = this.status();
return this.result;
};
}
Spec.prototype.status = function(excluded, failSpecWithNoExpectations) {
status(excluded, failSpecWithNoExpectations) {
if (excluded === true) {
return 'excluded';
}
@@ -977,22 +973,22 @@ getJasmineRequireObj().Spec = function(j$) {
}
return 'passed';
};
}
Spec.prototype.getFullName = function() {
getFullName() {
return this.getPath().join(' ');
};
}
Spec.prototype.addDeprecationWarning = function(deprecation) {
addDeprecationWarning(deprecation) {
if (typeof deprecation === 'string') {
deprecation = { message: deprecation };
}
this.result.deprecationWarnings.push(
j$.buildExpectationResult(deprecation)
);
};
}
Spec.prototype.debugLog = function(msg) {
debugLog(msg) {
if (!this.result.debugLogs) {
this.result.debugLogs = [];
}
@@ -1007,38 +1003,18 @@ getJasmineRequireObj().Spec = function(j$) {
message: msg,
timestamp: this.timer.elapsed()
});
};
const extractCustomPendingMessage = function(e) {
const fullMessage = e.toString(),
boilerplateStart = fullMessage.indexOf(Spec.pendingSpecExceptionMessage),
boilerplateEnd =
boilerplateStart + Spec.pendingSpecExceptionMessage.length;
return fullMessage.slice(boilerplateEnd);
};
Spec.pendingSpecExceptionMessage = '=> marked Pending';
Spec.isPendingSpecException = function(e) {
return !!(
e &&
e.toString &&
e.toString().indexOf(Spec.pendingSpecExceptionMessage) !== -1
);
};
}
/**
* @interface Spec
* @see Configuration#specFilter
* @since 2.0.0
*/
Object.defineProperty(Spec.prototype, 'metadata', {
get metadata() {
// NOTE: Although most of jasmine-core only exposes these metadata objects,
// actual Spec instances are still passed to Configuration#specFilter. Until
// that is fixed, it's important to make sure that all metadata properties
// also exist in compatible form on the underlying Spec.
get: function() {
if (!this.metadata_) {
this.metadata_ = {
/**
@@ -1081,7 +1057,26 @@ getJasmineRequireObj().Spec = function(j$) {
return this.metadata_;
}
});
}
const extractCustomPendingMessage = function(e) {
const fullMessage = e.toString(),
boilerplateStart = fullMessage.indexOf(Spec.pendingSpecExceptionMessage),
boilerplateEnd =
boilerplateStart + Spec.pendingSpecExceptionMessage.length;
return fullMessage.slice(boilerplateEnd);
};
Spec.pendingSpecExceptionMessage = '=> marked Pending';
Spec.isPendingSpecException = function(e) {
return !!(
e &&
e.toString &&
e.toString().indexOf(Spec.pendingSpecExceptionMessage) !== -1
);
};
return Spec;
};

View File

@@ -1,5 +1,6 @@
getJasmineRequireObj().Spec = function(j$) {
function Spec(attrs) {
class Spec {
constructor(attrs) {
this.expectationFactory = attrs.expectationFactory;
this.asyncExpectationFactory = attrs.asyncExpectationFactory;
this.id = attrs.id;
@@ -18,7 +19,9 @@ getJasmineRequireObj().Spec = function(j$) {
return {};
};
this.autoCleanClosures =
attrs.autoCleanClosures === undefined ? true : !!attrs.autoCleanClosures;
attrs.autoCleanClosures === undefined
? true
: !!attrs.autoCleanClosures;
this.getPath = function() {
return attrs.getPath ? attrs.getPath(this) : [];
@@ -40,7 +43,7 @@ getJasmineRequireObj().Spec = function(j$) {
this.reset();
}
Spec.prototype.addExpectationResult = function(passed, data, isError) {
addExpectationResult(passed, data, isError) {
const expectationResult = j$.buildExpectationResult(data);
if (passed) {
@@ -61,23 +64,23 @@ getJasmineRequireObj().Spec = function(j$) {
throw new j$.errors.ExpectationFailed();
}
}
};
}
Spec.prototype.getSpecProperty = function(key) {
getSpecProperty(key) {
this.result.properties = this.result.properties || {};
return this.result.properties[key];
};
}
Spec.prototype.setSpecProperty = function(key, value) {
setSpecProperty(key, value) {
this.result.properties = this.result.properties || {};
this.result.properties[key] = value;
};
}
Spec.prototype.executionStarted = function() {
executionStarted() {
this.timer.start();
};
}
Spec.prototype.executionFinished = function(excluded, failSpecWithNoExp) {
executionFinished(excluded, failSpecWithNoExp) {
if (this.autoCleanClosures) {
this.queueableFn.fn = null;
}
@@ -88,9 +91,9 @@ getJasmineRequireObj().Spec = function(j$) {
if (this.result.status !== 'failed') {
this.result.debugLogs = null;
}
};
}
Spec.prototype.reset = function() {
reset() {
/**
* @typedef SpecResult
* @property {String} id - The unique id of this spec.
@@ -129,9 +132,9 @@ getJasmineRequireObj().Spec = function(j$) {
};
this.markedPending = this.markedExcluding;
this.reportedDone = false;
};
}
Spec.prototype.handleException = function handleException(e) {
handleException(e) {
if (Spec.isPendingSpecException(e)) {
this.pend(extractCustomPendingMessage(e));
return;
@@ -152,40 +155,33 @@ getJasmineRequireObj().Spec = function(j$) {
},
true
);
};
}
/*
* Marks state as pending
* @param {string} [message] An optional reason message
*/
Spec.prototype.pend = function(message) {
pend(message) {
this.markedPending = true;
if (message) {
this.result.pendingReason = message;
}
};
}
/*
* Like {@link Spec#pend}, but pending state will survive {@link Spec#reset}
* Useful for fit, xit, where pending state remains.
* @param {string} [message] An optional reason message
*/
Spec.prototype.exclude = function(message) {
// Like pend(), but pending state will survive reset().
// Useful for fit, xit, where pending state remains.
exclude(message) {
this.markedExcluding = true;
if (this.message) {
this.excludeMessage = message;
}
this.pend(message);
};
}
// TODO: ensure that all access to result goes through .getResult()
// so that the status is correct.
Spec.prototype.getResult = function() {
getResult() {
this.result.status = this.status();
return this.result;
};
}
Spec.prototype.status = function(excluded, failSpecWithNoExpectations) {
status(excluded, failSpecWithNoExpectations) {
if (excluded === true) {
return 'excluded';
}
@@ -205,22 +201,22 @@ getJasmineRequireObj().Spec = function(j$) {
}
return 'passed';
};
}
Spec.prototype.getFullName = function() {
getFullName() {
return this.getPath().join(' ');
};
}
Spec.prototype.addDeprecationWarning = function(deprecation) {
addDeprecationWarning(deprecation) {
if (typeof deprecation === 'string') {
deprecation = { message: deprecation };
}
this.result.deprecationWarnings.push(
j$.buildExpectationResult(deprecation)
);
};
}
Spec.prototype.debugLog = function(msg) {
debugLog(msg) {
if (!this.result.debugLogs) {
this.result.debugLogs = [];
}
@@ -235,38 +231,18 @@ getJasmineRequireObj().Spec = function(j$) {
message: msg,
timestamp: this.timer.elapsed()
});
};
const extractCustomPendingMessage = function(e) {
const fullMessage = e.toString(),
boilerplateStart = fullMessage.indexOf(Spec.pendingSpecExceptionMessage),
boilerplateEnd =
boilerplateStart + Spec.pendingSpecExceptionMessage.length;
return fullMessage.slice(boilerplateEnd);
};
Spec.pendingSpecExceptionMessage = '=> marked Pending';
Spec.isPendingSpecException = function(e) {
return !!(
e &&
e.toString &&
e.toString().indexOf(Spec.pendingSpecExceptionMessage) !== -1
);
};
}
/**
* @interface Spec
* @see Configuration#specFilter
* @since 2.0.0
*/
Object.defineProperty(Spec.prototype, 'metadata', {
get metadata() {
// NOTE: Although most of jasmine-core only exposes these metadata objects,
// actual Spec instances are still passed to Configuration#specFilter. Until
// that is fixed, it's important to make sure that all metadata properties
// also exist in compatible form on the underlying Spec.
get: function() {
if (!this.metadata_) {
this.metadata_ = {
/**
@@ -309,7 +285,26 @@ getJasmineRequireObj().Spec = function(j$) {
return this.metadata_;
}
});
}
const extractCustomPendingMessage = function(e) {
const fullMessage = e.toString(),
boilerplateStart = fullMessage.indexOf(Spec.pendingSpecExceptionMessage),
boilerplateEnd =
boilerplateStart + Spec.pendingSpecExceptionMessage.length;
return fullMessage.slice(boilerplateEnd);
};
Spec.pendingSpecExceptionMessage = '=> marked Pending';
Spec.isPendingSpecException = function(e) {
return !!(
e &&
e.toString &&
e.toString().indexOf(Spec.pendingSpecExceptionMessage) !== -1
);
};
return Spec;
};