realm-js/tests/js/index.js

146 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-02-18 19:59:34 +00:00
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
2015-10-02 21:57:54 +00:00
'use strict';
2015-10-02 22:15:25 +00:00
const Realm = require('realm');
2017-10-16 18:47:35 +00:00
if( typeof Realm.Sync !== 'undefined' && Realm.Sync !== null ) {
global.WARNING = "global is not available in React Native. Use it only in tests";
global.enableSyncTests = true;
}
2017-10-12 14:06:24 +00:00
const isNodeProcess = typeof process === 'object' && process + '' === '[object process]';
2017-11-15 10:59:13 +00:00
const require_method = require;
function node_require(module) { return require_method(module); }
2017-10-12 14:06:24 +00:00
if (isNodeProcess && process.platform === 'win32') {
global.enableSyncTests = false;
}
// catching segfaults during testing can help debugging
if (isNodeProcess) {
const SegfaultHandler = node_require('segfault-handler');
SegfaultHandler.registerHandler("crash.log");
}
var TESTS = {
2017-10-13 12:39:28 +00:00
ListTests: require('./list-tests'),
LinkingObjectsTests: require('./linkingobjects-tests'),
ObjectTests: require('./object-tests'),
RealmTests: require('./realm-tests'),
ResultsTests: require('./results-tests'),
QueryTests: require('./query-tests'),
2017-10-13 12:39:28 +00:00
MigrationTests: require('./migration-tests'),
2017-09-26 13:18:14 +00:00
EncryptionTests: require('./encryption-tests'),
ObjectIDTests: require('./object-id-tests'),
// Garbagecollectiontests: require('./garbage-collection'),
ComputeSizeTests: require('./compute-size-tests'),
};
2015-10-15 01:52:55 +00:00
// If sync is enabled, run the sync tests
if (global.enableSyncTests) {
2017-10-13 12:39:28 +00:00
TESTS.UserTests = require('./user-tests');
TESTS.SessionTests = require('./session-tests');
// FIXME: Permission tests currently fail in react native
if (isNodeProcess) {
TESTS.PermissionTests = require('./permission-tests');
}
}
Add missing chrome debugging apis for Sync.User (#801) * Add user tests * Add Sync member to Realm * Add a simple test for addListener * Introduce browser User class * Introduce Sync.User proxy * Fix User.all * Add User.logout * Remove some console.log statements * Update rpc.cpp from tests-folder * Use user.current from user-methods, not cpp * remove user.current from two missing places * Start server in realmjs and react tests * Add user tests * Add Sync member to Realm * Add a simple test for addListener * Introduce browser User class * Introduce Sync.User proxy * Fix User.all * Add User.logout * Remove some console.log statements * Update rpc.cpp from tests-folder * Use user.current from user-methods, not cpp * remove user.current from two missing places * Start server in realmjs and react tests * Replace needle with node-fetch * Make AuthError work in the browser * Allow for persistent callbacks * Expose _authenticateRealm to native * Use clear_test_state in roc * Streamline test inclusion * Fix login tests * Promisify testLogin() * Disable AsyncTests for now * Mode jasmine timeout to spec/unit_tests.js * Avoid use of global for fetch module * Only include user-tests is sync is enabled * Skip ROS for android tests * Add some comments about persistent callbacks * Fix results-test * Run async-tests in node * Add a comment about (user object) * Cache all users in User.current and return promises in fetch * node_require async-tests so RN doesn't try and package them * Add missing parenthesis
2017-01-31 13:07:29 +00:00
// If on node, run the async tests
if (isNodeProcess && process.platform !== 'win32') {
TESTS.AsyncTests = node_require('./async-tests');
Add missing chrome debugging apis for Sync.User (#801) * Add user tests * Add Sync member to Realm * Add a simple test for addListener * Introduce browser User class * Introduce Sync.User proxy * Fix User.all * Add User.logout * Remove some console.log statements * Update rpc.cpp from tests-folder * Use user.current from user-methods, not cpp * remove user.current from two missing places * Start server in realmjs and react tests * Add user tests * Add Sync member to Realm * Add a simple test for addListener * Introduce browser User class * Introduce Sync.User proxy * Fix User.all * Add User.logout * Remove some console.log statements * Update rpc.cpp from tests-folder * Use user.current from user-methods, not cpp * remove user.current from two missing places * Start server in realmjs and react tests * Replace needle with node-fetch * Make AuthError work in the browser * Allow for persistent callbacks * Expose _authenticateRealm to native * Use clear_test_state in roc * Streamline test inclusion * Fix login tests * Promisify testLogin() * Disable AsyncTests for now * Mode jasmine timeout to spec/unit_tests.js * Avoid use of global for fetch module * Only include user-tests is sync is enabled * Skip ROS for android tests * Add some comments about persistent callbacks * Fix results-test * Run async-tests in node * Add a comment about (user object) * Cache all users in User.current and return promises in fetch * node_require async-tests so RN doesn't try and package them * Add missing parenthesis
2017-01-31 13:07:29 +00:00
}
var SPECIAL_METHODS = {
beforeEach: true,
afterEach: true,
};
exports.getTestNames = function() {
var testNames = {};
for (var suiteName in TESTS) {
var testSuite = TESTS[suiteName];
testNames[suiteName] = Object.keys(testSuite).filter(function(testName) {
return !(testName in SPECIAL_METHODS) && typeof testSuite[testName] == 'function';
});
}
return testNames;
};
exports.registerTests = function(tests) {
for (var suiteName in tests) {
TESTS[suiteName] = tests[suiteName];
}
};
exports.prepare = function(done) {
if (!global.enableSyncTests || !isNodeProcess || global.testAdminUserInfo) {
done();
return;
2017-07-07 07:30:24 +00:00
}
require('./admin-user-helper')
.createAdminUser()
.then(userInfo => {
global.testAdminUserInfo = userInfo;
done();
})
.catch(error => {
console.error("Error running admin-user-helper", error);
done.fail(error);
});
};
exports.runTest = function(suiteName, testName) {
var testSuite = TESTS[suiteName];
var testMethod = testSuite && testSuite[testName];
if (testMethod) {
// Start fresh in case of a crash in a previous run.
Realm.clearTestState();
console.warn("Starting test " + testName);
2016-02-18 19:19:13 +00:00
var promise;
try {
2016-02-18 19:19:13 +00:00
promise = testMethod.call(testSuite);
// If the test returns a promise, then clear state on success or failure.
if (promise) {
promise.then(
function() { Realm.clearTestState(); },
function() { Realm.clearTestState(); }
);
}
return promise;
} finally {
2016-02-18 19:19:13 +00:00
// Synchronously clear state if the test is not async.
if (!promise) {
Realm.clearTestState();
}
}
} else if (!testSuite || !(testName in SPECIAL_METHODS)) {
throw new Error('Missing test: ' + suiteName + '.' + testName);
}
}