From 81b4d26b9087d7732bd54faf8b673c264f300ba6 Mon Sep 17 00:00:00 2001 From: David Humphrey Date: Sat, 17 May 2014 15:17:09 -0400 Subject: [PATCH] Finish test suite changes for node.js --- tests/lib/indexeddb.js | 18 +++++++++++------- tests/lib/test-utils.js | 6 ++++-- tests/lib/websql.js | 8 +++++--- tests/node-runner.js | 19 ++++++++++--------- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/tests/lib/indexeddb.js b/tests/lib/indexeddb.js index 7857ede..aeb467f 100644 --- a/tests/lib/indexeddb.js +++ b/tests/lib/indexeddb.js @@ -1,14 +1,18 @@ define(["Filer"], function(Filer) { - var indexedDB = window.indexedDB || - window.mozIndexedDB || - window.webkitIndexedDB || - window.msIndexedDB; + var indexedDB = (function(window) { + return window.indexedDB || + window.mozIndexedDB || + window.webkitIndexedDB || + window.msIndexedDB; + }(this)); var needsCleanup = []; - window.addEventListener('beforeunload', function() { - needsCleanup.forEach(function(f) { f(); }); - }); + if(typeof window !== 'undefined') { + window.addEventListener('beforeunload', function() { + needsCleanup.forEach(function(f) { f(); }); + }); + } function IndexedDBTestProvider(name) { var _done = false; diff --git a/tests/lib/test-utils.js b/tests/lib/test-utils.js index acea1e0..84d2f3b 100644 --- a/tests/lib/test-utils.js +++ b/tests/lib/test-utils.js @@ -38,7 +38,8 @@ function(Filer, IndexedDBTestProvider, WebSQLTestProvider, MemoryTestProvider) { // (e.g., ?filer-provider=IndexedDB). If not specified, we use // the Memory provider by default. See test/require-config.js // for definition of window.filerArgs. - var providerType = window.filerArgs && window.filerArgs.provider ? + var providerType = (typeof window !== 'undefined' && + window.filerArgs && window.filerArgs.provider) ? window.filerArgs.provider : 'Memory'; var name = uniqueName(); @@ -59,7 +60,8 @@ function(Filer, IndexedDBTestProvider, WebSQLTestProvider, MemoryTestProvider) { } // Allow passing FS flags on query string - var flags = window.filerArgs && window.filerArgs.flags ? + var flags = (typeof window !== 'undefined' && + window && window.filerArgs && window.filerArgs.flags) ? window.filerArgs.flags : 'FORMAT'; // Create a file system and wait for it to get setup diff --git a/tests/lib/websql.js b/tests/lib/websql.js index ac59498..35f363a 100644 --- a/tests/lib/websql.js +++ b/tests/lib/websql.js @@ -1,9 +1,11 @@ define(["Filer"], function(Filer) { var needsCleanup = []; - window.addEventListener('beforeunload', function() { - needsCleanup.forEach(function(f) { f(); }); - }); + if(typeof window !== 'undefined') { + window.addEventListener('beforeunload', function() { + needsCleanup.forEach(function(f) { f(); }); + }); + } function WebSQLTestProvider(name) { var _done = false; diff --git a/tests/node-runner.js b/tests/node-runner.js index 260f152..7b524f8 100644 --- a/tests/node-runner.js +++ b/tests/node-runner.js @@ -1,5 +1,12 @@ -var requirejs = require('requirejs'); +// If there's something broken in filer or a test, +// requirejs can blow up, and mocha sees it as tests +// not getting added (i.e., it just exists with only +// 1 test run). Display an error so it's clear what happened. +process.on('uncaughtException', function(err) { + console.error('Error in require.js trying to build test suite, filer:\n', err.stack); +}); +var requirejs = require('requirejs'); requirejs.config({ paths: { "tests": "../tests", @@ -22,16 +29,10 @@ requirejs.config({ nodeRequire: require }); -GLOBAL.document = {}; -GLOBAL.navigator = { userAgent: ""}; -GLOBAL.window = { - addEventListener: function(){}, - navigator: navigator, - document: document, - setTimeout: setTimeout -}; +// We use Chai's expect assertions in all the tests via a global GLOBAL.expect = require('chai').expect; +// Workaround for Mocha bug, see https://github.com/visionmedia/mocha/issues/362 describe("Mocha needs one test in order to wait on requirejs tests", function() { it('should wait for other tests', function(){ require('assert').ok(true);