diff --git a/spec/helper.js b/spec/helper.js index c134f339ab..03ddff9743 100644 --- a/spec/helper.js +++ b/spec/helper.js @@ -8,6 +8,7 @@ var express = require('express'); var facebook = require('../src/authDataManager/facebook'); var ParseServer = require('../src/index').ParseServer; var path = require('path'); +var TestUtils = require('../src/index').TestUtils; var databaseURI = process.env.DATABASE_URI; var cloudMain = process.env.CLOUD_CODE_MAIN || './spec/cloud/main.js'; @@ -88,7 +89,7 @@ beforeEach(function(done) { afterEach(function(done) { Parse.User.logOut().then(() => { - return clearData(); + return TestUtils.destroyAllDataPermanently(); }).then(() => { done(); }, (error) => { @@ -232,14 +233,6 @@ function mockFacebook() { return facebook; } -function clearData() { - var promises = []; - for (var conn in DatabaseAdapter.dbConnections) { - promises.push(DatabaseAdapter.dbConnections[conn].deleteEverything()); - } - return Promise.all(promises); -} - // This is polluting, but, it makes it way easier to directly port old tests. global.Parse = Parse; global.TestObject = TestObject; diff --git a/src/DatabaseAdapter.js b/src/DatabaseAdapter.js index 51403ba3cf..3afdf1364e 100644 --- a/src/DatabaseAdapter.js +++ b/src/DatabaseAdapter.js @@ -49,6 +49,18 @@ function clearDatabaseSettings() { appDatabaseOptions = {}; } +//Used by tests +function destroyAllDataPermanently() { + if (process.env.TESTING) { + var promises = []; + for (var conn in dbConnections) { + promises.push(dbConnections[conn].deleteEverything()); + } + return Promise.all(promises); + } + throw 'Only supported in test environment'; +} + function getDatabaseConnection(appId: string, collectionPrefix: string) { if (dbConnections[appId]) { return dbConnections[appId]; @@ -71,5 +83,6 @@ module.exports = { setAppDatabaseOptions: setAppDatabaseOptions, setAppDatabaseURI: setAppDatabaseURI, clearDatabaseSettings: clearDatabaseSettings, + destroyAllDataPermanently: destroyAllDataPermanently, defaultDatabaseURI: databaseURI }; diff --git a/src/TestUtils.js b/src/TestUtils.js new file mode 100644 index 0000000000..ebdb9f9914 --- /dev/null +++ b/src/TestUtils.js @@ -0,0 +1,15 @@ +import { destroyAllDataPermanently } from './DatabaseAdapter'; + +let unsupported = function() { + throw 'Only supported in test environment'; +}; + +let _destroyAllDataPermanently; +if (process.env.TESTING) { + _destroyAllDataPermanently = destroyAllDataPermanently; +} else { + _destroyAllDataPermanently = unsupported; +} + +export default { + destroyAllDataPermanently: _destroyAllDataPermanently}; diff --git a/src/index.js b/src/index.js index 7c45345468..39075d50e1 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import winston from 'winston'; import ParseServer from './ParseServer'; import S3Adapter from 'parse-server-s3-adapter' import FileSystemAdapter from 'parse-server-fs-adapter' +import TestUtils from './TestUtils'; import { useExternal } from './deprecated' // Factory function @@ -15,4 +16,4 @@ _ParseServer.createLiveQueryServer = ParseServer.createLiveQueryServer; let GCSAdapter = useExternal('GCSAdapter', 'parse-server-gcs-adapter'); export default ParseServer; -export { S3Adapter, GCSAdapter, FileSystemAdapter, _ParseServer as ParseServer }; +export { S3Adapter, GCSAdapter, FileSystemAdapter, TestUtils, _ParseServer as ParseServer };