Move new test helpers to spec/helpers.js
authorRaphael von der Grün <raphinesse@gmail.com>
Tue, 15 May 2018 00:04:21 +0000 (02:04 +0200)
committerRaphael von der Grün <raphinesse@gmail.com>
Tue, 15 May 2018 22:16:12 +0000 (00:16 +0200)
spec/create.spec.js
spec/helpers.js

index bab1a90..f9fc2c1 100644 (file)
@@ -21,58 +21,18 @@ var fs = require('fs');
 var path = require('path');
 
 var shell = require('shelljs');
-var rewire = require('rewire');
 var requireFresh = require('import-fresh');
 
-var create = rewire('..');
-var helpers = require('./helpers');
+var create = require('..');
 var events = require('cordova-common').events;
 var CordovaError = require('cordova-common').CordovaError;
 var ConfigParser = require('cordova-common').ConfigParser;
-var CordovaLogger = require('cordova-common').CordovaLogger;
+const {tmpDir, createWith, createWithMockFetch, expectRejection} = require('./helpers');
 
-var tmpDir = helpers.tmpDir('create_test');
 var appName = 'TestBase';
 var appId = 'org.testing';
 var project = path.join(tmpDir, appName);
 
-// Disable regular console output during tests
-CordovaLogger.get().setLevel(CordovaLogger.ERROR);
-
-function createWith (rewiring) {
-    return (...args) => create.__with__(rewiring)(() => create(...args));
-}
-
-// Calls create with mocked fetch to not depend on the outside world
-function createWithMockFetch (dir, id, name, cfg, events) {
-    const mockFetchDest = path.join(tmpDir, 'mockFetchDest');
-    const templateDir = path.dirname(require.resolve('cordova-app-hello-world'));
-    const fetchSpy = jasmine.createSpy('fetchSpy')
-        .and.callFake(() => Promise.resolve(mockFetchDest));
-
-    shell.cp('-R', templateDir, mockFetchDest);
-    return createWith({fetch: fetchSpy})(dir, id, name, cfg, events)
-        .then(() => fetchSpy);
-}
-
-// Expect promise to get rejected with a reason matching expectedReason
-function expectRejection (promise, expectedReason) {
-    return promise.then(
-        () => fail('Expected promise to be rejected'),
-        reason => {
-            if (expectedReason instanceof Error) {
-                expect(reason instanceof expectedReason.constructor).toBeTruthy();
-                expect(reason.message).toContain(expectedReason.message);
-            } else if (typeof expectedReason === 'function') {
-                expect(expectedReason(reason)).toBeTruthy();
-            } else if (expectedReason !== undefined) {
-                expect(reason).toBe(expectedReason);
-            } else {
-                expect().nothing();
-            }
-        });
-}
-
 // Setup and teardown test dirs
 beforeEach(function () {
     shell.rm('-rf', project);
index a04ebc9..b1f7948 100644 (file)
     under the License.
 */
 
-var path = require('path');
-var fs = require('fs');
-var shell = require('shelljs');
-var os = require('os');
-
-module.exports.tmpDir = function (subdir) {
-    var dir = path.join(os.tmpdir(), 'e2e-test');
-    if (subdir) {
-        dir = path.join(dir, subdir);
-    }
-    if (fs.existsSync(dir)) {
-        shell.rm('-rf', dir);
-    }
-    shell.mkdir('-p', dir);
-    return dir;
+const fs = require('fs');
+const os = require('os');
+const path = require('path');
+
+const rewire = require('rewire');
+const shell = require('shelljs');
+
+// Disable regular console output during tests
+const CordovaLogger = require('cordova-common').CordovaLogger;
+CordovaLogger.get().setLevel(CordovaLogger.ERROR);
+
+// Temporary directory to use for all tests
+const tmpDir = path.join(os.tmpdir(), 'e2e-test', 'create_test');
+
+// Returns a version of create with its local scope rewired
+const create = rewire('..');
+function createWith (rewiring) {
+    return (...args) => create.__with__(rewiring)(() => create(...args));
+}
+
+// Calls create with mocked fetch to not depend on the outside world
+function createWithMockFetch (dir, id, name, cfg, events) {
+    const mockFetchDest = path.join(tmpDir, 'mockFetchDest');
+    const templateDir = path.dirname(require.resolve('cordova-app-hello-world'));
+    const fetchSpy = jasmine.createSpy('fetchSpy')
+        .and.callFake(() => Promise.resolve(mockFetchDest));
+
+    shell.cp('-R', templateDir, mockFetchDest);
+    return createWith({fetch: fetchSpy})(dir, id, name, cfg, events)
+        .then(() => fetchSpy);
+}
+
+// Expect promise to get rejected with a reason matching expectedReason
+function expectRejection (promise, expectedReason) {
+    return promise.then(
+        () => fail('Expected promise to be rejected'),
+        reason => {
+            if (expectedReason instanceof Error) {
+                expect(reason instanceof expectedReason.constructor).toBeTruthy();
+                expect(reason.message).toContain(expectedReason.message);
+            } else if (typeof expectedReason === 'function') {
+                expect(expectedReason(reason)).toBeTruthy();
+            } else if (expectedReason !== undefined) {
+                expect(reason).toBe(expectedReason);
+            } else {
+                expect().nothing();
+            }
+        });
+}
+
+module.exports = {
+    tmpDir,
+    createWith,
+    createWithMockFetch,
+    expectRejection
 };
 
 // Add the toExist matcher.