[tests] new test infra - wip

This commit is contained in:
Salakar 2018-03-24 07:09:45 +00:00
parent 92cdeac2bf
commit 966c808aa3
4 changed files with 44 additions and 31 deletions

View File

@ -3,8 +3,10 @@ const http = require('http');
const invariant = require('assert'); const invariant = require('assert');
const { createContext, Script } = require('vm'); const { createContext, Script } = require('vm');
const ws = require('./ws'); const ws = require('./ws');
const { merge } = require('deeps');
global.context = null; global.bridge.context = null;
global.__coverage__ = {};
let scriptCached = null; let scriptCached = null;
// this is a dummy file path - without a file name the source map is not used in the vm // this is a dummy file path - without a file name the source map is not used in the vm
@ -135,33 +137,36 @@ process.on('ws-message', request => {
// console.log(request.method); // console.log(request.method);
switch (request.method) { switch (request.method) {
case 'prepareJSRuntime': case 'prepareJSRuntime':
if (global.context) { if (global.bridge.context) {
// todo __coverage__ not working - mostly empty statements in output
merge(global.__coverage__, global.bridge.context.__coverage__ || {});
try { try {
for (const name in global.context.__fbBatchedBridge) { for (const name in global.bridge.context.__fbBatchedBridge) {
global.context.__fbBatchedBridge[name] = undefined; global.bridge.context.__fbBatchedBridge[name] = undefined;
delete global.context.__fbBatchedBridge[name]; delete global.bridge.context.__fbBatchedBridge[name];
} }
for (const name in global.context.__fbGenNativeModule) { for (const name in global.bridge.context.__fbGenNativeModule) {
global.context.__fbGenNativeModule[name] = undefined; global.bridge.context.__fbGenNativeModule[name] = undefined;
delete global.context.__fbGenNativeModule[name]; delete global.bridge.context.__fbGenNativeModule[name];
} }
for (const name in global.context.__fbBatchedBridgeConfig) { for (const name in global.bridge.context.__fbBatchedBridgeConfig) {
global.context.__fbBatchedBridgeConfig[name] = undefined; global.bridge.context.__fbBatchedBridgeConfig[name] = undefined;
delete global.context.__fbBatchedBridgeConfig[name]; delete global.bridge.context.__fbBatchedBridgeConfig[name];
} }
for (const name in global.context) { for (const name in global.bridge.context) {
global.context[name] = undefined; global.bridge.context[name] = undefined;
delete global.context[name]; delete global.bridge.context[name];
} }
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }
} }
global.context = undefined; global.bridge.context = undefined;
global.context = createContext({ global.bridge.context = createContext({
console: consoleShim(), console: consoleShim(),
__bridgeNode: { __bridgeNode: {
ready() { ready() {
@ -184,12 +189,6 @@ process.on('ws-message', request => {
global.bridge.root = rootComponent; global.bridge.root = rootComponent;
}, },
}, },
get __coverage__() {
return global.__coverage__;
},
set __coverage__(val) {
return (global.__coverage__ = val);
},
}); });
sendResult(request.id); sendResult(request.id);
return; return;
@ -210,19 +209,19 @@ process.on('ws-message', request => {
return; return;
} }
if (global.context == null) { if (global.bridge.context == null) {
sendError('JS runtime not prepared'); sendError('JS runtime not prepared');
return; return;
} }
if (request.inject) { if (request.inject) {
for (const name in request.inject) { for (const name in request.inject) {
global.context[name] = JSON.parse(request.inject[name]); global.bridge.context[name] = JSON.parse(request.inject[name]);
} }
} }
try { try {
script.runInContext(global.context, TEMP_BUNDLE_PATH); script.runInContext(global.bridge.context, TEMP_BUNDLE_PATH);
} catch (e) { } catch (e) {
sendError(e); sendError(e);
} }
@ -235,13 +234,12 @@ process.on('ws-message', request => {
let returnValue = [[], [], [], 0]; let returnValue = [[], [], [], 0];
try { try {
if ( if (
global.context != null && global.bridge.context != null &&
typeof global.context.__fbBatchedBridge === 'object' typeof global.bridge.context.__fbBatchedBridge === 'object'
) { ) {
returnValue = global.context.__fbBatchedBridge[request.method].apply( returnValue = global.bridge.context.__fbBatchedBridge[
null, request.method
request.arguments ].apply(null, request.arguments);
);
} }
} catch (e) { } catch (e) {
if (request.method !== '$disconnected') { if (request.method !== '$disconnected') {

View File

@ -14,12 +14,21 @@ if (bridgeNode) {
} }
export default { export default {
/**
* Makes the main module to be tested accessible to nodejs
* @param moduleExports
*/
provideModule(moduleExports) { provideModule(moduleExports) {
if (bridgeNode) { if (bridgeNode) {
bridgeNode.provideModule(moduleExports); bridgeNode.provideModule(moduleExports);
bridgeNode.ready(); bridgeNode.ready();
} }
}, },
/**
* Makes the root component accessible to nodejs - e.g. bridge.root.setState({ ... });
* @param rootComponent
*/
provideRoot(rootComponent) { provideRoot(rootComponent) {
if (bridgeNode) { if (bridgeNode) {
bridgeNode.provideRoot(rootComponent); bridgeNode.provideRoot(rootComponent);

View File

@ -2330,6 +2330,11 @@
"integrity": "sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ=", "integrity": "sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ=",
"dev": true "dev": true
}, },
"deeps": {
"version": "1.4.4",
"resolved": "https://registry.npmjs.org/deeps/-/deeps-1.4.4.tgz",
"integrity": "sha1-CRJrycesX2DIJ+EciQYa6bVyqWA="
},
"define-properties": { "define-properties": {
"version": "1.1.2", "version": "1.1.2",
"resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.2.tgz", "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.2.tgz",

View File

@ -15,6 +15,7 @@
"dependencies": { "dependencies": {
"babel-preset-es2015-mod": "^6.6.0", "babel-preset-es2015-mod": "^6.6.0",
"babel-preset-es3": "^1.0.1", "babel-preset-es3": "^1.0.1",
"deeps": "^1.4.4",
"detox": "^7.2.0", "detox": "^7.2.0",
"mocha": "^4.0.1", "mocha": "^4.0.1",
"react": "^16.2.0", "react": "^16.2.0",