Adopt forbidDuplicateNames: true in jasmine-core's own tests

This commit is contained in:
Steve Gravrock
2025-09-14 10:18:56 -07:00
parent 78940aa0fb
commit cca6b2aa07
5 changed files with 262 additions and 232 deletions

View File

@@ -242,7 +242,7 @@ describe('Clock', function() {
expect(fakeGlobal.clearInterval).toBe(replacedClearInterval); expect(fakeGlobal.clearInterval).toBe(replacedClearInterval);
}); });
it('replaces the global timer functions on uninstall', function() { it('restores the global timer functions on uninstall', function() {
const fakeSetTimeout = jasmine.createSpy('global setTimeout'), const fakeSetTimeout = jasmine.createSpy('global setTimeout'),
fakeClearTimeout = jasmine.createSpy('global clearTimeout'), fakeClearTimeout = jasmine.createSpy('global clearTimeout'),
fakeSetInterval = jasmine.createSpy('global setInterval'), fakeSetInterval = jasmine.createSpy('global setInterval'),
@@ -408,7 +408,8 @@ describe('Clock', function() {
expect(delayedFunctionScheduler.scheduleFunction).not.toHaveBeenCalled(); expect(delayedFunctionScheduler.scheduleFunction).not.toHaveBeenCalled();
}); });
it('schedules the delayed function (via setTimeout) with the fake timer', function() { describe('setTimeout', function() {
it('schedules the delayed function with the fake timer', function() {
const fakeSetTimeout = jasmine.createSpy('setTimeout'), const fakeSetTimeout = jasmine.createSpy('setTimeout'),
scheduleFunction = jasmine.createSpy('scheduleFunction'), scheduleFunction = jasmine.createSpy('scheduleFunction'),
delayedFunctionScheduler = { scheduleFunction: scheduleFunction }, delayedFunctionScheduler = { scheduleFunction: scheduleFunction },
@@ -481,7 +482,9 @@ describe('Clock', function() {
expect(timeout.constructor.name).toEqual('FakeTimeout'); expect(timeout.constructor.name).toEqual('FakeTimeout');
} }
}); });
});
describe('clearTimeout', function() {
it('clears the scheduled function with the scheduler', function() { it('clears the scheduled function with the scheduler', function() {
const fakeClearTimeout = jasmine.createSpy('clearTimeout'), const fakeClearTimeout = jasmine.createSpy('clearTimeout'),
delayedFunctionScheduler = jasmine.createSpyObj( delayedFunctionScheduler = jasmine.createSpyObj(
@@ -506,11 +509,13 @@ describe('Clock', function() {
clock.clearTimeout(123); clock.clearTimeout(123);
expect(fakeClearTimeout).not.toHaveBeenCalled(); expect(fakeClearTimeout).not.toHaveBeenCalled();
expect(delayedFunctionScheduler.removeFunctionWithId).toHaveBeenCalledWith( expect(
123 delayedFunctionScheduler.removeFunctionWithId
); ).toHaveBeenCalledWith(123);
});
}); });
describe('setInterval', function() {
it('schedules the delayed function with the fake timer', function() { it('schedules the delayed function with the fake timer', function() {
const fakeSetInterval = jasmine.createSpy('setInterval'), const fakeSetInterval = jasmine.createSpy('setInterval'),
scheduleFunction = jasmine.createSpy('scheduleFunction'), scheduleFunction = jasmine.createSpy('scheduleFunction'),
@@ -585,7 +590,9 @@ describe('Clock', function() {
expect(interval.constructor.name).toEqual('FakeTimeout'); expect(interval.constructor.name).toEqual('FakeTimeout');
} }
}); });
});
describe('clearInterval', function() {
it('clears the scheduled function with the scheduler', function() { it('clears the scheduled function with the scheduler', function() {
const clearInterval = jasmine.createSpy('clearInterval'), const clearInterval = jasmine.createSpy('clearInterval'),
delayedFunctionScheduler = jasmine.createSpyObj( delayedFunctionScheduler = jasmine.createSpyObj(
@@ -610,9 +617,10 @@ describe('Clock', function() {
clock.clearInterval(123); clock.clearInterval(123);
expect(clearInterval).not.toHaveBeenCalled(); expect(clearInterval).not.toHaveBeenCalled();
expect(delayedFunctionScheduler.removeFunctionWithId).toHaveBeenCalledWith( expect(
123 delayedFunctionScheduler.removeFunctionWithId
); ).toHaveBeenCalledWith(123);
});
}); });
it('gives you a friendly reminder if the Clock is not installed and you tick', function() { it('gives you a friendly reminder if the Clock is not installed and you tick', function() {

View File

@@ -470,22 +470,30 @@ describe('Matchers (Integration)', function() {
}); });
describe('toBeNullish', function() { describe('toBeNullish', function() {
describe('with undefined', function() {
verifyPasses(function(env) { verifyPasses(function(env) {
env.expect(undefined).toBeNullish(); env.expect(undefined).toBeNullish();
}); });
});
describe('with null', function() {
verifyPasses(function(env) { verifyPasses(function(env) {
env.expect(null).toBeNullish(); env.expect(null).toBeNullish();
}); });
});
describe('with a truthy value', function() {
verifyFails(function(env) { verifyFails(function(env) {
env.expect(1).toBeNullish(); env.expect(1).toBeNullish();
}); });
});
describe('with a non-null falsy value', function() {
verifyFails(function(env) { verifyFails(function(env) {
env.expect('').toBeNullish(); env.expect('').toBeNullish();
}); });
}); });
});
describe('toContain', function() { describe('toContain', function() {
verifyPasses(function(env) { verifyPasses(function(env) {
@@ -665,15 +673,19 @@ describe('Matchers (Integration)', function() {
env.expect(spyObj).toHaveSpyInteractions(); env.expect(spyObj).toHaveSpyInteractions();
}); });
describe('with no methods called', function() {
verifyFails(function(env) { verifyFails(function(env) {
env.expect(spyObj).toHaveSpyInteractions(); env.expect(spyObj).toHaveSpyInteractions();
}); });
});
describe('with only non-spy methods called', function() {
verifyFails(function(env) { verifyFails(function(env) {
spyObj.otherMethod(); spyObj.otherMethod();
env.expect(spyObj).toHaveSpyInteractions(); env.expect(spyObj).toHaveSpyInteractions();
}); });
}); });
});
describe('toHaveNoOtherSpyInteractions', function() { describe('toHaveNoOtherSpyInteractions', function() {
let spyObj; let spyObj;

View File

@@ -281,6 +281,7 @@ describe('toThrowError', function() {
); );
}); });
describe('with a string', function() {
it('fails if thrown is a type of Error and the expected is a different Error', function() { it('fails if thrown is a type of Error and the expected is a different Error', function() {
const matchersUtil = { const matchersUtil = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(false), equals: jasmine.createSpy('delegated-equal').and.returnValue(false),
@@ -298,6 +299,27 @@ describe('toThrowError', function() {
"Expected function to throw TypeError with message 'bar', but it threw TypeError with message 'foo'." "Expected function to throw TypeError with message 'bar', but it threw TypeError with message 'foo'."
); );
}); });
});
describe('with a regex', function() {
it('fails if thrown is a type of Error and the expected is a different Error', function() {
const matchersUtil = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(false),
pp: jasmineUnderTest.makePrettyPrinter()
},
matcher = jasmineUnderTest.matchers.toThrowError(matchersUtil),
fn = function() {
throw new TypeError('foo');
};
const result = matcher.compare(fn, TypeError, /bar/);
expect(result.pass).toBe(false);
expect(result.message()).toEqual(
"Expected function to throw TypeError with a message matching /bar/, but it threw TypeError with message 'foo'."
);
});
});
it('passes if thrown is a type of Error and has the same type as the expected Error and the message matches the expected message', function() { it('passes if thrown is a type of Error and has the same type as the expected Error and the message matches the expected message', function() {
const matchersUtil = { const matchersUtil = {
@@ -316,22 +338,4 @@ describe('toThrowError', function() {
'Expected function not to throw TypeError with a message matching /foo/.' 'Expected function not to throw TypeError with a message matching /foo/.'
); );
}); });
it('fails if thrown is a type of Error and the expected is a different Error', function() {
const matchersUtil = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(false),
pp: jasmineUnderTest.makePrettyPrinter()
},
matcher = jasmineUnderTest.matchers.toThrowError(matchersUtil),
fn = function() {
throw new TypeError('foo');
};
const result = matcher.compare(fn, TypeError, /bar/);
expect(result.pass).toBe(false);
expect(result.message()).toEqual(
"Expected function to throw TypeError with a message matching /bar/, but it threw TypeError with message 'foo'."
);
});
}); });

View File

@@ -26,6 +26,9 @@ module.exports = {
'helpers/defineJasmineUnderTest.js', 'helpers/defineJasmineUnderTest.js',
'helpers/resetEnv.js' 'helpers/resetEnv.js'
], ],
env: {
forbidDuplicateNames: true
},
random: true, random: true,
browser: { browser: {
name: process.env.JASMINE_BROWSER || 'firefox', name: process.env.JASMINE_BROWSER || 'firefox',

View File

@@ -12,5 +12,8 @@
"helpers/nodeDefineJasmineUnderTest.js", "helpers/nodeDefineJasmineUnderTest.js",
"helpers/resetEnv.js" "helpers/resetEnv.js"
], ],
"env": {
"forbidDuplicateNames": true
},
"random": true "random": true
} }