Skip to content

Commit

Permalink
Call mochify with --no-detect-globals and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mantoni committed Dec 18, 2019
1 parent 359ceb4 commit b709b12
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 66 deletions.
3 changes: 2 additions & 1 deletion lib/sinon/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var timesInWords = require("./util/core/times-in-words");
var format = require("./util/core/format");
var stringSlice = require("@sinonjs/commons").prototypes.string.slice;

var globalContext = typeof global !== "undefined" ? global : window;
var arraySlice = arrayProto.slice;
var concat = arrayProto.concat;
var forEach = arrayProto.forEach;
Expand Down Expand Up @@ -60,7 +61,7 @@ function verifyIsValidAssertion(assertionMethod, assertionArgs) {
}

function failAssertion(object, msg) {
var obj = object || global;
var obj = object || globalContext;
var failMethod = obj.fail || assert.fail;
failMethod.call(obj, msg);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/sinon/util/fake-timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var extend = require("./core/extend");
var llx = require("lolex");
var globalContext = typeof global !== "undefined" ? global : window;

function createClock(config, globalCtx) {
var llxCtx = llx;
Expand All @@ -14,7 +15,7 @@ function createClock(config, globalCtx) {
}

function addIfDefined(obj, globalPropName) {
var globalProp = global[globalPropName];
var globalProp = globalContext[globalPropName];
if (typeof globalProp !== "undefined") {
obj[globalPropName] = globalProp;
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"scripts": {
"test-node": "mocha --recursive -R dot \"test/**/*-test.js\"",
"test-dev": "npm run test-node -- --watch -R min",
"test-headless": "mochify --recursive -R dot --grep WebWorker --invert --plugin [ proxyquire-universal ] \"test/**/*-test.js\"",
"test-headless": "mochify --no-detect-globals --recursive -R dot --grep WebWorker --invert --plugin [ proxyquire-universal ] \"test/**/*-test.js\"",
"test-coverage": "nyc npm run test-headless -- --transform [ babelify --ignore [ test ] --plugins [ babel-plugin-istanbul ] ]",
"test-cloud": "npm run test-headless -- --wd",
"test-webworker": "mochify --https-server 8080 --no-request-interception test/webworker/webworker-support-assessment.js",
"test-webworker": "mochify --no-detect-globals --https-server 8080 --no-request-interception test/webworker/webworker-support-assessment.js",
"test-esm": "mocha -r esm test/es2015/module-support-assessment-test.es6",
"test-esm-bundle": "node test/es2015/check-esm-bundle-is-runnable.js",
"test-docker-image": "docker-compose up",
Expand Down
4 changes: 2 additions & 2 deletions test/fake-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ describe("fake", function() {
sinon.assert.calledWith(callback, "one", "two", "three");

done();
}, 0);
}, 5); // IE 11 and Edge are sometimes slow
});

it("should call the last function argument", function(done) {
Expand All @@ -345,7 +345,7 @@ describe("fake", function() {
sinon.assert.calledOnce(callback);

done();
}, 0);
}, 5); // IE 11 and Edge are sometimes slow
});

it("should throw if the last argument is not a function", function() {
Expand Down
15 changes: 8 additions & 7 deletions test/issues/issues-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ var sinon = require("../../lib/sinon");
var createStub = require("../../lib/sinon/stub");
var assert = referee.assert;
var refute = referee.refute;
var globalXHR = global.XMLHttpRequest;
var globalAXO = global.ActiveXObject;
var globalContext = typeof global !== "undefined" ? global : window;
var globalXHR = globalContext.XMLHttpRequest;
var globalAXO = globalContext.ActiveXObject;

describe("issues", function() {
beforeEach(function() {
Expand Down Expand Up @@ -265,7 +266,7 @@ describe("issues", function() {
}

before(function() {
if (!global.Promise) {
if (typeof Promise === "undefined") {
this.skip();
}
});
Expand Down Expand Up @@ -515,11 +516,11 @@ describe("issues", function() {
var originalSetTimeout = setTimeout;

sinon.useFakeTimers();
sinon.spy(global, "setTimeout");
sinon.spy(globalContext, "setTimeout");

sinon.restore();

assert.same(originalSetTimeout, global.setTimeout, "fakeTimers restored");
assert.same(originalSetTimeout, globalContext.setTimeout, "fakeTimers restored");
});
});

Expand All @@ -528,8 +529,8 @@ describe("issues", function() {
sinon.useFakeXMLHttpRequest();
sinon.restore();

assert.same(global.XMLHttpRequest, globalXHR);
assert.same(global.ActiveXObject, globalAXO);
assert.same(globalContext.XMLHttpRequest, globalXHR);
assert.same(globalContext.ActiveXObject, globalAXO);
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/mock-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ describe("sinonMock", function() {
});

it("must set all expectations with mockPromise", function() {
if (!global.Promise) {
if (typeof Promise === "undefined") {
return this.skip();
}

Expand Down
2 changes: 1 addition & 1 deletion test/proxy-call-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ describe("sinonSpy.call", function() {
// https://github.com/sinonjs/sinon/issues/1066
/* eslint-disable consistent-return */
it("does not throw when the call stack is empty", function(done) {
if (!global.Promise) {
if (typeof Promise === "undefined") {
this.skip();
}

Expand Down
53 changes: 29 additions & 24 deletions test/sandbox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ var sinonAssert = require("../lib/sinon/assert");
var sinonClock = require("../lib/sinon/util/fake-timers");

var supportsAjax = typeof XMLHttpRequest !== "undefined" || typeof ActiveXObject !== "undefined";
var supportPromise = Boolean(global.Promise);
var globalXHR = global.XMLHttpRequest;
var globalAXO = global.ActiveXObject;
var supportPromise = typeof Promise !== "undefined";
var globalContext = typeof global !== "undefined" ? global : window;
var globalXHR = globalContext.XMLHttpRequest;
var globalAXO = globalContext.ActiveXObject;

if (!assert.stub) {
require("./test-helper");
Expand Down Expand Up @@ -473,25 +474,29 @@ describe("Sandbox", function() {
assert.equals(this.sandbox.stub(object, "method"), object.method);
});

if (typeof process !== "undefined") {
describe("on node", function() {
beforeEach(function() {
process.env.HELL = "Ain't too bad";
});
describe("on node", function() {
before(function() {
if (typeof process === "undefined" || !process.env) {
this.skip();
}
});

it("stubs environment property", function() {
var originalPrintWarning = deprecated.printWarning;
deprecated.printWarning = function() {
return;
};
beforeEach(function() {
process.env.HELL = "Ain't too bad";
});

it("stubs environment property", function() {
var originalPrintWarning = deprecated.printWarning;
deprecated.printWarning = function() {
return;
};

this.sandbox.stub(process.env, "HELL").value("froze over");
assert.equals(process.env.HELL, "froze over");
this.sandbox.stub(process.env, "HELL").value("froze over");
assert.equals(process.env.HELL, "froze over");

deprecated.printWarning = originalPrintWarning;
});
deprecated.printWarning = originalPrintWarning;
});
}
});
});

describe("stub anything", function() {
Expand Down Expand Up @@ -1399,11 +1404,11 @@ describe("Sandbox", function() {
var originalSetTimeout = setTimeout;

this.sandbox.useFakeTimers();
this.sandbox.spy(global, "setTimeout");
this.sandbox.spy(globalContext, "setTimeout");

this.sandbox.restore();

assert.same(originalSetTimeout, global.setTimeout, "fakeTimers restored");
assert.same(originalSetTimeout, globalContext.setTimeout, "fakeTimers restored");
});
});

Expand Down Expand Up @@ -1551,8 +1556,8 @@ describe("Sandbox", function() {
this.sandbox.useFakeXMLHttpRequest();
this.sandbox.restore();

assert.same(global.XMLHttpRequest, globalXHR);
assert.same(global.ActiveXObject, globalAXO);
assert.same(globalContext.XMLHttpRequest, globalXHR);
assert.same(globalContext.ActiveXObject, globalAXO);
});
});

Expand Down Expand Up @@ -1601,8 +1606,8 @@ describe("Sandbox", function() {
this.sandbox.useFakeServer();
this.sandbox.restore();

assert.same(global.XMLHttpRequest, globalXHR);
assert.same(global.ActiveXObject, globalAXO);
assert.same(globalContext.XMLHttpRequest, globalXHR);
assert.same(globalContext.ActiveXObject, globalAXO);
});
});
});
Expand Down
7 changes: 4 additions & 3 deletions test/spy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var referee = require("@sinonjs/referee");
var createSpy = require("../lib/sinon/spy");
var match = require("@sinonjs/samsam").createMatcher;
var globalContext = typeof global !== "undefined" ? global : window;
var assert = referee.assert;
var refute = referee.refute;

Expand Down Expand Up @@ -329,16 +330,16 @@ describe("spy", function() {

describe("global.Error", function() {
beforeEach(function() {
this.originalError = global.Error;
this.originalError = globalContext.Error;
});

afterEach(function() {
global.Error = this.originalError;
globalContext.Error = this.originalError;
});

it("creates a spy for Error", function() {
refute.exception(function() {
createSpy(global, "Error");
createSpy(globalContext, "Error");
});
});
});
Expand Down
42 changes: 36 additions & 6 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var match = require("@sinonjs/samsam").createMatcher;
var assert = referee.assert;
var refute = referee.refute;
var fail = referee.fail;
var Promise = require("native-promise-only"); // eslint-disable-line no-unused-vars
var globalContext = typeof global !== "undefined" ? global : window;

function verifyFunctionName(func, expectedName) {
var descriptor = Object.getOwnPropertyDescriptor(func, "name");
Expand Down Expand Up @@ -228,6 +228,12 @@ describe("stub", function() {
});

describe(".resolves", function() {
before(function() {
if (typeof Promise === "undefined") {
this.skip();
}
});

afterEach(function() {
if (Promise.resolve.restore) {
Promise.resolve.restore();
Expand Down Expand Up @@ -283,6 +289,12 @@ describe("stub", function() {
});

describe(".rejects", function() {
before(function() {
if (typeof Promise === "undefined") {
this.skip();
}
});

afterEach(function() {
if (Promise.reject.restore) {
Promise.reject.restore();
Expand Down Expand Up @@ -366,6 +378,12 @@ describe("stub", function() {
});

describe(".resolvesThis", function() {
before(function() {
if (typeof Promise === "undefined") {
this.skip();
}
});

afterEach(function() {
if (Promise.resolve.restore) {
Promise.resolve.restore();
Expand Down Expand Up @@ -432,6 +450,12 @@ describe("stub", function() {
});

describe(".resolvesArg", function() {
before(function() {
if (typeof Promise === "undefined") {
this.skip();
}
});

afterEach(function() {
if (Promise.resolve.restore) {
Promise.resolve.restore();
Expand Down Expand Up @@ -682,6 +706,12 @@ describe("stub", function() {
});

describe(".usingPromise", function() {
before(function() {
if (typeof Promise === "undefined") {
this.skip();
}
});

it("should exist and be a function", function() {
var stub = createStub();

Expand Down Expand Up @@ -804,15 +834,15 @@ describe("stub", function() {
describe("lazy instantiation of exceptions", function() {
var errorSpy;
beforeEach(function() {
this.originalError = global.Error;
errorSpy = createSpy(global, "Error");
this.originalError = globalContext.Error;
errorSpy = createSpy(globalContext, "Error");
// errorSpy starts with a call already made, not sure why
errorSpy.resetHistory();
});

afterEach(function() {
errorSpy.restore();
global.Error = this.originalError;
globalContext.Error = this.originalError;
});

it("uses a lazily created exception for the generic error", function() {
Expand Down Expand Up @@ -1267,8 +1297,8 @@ describe("stub", function() {
});

afterEach(function() {
if (global.console.info.restore) {
global.console.info.restore();
if (globalContext.console.info.restore) {
globalContext.console.info.restore();
}
});

Expand Down
Loading

0 comments on commit b709b12

Please sign in to comment.