From ab14e6bf95a0617fca056b5b3630525709841fc4 Mon Sep 17 00:00:00 2001 From: JiaLiPassion Date: Sat, 14 Mar 2020 00:03:06 +0900 Subject: [PATCH] fix: refactor zone patch test method --- example/src/app/app.component.spec.ts | 16 ++++++ src/zone-patch/index.js | 71 +++++---------------------- 2 files changed, 28 insertions(+), 59 deletions(-) diff --git a/example/src/app/app.component.spec.ts b/example/src/app/app.component.spec.ts index c2afc8410e..057b8612d2 100644 --- a/example/src/app/app.component.spec.ts +++ b/example/src/app/app.component.spec.ts @@ -108,6 +108,22 @@ describe('AppComponent', () => { expect(arg2).toBe(2); done(); }); + + it.each` + foo | bar + ${1} | ${2} + `('it.each should work with table as a tagged template literal', ({foo, bar}) => { + expect(foo).toBe(1); + expect(bar).toBe(2); + }); + + it.each` + foo | bar + ${1} | ${2} + `('(async) it.each should work with table as a tagged template literal', async ({foo, bar}) => { + expect(foo).toBe(1); + expect(bar).toBe(2); + }); }); test.todo('a sample todo'); diff --git a/src/zone-patch/index.js b/src/zone-patch/index.js index e5f6bfd0b7..cac85ff649 100644 --- a/src/zone-patch/index.js +++ b/src/zone-patch/index.js @@ -49,46 +49,21 @@ function wrapTestInZone(testBody, eachArgs) { return; } - if (!eachArgs || eachArgs.length === 0 || eachArgs[0].length === 0) { - // If we are not handling `test.each`, then the parameter of `testBody` - // will be 0 or 1, if it is 1, then we need to return a function with - // done parameter + const wrappedFunc = function() { + return testProxyZone.run(testBody, null, arguments); + }; + try { + Object.defineProperty(wrappedFunc, 'length', { + configurable: true, + writable: true + }); + wrappedFunc.length = testBody.length; + } catch (e) { return testBody.length === 0 ? () => testProxyZone.run(testBody, null) : done => testProxyZone.run(testBody, null, [done]); - } else { - // Dynamically create a Function to contain the same length - // of the parameters as the testBody - // For example: - // ``` - // test.each([[1, 2]])('test.each', (arg1, arg2) => {}); - // ``` - // In this case we need to return a function like this - // ``` - // return function(arg1, arg2) { - // return testProxyZone.run(testBody, null, [arg1, arg2]); - // } - // ``` - const len = eachArgs[0].length; - const args = []; - let argString = ''; - for (let i = 0; i < len; i++) { - args.push('arg' + i); - argString += 'arg' + i; - if (i !== len - 1) { - argString += ', '; - } - } - args.push('testBody'); - args.push('testProxyZone'); - if (len < testBody.length) { - args.push('done'); - argString += ', done'; - } - const funcBody = ` - return testProxyZone.run(testBody, null, [${argString}])`; - return new Function(args, funcBody); } + return wrappedFunc; } /** @@ -108,29 +83,7 @@ const bindDescribe = originalJestFn => const bindTest = originalJestFn => function(...eachArgs) { return function(...args) { - const testBody = args[1]; - if ( - testBody.length > 0 && - Array.isArray(eachArgs) && - eachArgs.length > 0 && - eachArgs[0].length > 0 - ) { - // check whether eachArgs is a 1D array - if (!Array.isArray(eachArgs[0][0])) { - // transfer eachArgs from 1D to 2D array - eachArgs = eachArgs.map(row => row.map(a => [a])); - } - } - args[1] = wrapTestInZone(args[1], ...eachArgs); - if (testBody.length > 0 || (eachArgs.length > 0 && eachArgs[0].length > 0)) { - eachArgs.forEach(row => { - const modifiedRow = row.map(a => { - a.push(testBody); - a.push(testProxyZone); - }); - return modifiedRow; - }); - } + args[1] = wrapTestInZone(args[1]); return originalJestFn.apply(this, eachArgs).apply(this, args); }; };