Skip to content

Commit

Permalink
[io.js] Update jsdom and get jest working on io.js
Browse files Browse the repository at this point in the history
This gets jest working on io.js 1.8.x and 2.0.x. io.js is reconciling with the Node Foundation so users who are still on Node are on the upgrade path that converges with io.js anyway. What this means is the jest version probably should be bumped to 0.5.0, where 0.4.x is a maintenance branch for legacy Node (typically with semver, a major version bump is required but 0.x.x releases are considered a free-for-all).

This diff is near the minimal amount of work to bring jest up to date and pass all of its tests.

 - Upgraded npm dependencies. The major update is jsdom from 0.10.3 to 5.4.3. This lets us drop the indirect contextify dependency.
 - Removed harmonize, since io.js uses modern V8 and enables stable ES6 features by default
 - Removed the `--harmony` flag from the TestRunner for the same reason (trivia: i added that line once upon a time, let me delete it)
 - Replaced jsdom's `run` with io.js's built-in `vm` module
 - Added support for curried arguments in the fake `nextTick` implementation, which io.js itself relies on
 - Updated the HasteModuleLoader to handle Node built-in modules before `node_modules` -- this fixes an issue I was getting with graceful-fs trying to require smalloc.
 - Patched coffeescript to use Array.isArray instead of `instanceof Array`. Working on getting that merged upstream.

Test Plan:
```
nvm install iojs-1
rm -fr node_modules && npm install

nvm install iojs-2
rm -fr node_modules && npm install
```
  • Loading branch information
ide committed May 23, 2015
1 parent 69229fb commit b6aa18b
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 58 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- "0.10"
- "iojs-v1"
- "iojs-v2"
sudo: false
3 changes: 0 additions & 3 deletions bin/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
'use strict';

var fs = require('fs');
var harmonize = require('harmonize');
var optimist = require('optimist');
var path = require('path');

Expand All @@ -36,8 +35,6 @@ function _wrapDesc(desc) {
}, ['']).join(indent);
}

harmonize();

var argv = optimist
.usage('Usage: $0 [--config=<pathToConfigFile>] [TestPathRegExp]')
.options({
Expand Down
34 changes: 16 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,30 @@
"version": "0.4.5",
"main": "src/jest.js",
"dependencies": {
"coffee-script": "^1.8.0",
"cover": "~0.2.8",
"diff": "~1.0.4",
"graceful-fs": "^2.0.3",
"harmonize": "1.4.2",
"istanbul": "^0.3.2",
"jasmine-only": "0.1.0",
"jasmine-pit": "~2.0.0",
"jsdom": "~0.10.3",
"coffee-script": "ide/coffeescript#array-check",
"cover": "^0.2.9",
"diff": "^1.4.0",
"graceful-fs": "^3.0.7",
"istanbul": "^0.3.14",
"jasmine-only": "^0.1.1",
"jasmine-pit": "^2.0.2",
"jsdom": "^5.4.2",
"node-haste": "^1.2.8",
"node-worker-pool": "~2.4.2",
"optimist": "~0.6.0",
"q": "~0.9.7",
"resolve": "~0.6.1",
"through": "^2.3.4",
"lodash.template": "^3.0.0"
"node-worker-pool": "^2.4.4",
"optimist": "^0.6.1",
"q": "^1.4.1",
"resolve": "^1.1.6",
"through": "^2.3.7"
},
"devDependencies": {
"jshint": "~2.5.0",
"node-find-files": "0.0.2"
"jshint": "^2.7.0",
"node-find-files": "0.0.4"
},
"bin": {
"jest": "./bin/jest.js"
},
"engines": {
"node": "0.8.x || 0.10.x"
"node": ">= 1"
},
"repository": {
"type": "git",
Expand Down
21 changes: 8 additions & 13 deletions src/HasteModuleLoader/HasteModuleLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,6 @@ function Loader(config, environment, resourceMap) {
this._configShouldMockModuleNames = {};

if (_configUnmockListRegExpCache === null) {
// Node must have been run with --harmony in order for WeakMap to be
// available prior to version 0.12
if (typeof WeakMap !== 'function') {
throw new Error(
'Please run node with the --harmony flag! jest requires WeakMap ' +
'which is only available with the --harmony flag in node < v0.12'
);
}

_configUnmockListRegExpCache = new WeakMap();
}

Expand Down Expand Up @@ -862,14 +853,14 @@ Loader.prototype.requireModule = function(currPath, moduleName,
modulePath = manualMockResource.path;
}

if (!modulePath) {
modulePath = this._moduleNameToPath(currPath, moduleName);
}

if (NODE_CORE_MODULES[moduleName]) {
return require(moduleName);
}

if (!modulePath) {
modulePath = this._moduleNameToPath(currPath, moduleName);
}

// Always natively require the jasmine runner.
if (modulePath.indexOf(VENDOR_PATH) === 0) {
return require(modulePath);
Expand Down Expand Up @@ -1037,6 +1028,10 @@ Loader.prototype.resetModuleRegistry = function() {
this._environment.fakeTimers.runAllTicks();
}.bind(this),

runAllImmediates: function() {
this._environment.fakeTimers.runAllImmediates();
}.bind(this),

runAllTimers: function() {
this._environment.fakeTimers.runAllTimers();
}.bind(this),
Expand Down
11 changes: 8 additions & 3 deletions src/JSDomEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

var FakeTimers = require('./lib/FakeTimers');

var vm = require('vm');

function _deepCopy(obj) {
var newObj = {};
var value;
Expand All @@ -29,7 +31,8 @@ function JSDomEnvironment(config) {
// use it (depending on the context -- such as TestRunner.js when operating as
// a workerpool parent), this is the best way to ensure we only spend time
// require()ing this when necessary.
this.global = require('./lib/jsdom-compat').jsdom().parentWindow;
this.global = require('./lib/jsdom-compat').jsdom().defaultView;
this.global.window = this.global;

// Node's error-message stack size is limited at 10, but it's pretty useful to
// see more than that when a test fails.
Expand All @@ -52,7 +55,6 @@ function JSDomEnvironment(config) {
this.global.Uint32Array = Uint32Array;
this.global.DataView = DataView;
this.global.Buffer = Buffer;
this.global.process = process;
this.global.setImmediate = setImmediate;
this.global.clearImmediate = clearImmediate;

Expand Down Expand Up @@ -105,7 +107,10 @@ JSDomEnvironment.prototype.dispose = function() {
};

JSDomEnvironment.prototype.runSourceText = function(sourceText, fileName) {
return this.global.run(sourceText, fileName);
var sandbox = vm.createContext(this.global);
return vm.runInContext(sourceText, sandbox, {
filename: fileName,
});
};

JSDomEnvironment.prototype.runWithRealTimers = function(cb) {
Expand Down
1 change: 0 additions & 1 deletion src/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,6 @@ TestRunner.prototype._createParallelTestRun = function(
this._opts.maxWorkers,
this._opts.nodePath,
this._opts.nodeArgv.concat([
'--harmony',
TEST_WORKER_PATH,
'--config=' + JSON.stringify(this._config)
])
Expand Down
8 changes: 1 addition & 7 deletions src/jasmineTestRunner/JasmineReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,7 @@ JasmineReporter.prototype._prettyPrint = function(obj, indent, cycleWeakMap) {
')';
}

/* jshint camelcase:false */
if (!cycleWeakMap) {
if (typeof WeakMap !== 'function') {
throw new Error(
'Please run node with the --harmony flag! jest requires WeakMap ' +
'which is only available with the --harmony flag in node < v0.12'
);
}
cycleWeakMap = new WeakMap();
}

Expand All @@ -231,6 +224,7 @@ JasmineReporter.prototype._prettyPrint = function(obj, indent, cycleWeakMap) {
this._prettyPrint(value, indent + keyIndent, cycleWeakMap)
);
}
/* jshint camelcase:false */
delete obj.__jstest_pp_cycle__;
return '{\n' + keysOutput.join(',\n') + '\n' + indent + '}';
} else {
Expand Down
9 changes: 0 additions & 9 deletions src/jasmineTestRunner/jasmineTestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,6 @@ function jasmineTestRunner(config, environment, moduleLoader, testPath) {
// Install jasmine-only
environment.runSourceText(jasmineOnlyContent);

// Node must have been run with --harmony in order for WeakMap to be
// available prior to version 0.12
if (typeof WeakMap !== 'function') {
throw new Error(
'Please run node with the --harmony flag! jest requires WeakMap ' +
'which is only available with the --harmony flag in node < v0.12'
);
}

// Mainline Jasmine sets __Jasmine_been_here_before__ on each object to
// detect cycles, but that doesn't work on frozen objects so we use a
// WeakMap instead.
Expand Down
14 changes: 11 additions & 3 deletions src/lib/FakeTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,26 @@ FakeTimers.prototype._fakeClearImmediate = function(uuid) {
};

FakeTimers.prototype._fakeNextTick = function(callback) {
var args = [];
for (var ii = 1, ll = arguments.length; ii < ll; ii++) {
args.push(arguments[ii]);
}

var uuid = this._uuidCounter++;

this._ticks.push({
uuid: uuid,
callback: callback
callback: function() {
return callback.apply(null, args);
}
});

var cancelledTicks = this._cancelledTicks;
this._originalTimerAPIs.nextTick(function() {
if (!cancelledTicks.hasOwnProperty(uuid)) {
// Callback may throw, so update the map prior calling.
cancelledTicks[uuid] = true;
callback();
callback.apply(null, args);
}
});
};
Expand All @@ -351,7 +359,7 @@ FakeTimers.prototype._fakeSetImmediate = function(callback) {
if (!cancelledImmediates.hasOwnProperty(uuid)) {
// Callback may throw, so update the map prior calling.
cancelledImmediates[uuid] = true;
callback();
callback.apply(null, args);
}
});

Expand Down

0 comments on commit b6aa18b

Please sign in to comment.