Skip to content

Commit

Permalink
FIX: testApp compatibility with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
melsomino committed Jul 23, 2020
1 parent 5177ff2 commit da0db28
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 15 deletions.
4 changes: 1 addition & 3 deletions testApp/prepare-suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function rewriteRunScript() {

const giverKeysPath = path.resolve(os.homedir(), 'giverKeys.json');
const giverKeys = fs.existsSync(giverKeysPath)
? JSON.parse(fs.readFileSync(giverKeysPath, {encoding:'utf8'}))
? JSON.parse(fs.readFileSync(giverKeysPath, { encoding: 'utf8' }))
: null;
const assets = [
`export default {`,
Expand Down Expand Up @@ -198,5 +198,3 @@ function removeTgzFiles() {
fs.writeFileSync(path.resolve(__dirname, 'package.json'), packageJson);
}
})();


9 changes: 5 additions & 4 deletions testApp/suite/_/run.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as j from 'jest-lite';
import {tests} from './init-tests';
import { BigInteger as bigInt } from 'javascript-biginteger';
import { tests } from './init-tests';

//IMPORTS
import aggregations_suite from '../aggregations.js';
Expand All @@ -13,6 +13,7 @@ import run_local_suite from '../run-local.js';
import test_error_messages_suite from '../test-error-messages.js';
//IMPORTS


function errorToJson(error) {
const json = {};
Object.entries(error).forEach(([key, value]) => {
Expand Down Expand Up @@ -40,7 +41,7 @@ export async function startTests(onStateChange) {
};
onStateChange(state);

j.addEventHandler((event) => {
jest.addEventHandler((event) => {
if (event.name === 'test_start') {
console.log(`[TEST_START] ${JSON.stringify({
name: event.test.name,
Expand All @@ -63,7 +64,7 @@ export async function startTests(onStateChange) {
onStateChange(state);
});
onStateChange(state);
j.run().then((results) => {
jest.run().then((results) => {
results.forEach((result) => {
result.errors = result.errors.map((e) => {
return e.toString().replace(/\n\s+at\s+.*/gi, '')
Expand Down
108 changes: 100 additions & 8 deletions testApp/suite/_/testing-platform.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {initTONClient as initTC} from 'ton-client-web-js';
import * as j from 'jest-lite';
import {Buffer as buffer} from 'buffer';
import { initTONClient as initTC } from 'ton-client-web-js';
import * as jestLite from 'jest-lite';
import { Buffer as buffer } from 'buffer';
import * as p1 from 'error-polyfill';
// import * as p2 from 'react-native-console-time-polyfill';
import {BigInteger as bigInt} from 'javascript-biginteger';
import { BigInteger as bigInt } from 'javascript-biginteger';
import assets from './assets';

if (bigInt && !global.BigInt) {
Expand All @@ -13,10 +13,15 @@ if (buffer && !global.Buffer) {
global.Buffer = buffer;
}

global.jest = j;
global.jest = jestLite.jest;
['test', 'expect', 'afterAll', 'afterEach', 'beforeAll', 'beforeEach'].forEach((name) => {
global[name] = j[name];
})
jestLite.jest[name] = jestLite[name];
global[name] = jestLite[name];
});

['addEventHandler', 'run'].forEach((name) => {
jestLite.jest[name] = jestLite[name];
});

jest.test.each = variants => (title, fn) => variants.forEach((v) => {
jest.test(title.replace(/%i/g, v), () => fn(v));
Expand All @@ -27,7 +32,94 @@ jest.setTimeout = (timeout) => {
global[testTimeoutSymbol] = timeout;
};

j.setTimeout(200000);
jestLite.jest.setTimeout(200000);

function isBigInt(a) {
return typeof a === 'bigint' || a instanceof BigInt;
}

function compareBigInt(a, b) {
const bigA = BigInt(a);
const bigB = BigInt(b);
if (typeof bigA !== 'bigint') {
return bigA.compare(bigB);
}
if (bigA < bigB) {
return -1;
}
if (bigA > bigB) {
return 1;
}
return 0;
}

const bigIntMatchers = {
toBeGreaterThan(received, other) {
return {
pass: compareBigInt(received, other) > 0,
message: () => `${received} must be greater than ${other}`,
};
},
toBeGreaterThanOrEqual(received, other) {
return {
pass: compareBigInt(received, other) >= 0,
message: () => `${received} must be greater than or equal to ${other}`,
};
},
toBeLessThan(received, other) {
return {
pass: compareBigInt(received, other) < 0,
message: () => `${received} must be less than ${other}`,
}
},
toBeLessThanOrEqual(received, other) {
return {
pass: compareBigInt(received, other) <= 0,
message: () => `${received} must be less than or equal to ${other}`,
}
},
toEqual(received, other) {
return {
pass: compareBigInt(received, other) === 0,
message: () => `${received} must be equal to ${other}`,
}
},
// toBe: null,
// toBeCloseTo: null,
// toBeDefined: null,
// toBeFalsy: null,
// toBeInstanceOf: null,
// toBeNaN: null,
// toBeNull: null,
// toBeTruthy: null,
// toBeUndefined: null,
// toContain: null,
// toContainEqual: null,
// toHaveLength: null,
// toHaveProperty: null,
// toMatch: null,
// toMatchObject: null,
// lastCalledWith: null,
// toBeCalled: null,
// toBeCalledWith: null,
// toHaveBeenCalled: null,
// toHaveBeenCalledTimes: null,
// toHaveBeenCalledWith: null,
// toHaveBeenLastCalledWith: null,
// toThrow: null,
// toThrowError: null,
};
const defaultExpect = jestLite.jest.expect;
global.expect = jestLite.jest.expect = (received) => {
const wrapped = defaultExpect(received);
if (isBigInt(received)) {
Object.entries(bigIntMatchers).forEach(([name, fn]) => {
wrapped[name] = (...args) => fn(received, ...args);
})
}
return wrapped;
}


// platform

Expand Down

0 comments on commit da0db28

Please sign in to comment.