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

@@ -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');