Improved readability of matcher-related deprecations

* Include stack traces. This makes it easier to find the matcher that
needs to be updated, particularly when it comes from a library rather
than the user's own code.

* Show each deprecation only once unless `config.verboseDeprecations`
is set. Since matchers are often added in a global `beforeEach`, logging
deprecations every time can be overwhelming.
This commit is contained in:
Steve Gravrock
2020-01-18 10:39:51 -08:00
committed by Steve Gravrock
parent 90d6f9d73c
commit 9aed55bb91
11 changed files with 216 additions and 47 deletions

View File

@@ -281,6 +281,60 @@ describe('Env', function() {
});
});
describe('#deprecatedOnceWithStack', function() {
it('includes a stack trace', function() {
spyOn(env, 'deprecated');
env.deprecatedOnceWithStack('msg');
expect(env.deprecated).toHaveBeenCalled();
var msg = env.deprecated.calls.argsFor(0)[0];
expect(msg).toContain('msg');
expect(msg).toContain('EnvSpec.js');
expect(msg).not.toContain('Error');
});
describe('When verboseDeprecations is true', function() {
it('calls #deprecated every time', function() {
env.configure({ verboseDeprecations: true });
spyOn(env, 'deprecated');
env.deprecatedOnceWithStack('msg');
env.deprecatedOnceWithStack('msg');
expect(env.deprecated).toHaveBeenCalledWith(
jasmine.stringMatching(/msg/)
);
expect(env.deprecated).toHaveBeenCalledTimes(2);
expect(env.deprecated).not.toHaveBeenCalledWith(
jasmine.stringMatching(/only once/)
);
});
});
describe('When verboseDeprecations is false', function() {
it('calls #deprecated once per unique message', function() {
env.configure({ verboseDeprecations: false });
spyOn(env, 'deprecated');
env.deprecatedOnceWithStack('foo');
env.deprecatedOnceWithStack('bar');
env.deprecatedOnceWithStack('foo');
expect(env.deprecated).toHaveBeenCalledWith(
jasmine.stringMatching(
/foo\nNote: This message will be shown only once. Set config.verboseDeprecations to true to see every occurrence/m
)
);
expect(env.deprecated).toHaveBeenCalledWith(
jasmine.stringMatching(
/bar\nNote: This message will be shown only once. Set config.verboseDeprecations to true to see every occurrence/m
)
);
expect(env.deprecated).toHaveBeenCalledTimes(2);
});
});
});
describe('when not constructed with suppressLoadErrors: true', function() {
it('installs a global error handler on construction', function() {
var globalErrors = jasmine.createSpyObj('globalErrors', [

View File

@@ -148,22 +148,33 @@ describe('Custom Async Matchers (Integration)', function() {
env.execute();
});
it('logs a deprecation warning if the matcher factory takes two arguments', function (done) {
it('logs a deprecation once per matcher if the matcher factory takes two arguments', function (done) {
var matcherFactory = function (matchersUtil, customEqualityTesters) {
return { compare: function () {} };
};
spyOn(env, 'deprecated');
env.it('a spec', function() {
env.beforeEach(function() {
env.addAsyncMatchers({toBeFoo: matcherFactory});
env.addAsyncMatchers({toBeBar: matcherFactory});
});
env.it('a spec', function() {});
env.it('another spec', function() {});
function jasmineDone() {
expect(env.deprecated).toHaveBeenCalledWith('The matcher factory for "toBeFoo" ' +
'accepts custom equality testers, but this parameter will no longer be passed ' +
'in a future release. ' +
'See <https://jasmine.github.io/tutorials/upgrading_to_4.0> for details.');
expect(env.deprecated).toHaveBeenCalledWith(jasmine.stringMatching(
'The matcher factory for "toBeFoo" accepts custom equality testers, ' +
'but this parameter will no longer be passed in a future release. ' +
'See <https://jasmine.github.io/tutorials/upgrading_to_4.0> for details.'
));
expect(env.deprecated).toHaveBeenCalledWith(jasmine.stringMatching(
'The matcher factory for "toBeBar" accepts custom equality testers, ' +
'but this parameter will no longer be passed in a future release. ' +
'See <https://jasmine.github.io/tutorials/upgrading_to_4.0> for details.'
));
expect(env.deprecated).toHaveBeenCalledTimes(2);
done();
}

View File

@@ -267,22 +267,33 @@ describe("Custom Matchers (Integration)", function () {
env.execute();
});
it('logs a deprecation warning if the matcher factory takes two arguments', function(done) {
it('logs a deprecation once per matcher if the matcher factory takes two arguments', function(done) {
var matcherFactory = function (matchersUtil, customEqualityTesters) {
return { compare: function() {} };
};
spyOn(env, 'deprecated');
env.it('a spec', function() {
env.beforeEach(function() {
env.addMatchers({toBeFoo: matcherFactory});
env.addMatchers({toBeBar: matcherFactory});
});
env.it('a spec', function() {});
env.it('another spec', function() {});
function jasmineDone() {
expect(env.deprecated).toHaveBeenCalledWith('The matcher factory for "toBeFoo" ' +
'accepts custom equality testers, but this parameter will no longer be passed ' +
'in a future release. ' +
'See <https://jasmine.github.io/tutorials/upgrading_to_4.0> for details.');
expect(env.deprecated).toHaveBeenCalledWith(jasmine.stringMatching(
'The matcher factory for "toBeFoo" accepts custom equality testers, ' +
'but this parameter will no longer be passed in a future release. ' +
'See <https://jasmine.github.io/tutorials/upgrading_to_4.0> for details.'
));
expect(env.deprecated).toHaveBeenCalledWith(jasmine.stringMatching(
'The matcher factory for "toBeBar" accepts custom equality testers, ' +
'but this parameter will no longer be passed in a future release. ' +
'See <https://jasmine.github.io/tutorials/upgrading_to_4.0> for details.'
));
expect(env.deprecated).toHaveBeenCalledTimes(2);
done();
}

View File

@@ -856,20 +856,26 @@ describe("matchersUtil", function() {
matchersUtil.equals(0, 0, []);
expect(deprecated).toHaveBeenCalledWith('Passing custom equality testers ' +
expect(deprecated).toHaveBeenCalledWith(jasmine.stringMatching(
'Passing custom equality testers ' +
'to MatchersUtil#equals is deprecated. ' +
'See <https://jasmine.github.io/tutorials/upgrading_to_4.0> for details.');
'See <https://jasmine.github.io/tutorials/upgrading_to_4.0> for details.'
));
});
it('logs a deprecation warning when a diffBuilder is provided as the fourth argument', function() {
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
deprecated = spyOn(jasmineUnderTest.getEnv(), 'deprecated');
debugger;
matchersUtil.equals(0, 0, null, new jasmineUnderTest.NullDiffBuilder());
expect(deprecated).toHaveBeenCalledWith('Diff builder should be passed as the ' +
expect(deprecated).toHaveBeenCalledWith(jasmine.stringMatching(
'Diff builder should be passed as the ' +
'third argument to MatchersUtil#equals, not the fourth. ' +
'See <https://jasmine.github.io/tutorials/upgrading_to_4.0> for details.');
'See <https://jasmine.github.io/tutorials/upgrading_to_4.0> for details.'
));
});
it('uses a diffBuilder if one is provided as the fourth argument', function() {
@@ -936,9 +942,10 @@ describe("matchersUtil", function() {
expect(matchersUtil.contains([1, 2], 3, [customTester])).toBe(true);
expect(deprecated).toHaveBeenCalledWith('Passing custom equality testers ' +
'to MatchersUtil#contains is deprecated. ' +
'See <https://jasmine.github.io/tutorials/upgrading_to_4.0> for details.');
expect(deprecated).toHaveBeenCalledWith(jasmine.stringMatching(
'Passing custom equality testers to MatchersUtil#contains is deprecated. ' +
'See <https://jasmine.github.io/tutorials/upgrading_to_4.0> for details.'
));
});
it("uses custom equality testers if passed to the constructor and actual is an Array", function() {