Don't attach spec helpers to the env

This commit is contained in:
Steve Gravrock
2023-08-26 11:36:48 -07:00
parent bff612a169
commit 39f9c2e1a0
11 changed files with 35 additions and 46 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ describe('Env integration', function() {
const isBrowser = typeof window !== 'undefined'; const isBrowser = typeof window !== 'undefined';
beforeEach(function() { beforeEach(function() {
jasmine.getEnv().registerIntegrationMatchers(); specHelpers.registerIntegrationMatchers();
env = new jasmineUnderTest.Env(); env = new jasmineUnderTest.Env();
}); });
+2 -8
View File
@@ -610,19 +610,13 @@ describe('Matchers (Integration)', function() {
}); });
describe('toHaveClass', function() { describe('toHaveClass', function() {
beforeEach(function() {
this.domHelpers = jasmine.getEnv().domHelpers();
});
verifyPasses(function(env) { verifyPasses(function(env) {
const domHelpers = jasmine.getEnv().domHelpers(); const el = specHelpers.domHelpers.createElementWithClassName('foo');
const el = domHelpers.createElementWithClassName('foo');
env.expect(el).toHaveClass('foo'); env.expect(el).toHaveClass('foo');
}); });
verifyFails(function(env) { verifyFails(function(env) {
const domHelpers = jasmine.getEnv().domHelpers(); const el = specHelpers.domHelpers.createElementWithClassName('foo');
const el = domHelpers.createElementWithClassName('foo');
env.expect(el).toHaveClass('bar'); env.expect(el).toHaveClass('bar');
}); });
}); });
+1 -1
View File
@@ -2,7 +2,7 @@ describe('spec running', function() {
let env; let env;
beforeEach(function() { beforeEach(function() {
jasmine.getEnv().registerIntegrationMatchers(); specHelpers.registerIntegrationMatchers();
env = new jasmineUnderTest.Env(); env = new jasmineUnderTest.Env();
env.configure({ random: false }); env.configure({ random: false });
}); });
+4 -8
View File
@@ -1,12 +1,8 @@
describe('toHaveClass', function() { describe('toHaveClass', function() {
beforeEach(function() {
this.domHelpers = jasmine.getEnv().domHelpers();
});
it('fails for a DOM element that lacks the expected class', function() { it('fails for a DOM element that lacks the expected class', function() {
const matcher = jasmineUnderTest.matchers.toHaveClass(), const matcher = jasmineUnderTest.matchers.toHaveClass(),
result = matcher.compare( result = matcher.compare(
this.domHelpers.createElementWithClassName(''), specHelpers.domHelpers.createElementWithClassName(''),
'foo' 'foo'
); );
@@ -15,7 +11,7 @@ describe('toHaveClass', function() {
it('passes for a DOM element that has the expected class', function() { it('passes for a DOM element that has the expected class', function() {
const matcher = jasmineUnderTest.matchers.toHaveClass(), const matcher = jasmineUnderTest.matchers.toHaveClass(),
el = this.domHelpers.createElementWithClassName('foo bar baz'); el = specHelpers.domHelpers.createElementWithClassName('foo bar baz');
expect(matcher.compare(el, 'foo').pass).toBe(true); expect(matcher.compare(el, 'foo').pass).toBe(true);
expect(matcher.compare(el, 'bar').pass).toBe(true); expect(matcher.compare(el, 'bar').pass).toBe(true);
@@ -24,7 +20,7 @@ describe('toHaveClass', function() {
it('fails for a DOM element that only has other classes', function() { it('fails for a DOM element that only has other classes', function() {
const matcher = jasmineUnderTest.matchers.toHaveClass(), const matcher = jasmineUnderTest.matchers.toHaveClass(),
el = this.domHelpers.createElementWithClassName('foo bar'); el = specHelpers.domHelpers.createElementWithClassName('foo bar');
expect(matcher.compare(el, 'fo').pass).toBe(false); expect(matcher.compare(el, 'fo').pass).toBe(false);
}); });
@@ -42,7 +38,7 @@ describe('toHaveClass', function() {
matcher.compare(undefined, 'foo'); matcher.compare(undefined, 'foo');
}).toThrowError('undefined is not a DOM element'); }).toThrowError('undefined is not a DOM element');
const textNode = this.domHelpers.document.createTextNode(''); const textNode = specHelpers.domHelpers.document.createTextNode('');
expect(function() { expect(function() {
matcher.compare(textNode, 'foo'); matcher.compare(textNode, 'foo');
}).toThrowError('HTMLNode is not a DOM element'); }).toThrowError('HTMLNode is not a DOM element');
+3 -3
View File
@@ -1,4 +1,4 @@
(function(env) { (function() {
function browserVersion(matchFn) { function browserVersion(matchFn) {
const userAgent = jasmine.getGlobal().navigator.userAgent; const userAgent = jasmine.getGlobal().navigator.userAgent;
if (!userAgent) { if (!userAgent) {
@@ -10,7 +10,7 @@
return match ? parseFloat(match[1]) : void 0; return match ? parseFloat(match[1]) : void 0;
} }
env.firefoxVersion = browserVersion(function(userAgent) { specHelpers.firefoxVersion = browserVersion(function(userAgent) {
return /Firefox\/([0-9]{0,})/.exec(userAgent); return /Firefox\/([0-9]{0,})/.exec(userAgent);
}); });
})(jasmine.getEnv()); })();
+17 -21
View File
@@ -1,24 +1,20 @@
(function(env) { (function() {
function domHelpers() { let doc;
let doc;
if (typeof document !== 'undefined') { if (typeof document !== 'undefined') {
doc = document; doc = document;
} else { } else {
const JSDOM = require('jsdom').JSDOM; const JSDOM = require('jsdom').JSDOM;
const dom = new JSDOM(); const dom = new JSDOM();
doc = dom.window.document; doc = dom.window.document;
}
return {
document: doc,
createElementWithClassName: function(className) {
const el = this.document.createElement('div');
el.className = className;
return el;
}
};
} }
env.domHelpers = domHelpers; specHelpers.domHelpers = {
})(jasmine.getEnv()); document: doc,
createElementWithClassName(className) {
const el = this.document.createElement('div');
el.className = className;
return el;
}
};
})();
+1
View File
@@ -0,0 +1 @@
globalThis.specHelpers = {};
+3 -3
View File
@@ -1,5 +1,5 @@
(function(env) { (function() {
env.registerIntegrationMatchers = function() { specHelpers.registerIntegrationMatchers = function() {
jasmine.addMatchers({ jasmine.addMatchers({
toHaveFailedExpectationsForRunnable: function() { toHaveFailedExpectationsForRunnable: function() {
return { return {
@@ -51,4 +51,4 @@
} }
}); });
}; };
})(jasmine.getEnv()); })();
+1 -1
View File
@@ -22,7 +22,7 @@ describe('PrettyPrinter (HTML Dependent)', function() {
}); });
it("should print Firefox's wrapped native objects correctly", function() { it("should print Firefox's wrapped native objects correctly", function() {
if (jasmine.getEnv().firefoxVersion) { if (specHelpers.firefoxVersion) {
const pp = jasmineUnderTest.makePrettyPrinter(); const pp = jasmineUnderTest.makePrettyPrinter();
let err; let err;
try { try {
+1
View File
@@ -18,6 +18,7 @@ module.exports = {
specDir: 'spec', specDir: 'spec',
specFiles: ['**/*[Ss]pec.js', '!npmPackage/**/*'], specFiles: ['**/*[Ss]pec.js', '!npmPackage/**/*'],
helpers: [ helpers: [
'helpers/init.js',
'helpers/generator.js', 'helpers/generator.js',
'helpers/BrowserFlags.js', 'helpers/BrowserFlags.js',
'helpers/domHelpers.js', 'helpers/domHelpers.js',
+1
View File
@@ -5,6 +5,7 @@
"npmPackage/**/*[Ss]pec.js" "npmPackage/**/*[Ss]pec.js"
], ],
"helpers": [ "helpers": [
"helpers/init.js",
"helpers/domHelpers.js", "helpers/domHelpers.js",
"helpers/integrationMatchers.js", "helpers/integrationMatchers.js",
"helpers/overrideConsoleLogForCircleCi.js", "helpers/overrideConsoleLogForCircleCi.js",