diff --git a/jsunit/index.d.ts b/jsunit/index.d.ts index 10ec8c7aa7108b1f33c6233c5866a8909777ba00..410cdff292f4c6d26c914390fc0b9785f2dc447b 100644 --- a/jsunit/index.d.ts +++ b/jsunit/index.d.ts @@ -89,7 +89,7 @@ declare interface when { afterReturnNothing(): undefined afterAction(action: any): any afterThrow(e_msg: string): string - (argMatchers: any): when; + (argMatchers?: any): when; } export interface VerificationMode { @@ -103,6 +103,7 @@ export interface VerificationMode { export class MockKit { constructor() mockFunc(obj: Object, func: Function): Function + mockObject(obj: Object): Object verify(methodName: String, argsArray: Array): VerificationMode ignoreMock(obj: Object, func: Function): void clear(obj: Object): void diff --git a/jsunit/src/event.js b/jsunit/src/event.js index 28299c36697a833d0a3e7887d2f3439271b69888..bbd0e84bf3c843bf9a87066e28dc8101784ee8b5 100644 --- a/jsunit/src/event.js +++ b/jsunit/src/event.js @@ -24,15 +24,15 @@ class SpecEvent { this.eventMonitors.push(service); } - specStart() { + async specStart() { for (let monitor in this.eventMonitors) { - this.eventMonitors[monitor]['specStart'](); + await this.eventMonitors[monitor]['specStart'](); } } - specDone() { + async specDone() { for (const monitor in this.eventMonitors) { - this.eventMonitors[monitor]['specDone'](); + await this.eventMonitors[monitor]['specDone'](); } } } @@ -48,15 +48,15 @@ class SuiteEvent { this.eventMonitors.push(service); } - suiteStart() { + async suiteStart() { for (let monitor in this.eventMonitors) { - this.eventMonitors[monitor]['suiteStart'](); + await this.eventMonitors[monitor]['suiteStart'](); } } - suiteDone() { + async suiteDone() { for (let monitor in this.eventMonitors) { - this.eventMonitors[monitor]['suiteDone'](); + await this.eventMonitors[monitor]['suiteDone'](); } } } @@ -78,9 +78,9 @@ class TaskEvent { } } - taskDone() { + async taskDone() { for (let monitor in this.eventMonitors) { - this.eventMonitors[monitor]['taskDone'](); + await this.eventMonitors[monitor]['taskDone'](); } } diff --git a/jsunit/src/interface.js b/jsunit/src/interface.js index b4f145dccfa405f90d150a20abd8fd8d10539431..40398c849d55cd76ab64fcba26899b547e2a3c97 100644 --- a/jsunit/src/interface.js +++ b/jsunit/src/interface.js @@ -18,46 +18,25 @@ import Core from './core'; const core = Core.getInstance(); const describe = function (desc, func) { - if (typeof globalThis !== 'undefined') { - return globalThis.describe(desc, func); - } - return core.describe(desc, func); + return Reflect.has(core, 'describe') ? core.describe(desc, func) : (desc, func) => { }; }; const it = function (desc, filter, func) { - if (typeof globalThis !== 'undefined') { - return globalThis.it(desc, filter, func); - } - return core.it(desc, filter, func); + return Reflect.has(core, 'it') ? core.it(desc, filter, func) : (desc, filter, func) => { }; }; const beforeEach = function (func) { - if (typeof globalThis !== 'undefined') { - return globalThis.beforeEach(func); - } - return core.beforeEach(func); + return Reflect.has(core, 'beforeEach') ? core.beforeEach(func) : (func) => { }; }; const afterEach = function (func) { - if (typeof globalThis !== 'undefined') { - return globalThis.afterEach(func); - } - return core.afterEach(func); + return Reflect.has(core, 'afterEach') ? core.afterEach(func) : (func) => { }; }; const beforeAll = function (func) { - if (typeof globalThis !== 'undefined') { - return globalThis.beforeAll(func); - } - return core.beforeAll(func); + return Reflect.has(core, 'beforeAll') ? core.beforeAll(func) : (func) => { }; }; const afterAll = function (func) { - if (typeof globalThis !== 'undefined') { - return globalThis.afterAll(func); - } - return core.afterAll(func); + return Reflect.has(core, 'afterAll') ? core.afterAll(func) : (func) => { }; }; const expect = function (actualValue) { - if (typeof globalThis !== 'undefined') { - return globalThis.expect(actualValue); - } - return core.expect(actualValue); + return Reflect.has(core, 'expect') ? core.expect(actualValue) : (actualValue) => { }; }; export { diff --git a/jsunit/src/module/config/configService.js b/jsunit/src/module/config/configService.js index 6848519304d5fc50ff62066a24ff5d5c103a96f9..b9d38aa7d75e68f7c43208839ec0df2c90857efc 100644 --- a/jsunit/src/module/config/configService.js +++ b/jsunit/src/module/config/configService.js @@ -75,12 +75,11 @@ class ConfigService { } } - if (params.dryRun !== undefined && params.dryRun !== 'true' && params.dryRun !== 'false') { - this.filterValid.push('dryRun:' + params.dryRun); - } - - if (params.random !== undefined && params.random !== 'true' && params.random !== 'false') { - this.filterValid.push('random:' + params.random); + let paramKeys = ['dryRun', 'random']; + for (const key of paramKeys) { + if (paramKeys[key] !== undefined && paramKeys[key] !== 'true' && paramKeys[key] !== 'false') { + this.filterValid.push(`${key}:${paramKeys[key]}`); + } } let classes = params.class; diff --git a/jsunit/src/module/mock/MockKit.js b/jsunit/src/module/mock/MockKit.js index fa41f1154bcf85aef565b354ea231f797b456ef0..a23462472c023f0ef81a6a6d274f14f8649a8dc0 100644 --- a/jsunit/src/module/mock/MockKit.js +++ b/jsunit/src/module/mock/MockKit.js @@ -94,6 +94,9 @@ class MockKit { values = new Map(); } let key = params[0]; + if (typeof key == "undefined") { + key = "anonymous-mock-" + f.propName; + } let matcher = new ArgumentMatchers(); if (matcher.matcheStubKey(key)) { key = matcher.matcheStubKey(key); @@ -111,6 +114,9 @@ class MockKit { return undefined; } let retrunKet = params[0]; + if (typeof retrunKet == "undefined") { + retrunKet = "anonymous-mock-" + f.propName; + } let stubSetKey = this.currentSetKey; if (this.currentSetKey && (typeof (retrunKet) != "undefined")) { @@ -213,6 +219,21 @@ class MockKit { let a = this.recordCalls.get(methodName + '(' + argsArray.toString() + ')'); return new VerificationMode(a ? a : 0); } + + mockObject(object) { + if (!object || typeof object === "string") { + throw Error(`this ${object} cannot be mocked`); + } + const _this = this; + let mockedObject = {}; + let keys = Reflect.ownKeys(object); + keys.filter(key => (typeof Reflect.get(object, key)) === 'function') + .forEach(key => { + mockedObject[key] = object[key]; + mockedObject[key] = _this.mockFunc(mockedObject, mockedObject[key]); + }); + return mockedObject; + } } function ifMockedFunction(f) { diff --git a/jsunit/src/module/report/OhReport.js b/jsunit/src/module/report/OhReport.js index 3d3de218ef728a3dab571cef1a10f07c091856c5..689274778dd22b9aaa81810d9b4c88bce648ec8c 100644 --- a/jsunit/src/module/report/OhReport.js +++ b/jsunit/src/module/report/OhReport.js @@ -34,7 +34,7 @@ class OhReport { async taskDone() { this.taskDoneTime = new Date().getTime(); let summary = this.suiteService.getSummary(); - var message = '\n' + 'OHOS_REPORT_RESULT: stream=Tests run: ' + summary.total + ', Failure: ' + summary.failure; + let message = '\n' + 'OHOS_REPORT_RESULT: stream=Tests run: ' + summary.total + ', Failure: ' + summary.failure; message += ', Error: ' + summary.error; message += ', Pass: ' + summary.pass; message += '\n' + 'OHOS_REPORT_CODE: ' + (summary.failure > 0 ? -1 : 0) + '\n'; @@ -56,7 +56,7 @@ class OhReport { } async suiteStart() { - var message = '\n' + 'OHOS_REPORT_SUM: ' + this.suiteService.getCurrentRunningSuite().getSpecsNum(); + let message = '\n' + 'OHOS_REPORT_SUM: ' + this.suiteService.getCurrentRunningSuite().getSpecsNum(); message += '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description + '\n'; await this.delegator.print(message).then(() => { console.info(this.suiteService.getCurrentRunningSuite().description + ' print success'); @@ -64,7 +64,7 @@ class OhReport { } async suiteDone() { - var message = '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description; + let message = '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description; message += '\n' + 'OHOS_REPORT_STATUS: suiteconsuming=' + this.suiteService.getCurrentRunningSuite().duration + '\n'; await this.delegator.print(message).then(() => { console.info(this.suiteService.getCurrentRunningSuite().description + ' print success'); @@ -72,7 +72,7 @@ class OhReport { } async specStart() { - var message = '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description; + let message = '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description; message += '\n' + 'OHOS_REPORT_STATUS: current=' + (++this.index); message += '\n' + 'OHOS_REPORT_STATUS: id=JS'; message += '\n' + 'OHOS_REPORT_STATUS: numtests=' + this.suiteService.getSummary().total; @@ -85,7 +85,7 @@ class OhReport { } async specDone() { - var message = '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description; + let message = '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description; message += '\n' + 'OHOS_REPORT_STATUS: current=' + (this.index); message += '\n' + 'OHOS_REPORT_STATUS: id=JS'; message += '\n' + 'OHOS_REPORT_STATUS: numtests=' + this.suiteService.getSummary().total; diff --git a/jsunit/src/service.js b/jsunit/src/service.js index 5ace31d764b600de51303f83d587ea0c165b448b..c45b8b197f69e6442487d5a0ffec0c27fbd61588 100644 --- a/jsunit/src/service.js +++ b/jsunit/src/service.js @@ -195,12 +195,21 @@ class SuiteService { } execute() { - if (this.coreContext.getDefaultService('config').filterValid.length !== 0) { + const configService = this.coreContext.getDefaultService('config'); + if (configService.filterValid.length !== 0) { this.coreContext.fireEvents('task', 'incorrectFormat'); return; } + + if (configService.isRandom() && this.rootSuite.childSuites.length > 0) { + this.rootSuite.childSuites.sort(function () { + return Math.random().toFixed(1) > 0.5 ? -1 : 1; + }); + this.currentRunningSuite = this.rootSuite.childSuites[0]; + } + this.coreContext.fireEvents('task', 'taskStart'); - if (this.coreContext.getDefaultService('config').isSupportAsync()) { + if (configService.isSupportAsync()) { let asyncExecute = async () => { await this.rootSuite.asyncRun(this.coreContext); }; @@ -298,7 +307,7 @@ SuiteService.Suite = class { suiteService.setCurrentRunningSuite(this); return new Promise(async resolve => { if (this.description !== '') { - coreContext.fireEvents('suite', 'suiteStart', this); + await coreContext.fireEvents('suite', 'suiteStart', this); } await this.runAsyncHookFunc('beforeAll'); if (this.specs.length > 0) { @@ -324,7 +333,7 @@ SuiteService.Suite = class { await this.runAsyncHookFunc('afterAll'); if (this.description !== '') { - coreContext.fireEvents('suite', 'suiteDone'); + await coreContext.fireEvents('suite', 'suiteDone'); } resolve(); }); @@ -464,7 +473,7 @@ SpecService.Spec = class { const config = coreContext.getDefaultService('config'); const timeout = + (config.timeout === undefined ? 5000 : config.timeout); return new Promise(async resolve => { - coreContext.fireEvents('spec', 'specStart', this); + await coreContext.fireEvents('spec', 'specStart', this); function timeoutPromise() { return new Promise(function (resolve, reject) { @@ -503,7 +512,7 @@ SpecService.Spec = class { this.error = e; } } - coreContext.fireEvents('spec', 'specDone', this); + await coreContext.fireEvents('spec', 'specDone', this); resolve(); }); }