Statically expose pretty printer as jasmine.pp

Pretty printing is occasionally useful outside of the places where a
configured pretty printer is injected (matchers and asymmetric equality
testers). Users sometimes use the private basicPrettyPrinter for that.
jasmine.pp is part of the public interface and uses the current runable's
custom object formatters.
This commit is contained in:
Steve Gravrock
2025-11-22 09:44:54 -08:00
parent 788eba34b6
commit 32168be6c7
5 changed files with 74 additions and 2 deletions

View File

@@ -2045,6 +2045,13 @@ getJasmineRequireObj().Env = function(j$) {
}
};
this.pp = function(value) {
const pp = runner.currentRunable()
? runableResources.makePrettyPrinter()
: j$.private.basicPrettyPrinter;
return pp(value);
};
this.cleanup_ = function() {
uninstallGlobalErrors();
};
@@ -5288,7 +5295,7 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
this.customTesters_ = options.customTesters || [];
/**
* Formats a value for use in matcher failure messages and similar contexts,
* taking into account the current set of custom value formatters.
* taking into account the current set of custom object formatters.
* @function
* @name MatchersUtil#pp
* @since 3.6.0
@@ -9500,6 +9507,21 @@ getJasmineRequireObj().interface = function(jasmine, env) {
return env.setDefaultSpyStrategy(defaultStrategyFn);
};
/**
* Formats a value for display, taking into account the current set of
* custom object formatters.
*
* @name jasmine.pp
* @function
* @since 6.0.0
* @param {*} value The value to pretty-print
* @return {string} The pretty-printed value
* @see {MatchersUtil#pp}
*/
jasmine.pp = function(value) {
return env.pp(value);
};
/**
* {@link AsymmetricEqualityTester|Asymmetric equality testers} allow for
* non-exact matching in matchers that use Jasmine's deep value equality

View File

@@ -3842,6 +3842,34 @@ describe('Env integration', function() {
});
});
describe('pp', function() {
it("pretty-prints using the current runable's custom object formatters", async function() {
env.it('a spec', function() {
env.addCustomObjectFormatter(function(x) {
if (x === 1) {
return 'hi!';
}
});
env.expect(env.pp(1)).toEqual('hi!');
});
const reporter = jasmine.createSpyObj('reporter', ['specDone']);
env.addReporter(reporter);
await env.execute();
expect(reporter.specDone).toHaveBeenCalledWith(
jasmine.objectContaining({
failedExpectations: []
})
);
});
it('works when there is no current runable', function() {
expect(env.pp({ some: 'thing' })).toEqual("Object({ some: 'thing' })");
});
});
it('forbids duplicates when forbidDuplicateNames is true', function() {
env.configure({ forbidDuplicateNames: true });
env.it('a spec');

View File

@@ -821,6 +821,13 @@ getJasmineRequireObj().Env = function(j$) {
}
};
this.pp = function(value) {
const pp = runner.currentRunable()
? runableResources.makePrettyPrinter()
: j$.private.basicPrettyPrinter;
return pp(value);
};
this.cleanup_ = function() {
uninstallGlobalErrors();
};

View File

@@ -13,7 +13,7 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
this.customTesters_ = options.customTesters || [];
/**
* Formats a value for use in matcher failure messages and similar contexts,
* taking into account the current set of custom value formatters.
* taking into account the current set of custom object formatters.
* @function
* @name MatchersUtil#pp
* @since 3.6.0

View File

@@ -498,6 +498,21 @@ getJasmineRequireObj().interface = function(jasmine, env) {
return env.setDefaultSpyStrategy(defaultStrategyFn);
};
/**
* Formats a value for display, taking into account the current set of
* custom object formatters.
*
* @name jasmine.pp
* @function
* @since 6.0.0
* @param {*} value The value to pretty-print
* @return {string} The pretty-printed value
* @see {MatchersUtil#pp}
*/
jasmine.pp = function(value) {
return env.pp(value);
};
/**
* {@link AsymmetricEqualityTester|Asymmetric equality testers} allow for
* non-exact matching in matchers that use Jasmine's deep value equality