Skip to content

Commit

Permalink
fix: refactor zone patch test method
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion committed Mar 13, 2020
1 parent 78b96a3 commit 2778218
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 60 deletions.
28 changes: 28 additions & 0 deletions example/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,34 @@ 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}
` as any)(
'it.each should work with table as a tagged template literal with done',
({ foo, bar }, done) => {
expect(foo).toBe(1);
expect(bar).toBe(2);
done();
}
);

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');
74 changes: 14 additions & 60 deletions src/zone-patch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,51 +44,27 @@ function wrapDescribeInZone(describeBody) {
// Create a proxy zone in which to run `test` blocks so that the tests function
// can retroactively install different zones.
const testProxyZone = ambientZone.fork(new ProxyZoneSpec());
function wrapTestInZone(testBody, eachArgs) {
function wrapTestInZone(testBody) {
if (testBody === undefined) {
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,
enumerable: false
});
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;
}

/**
Expand All @@ -108,29 +84,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);
};
};
Expand Down

0 comments on commit 2778218

Please sign in to comment.