Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: test.each should be in ProxyZone #340

Merged
merged 5 commits into from
Mar 10, 2020
Merged

Conversation

JiaLiPassion
Copy link
Contributor

@JiaLiPassion JiaLiPassion commented Jan 8, 2020

Close #339.

Fix test.each not in ProxyZone issue.
Also remove some not needed assignment.

And I will try to move this source into angular/zone.js repo, and also add support to timer methods such as jest.fakeTimers/jest.runAllTicks, after that jest-preset-angular can just import zone-testing bundle.

@thymikee , please help to check this one, thank you!

@wtho
Copy link
Collaborator

wtho commented Jan 20, 2020

Nice!
Looking forward to the zone-testing bundle getting more features!

Could you add a minimal test using test.each in the example-app (in this repo at /example)? That would be awesome, so we can also track the migration to zone-testing later on.

@thymikee you definitely have to have a look at this, as you authored the patch for zone.js back then.


Do you plan to migrate all patches from zone-patch.js to zone-testing, or would that create conflicts with karma?

@wtho wtho requested a review from thymikee January 20, 2020 14:02
@JiaLiPassion
Copy link
Contributor Author

@wtho, thanks for the review, sure I will add test under /example, and I have tested in another standalone app.
And I am working on the zone.js integration with jest in angular repo, after that, jest-preset-angular can just import zone-testing and care about the jest configuration only.

And thanks for the awesome work for patch zone for jest.

@JiaLiPassion
Copy link
Contributor Author

@thymikee, @wtho , I have added test cases under example, please review, thanks!

Copy link
Owner

@thymikee thymikee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, @wtho can you make sure the tests work as expected and nothing regress? I'd like to see a small describe.each test as well, since some code around it was removed

? () => testProxyZone.run(testBody, null)
: done => testProxyZone.run(testBody, null, [done]);
return function() {
return testProxyZone.run(testBody, null, arguments);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about return (...args) => testProxyZone.run(testBody, null, args)? Not that it matters too much, it's just my preference of not using arguments when possible

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I also think that is a good idea, will update it.

[2, { id: 2, name: 'Test Hero 2' }]
])('should call the GET hero api and return the result', (id: number, testData: any) => {
debugger;
console.log('id, testData', id, testData, (window as any).Zone.current.name);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove debug code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, forget to remove

@JiaLiPassion
Copy link
Contributor Author

@thymikee , I have updated the PR, also add test cases for describe.each, I already test all jest apis in another standalone apps.
Thanks!

@JiaLiPassion JiaLiPassion requested a review from thymikee January 22, 2020 07:20
@JiaLiPassion JiaLiPassion force-pushed the each branch 2 times, most recently from 2771812 to 68c42b1 Compare January 31, 2020 04:50
@tonivj5
Copy link

tonivj5 commented Feb 25, 2020

Any news? I have seen that angular/angular#35080 was merged 18 days ago 🎉

Copy link
Collaborator

@wtho wtho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!
Just a nit and we can merge.

example/src/app/simple/simple.component.spec.ts Outdated Show resolved Hide resolved
src/zone-patch/index.js Show resolved Hide resolved
@stefanmatar
Copy link

stefanmatar commented Feb 28, 2020

Can we merge this? @thymikee

@mehrad-rafigh
Copy link

Hey guys, any news on this? We are eagerly waiting :)

@wtho wtho merged commit 17dc5bf into thymikee:master Mar 10, 2020
@wtho
Copy link
Collaborator

wtho commented Mar 10, 2020

@mehrad-rafigh Sorry for the delay, it's out now.

JiaLiPassion added a commit to JiaLiPassion/jest-preset-angular that referenced this pull request Mar 11, 2020
in the pervious PR thymikee#340, zone.js add support to jest test.each methods, but it
introduces a bug, which is the `done()` function will not be handled correctly.

```
it('should work with done', done => {
  // done will be undefined.
});
```

The reason is the logic of monkey patching `test` method changed from

```
return testBody.length === 0
   ? () => testProxyZone.run(testBody, null)
   : done => testProxyZone.run(testBody, null, [done]);
```
to
```
 return function(...args) {
   return testProxyZone.run(testBody, null, args);
 };
```

the purpose of this change is to handle the following cases.

```
test.each([1, 2])('test.each', (arg1, arg2) => {
  expect(arg1).toBe(1);
  expect(arg2).toBe(2);
});
```

so in jest, it is a little complex, because the `testBody`'s parameter may be bigger than 1, so the
previous logic

```
return testBody.length === 0
   ? () => testProxyZone.run(testBody, null)
   : done => testProxyZone.run(testBody, null, [done]);
```
will not work for `test.each`.

So in this PR, I created a dynamic `Function` to return the correct length of paramters (which is required by jest core), to handle
1. normal `test` with or without `done`.
2. each with parameters with or without done.
JiaLiPassion added a commit to JiaLiPassion/jest-preset-angular that referenced this pull request Mar 11, 2020
in the pervious PR thymikee#340, zone.js add support to jest test.each methods, but it
introduces a bug, which is the `done()` function will not be handled correctly.

```
it('should work with done', done => {
  // done will be undefined.
});
```

The reason is the logic of monkey patching `test` method changed from

```
return testBody.length === 0
   ? () => testProxyZone.run(testBody, null)
   : done => testProxyZone.run(testBody, null, [done]);
```
to
```
 return function(...args) {
   return testProxyZone.run(testBody, null, args);
 };
```

the purpose of this change is to handle the following cases.

```
test.each([1, 2])('test.each', (arg1, arg2) => {
  expect(arg1).toBe(1);
  expect(arg2).toBe(2);
});
```

so in jest, it is a little complex, because the `testBody`'s parameter may be bigger than 1, so the
previous logic

```
return testBody.length === 0
   ? () => testProxyZone.run(testBody, null)
   : done => testProxyZone.run(testBody, null, [done]);
```
will not work for `test.each`.

So in this PR, I created a dynamic `Function` to return the correct length of paramters (which is required by jest core), to handle
1. normal `test` with or without `done`.
2. each with parameters with or without done.
JiaLiPassion added a commit to JiaLiPassion/jest-preset-angular that referenced this pull request Mar 11, 2020
in the pervious PR thymikee#340, zone.js add support to jest test.each methods, but it
introduces a bug, which is the `done()` function will not be handled correctly.

```
it('should work with done', done => {
  // done will be undefined.
});
```

The reason is the logic of monkey patching `test` method changed from

```
return testBody.length === 0
   ? () => testProxyZone.run(testBody, null)
   : done => testProxyZone.run(testBody, null, [done]);
```
to
```
 return function(...args) {
   return testProxyZone.run(testBody, null, args);
 };
```

the purpose of this change is to handle the following cases.

```
test.each([1, 2])('test.each', (arg1, arg2) => {
  expect(arg1).toBe(1);
  expect(arg2).toBe(2);
});
```

so in jest, it is a little complex, because the `testBody`'s parameter may be bigger than 1, so the
previous logic

```
return testBody.length === 0
   ? () => testProxyZone.run(testBody, null)
   : done => testProxyZone.run(testBody, null, [done]);
```
will not work for `test.each`.

So in this PR, I created a dynamic `Function` to return the correct length of paramters (which is required by jest core), to handle
1. normal `test` with or without `done`.
2. each with parameters with or without done.
JiaLiPassion added a commit to JiaLiPassion/jest-preset-angular that referenced this pull request Mar 11, 2020
in the pervious PR thymikee#340, zone.js add support to jest test.each methods, but it
introduces a bug, which is the `done()` function will not be handled correctly.

```
it('should work with done', done => {
  // done will be undefined.
});
```

The reason is the logic of monkey patching `test` method changed from

```
return testBody.length === 0
   ? () => testProxyZone.run(testBody, null)
   : done => testProxyZone.run(testBody, null, [done]);
```
to
```
 return function(...args) {
   return testProxyZone.run(testBody, null, args);
 };
```

the purpose of this change is to handle the following cases.

```
test.each([1, 2])('test.each', (arg1, arg2) => {
  expect(arg1).toBe(1);
  expect(arg2).toBe(2);
});
```

so in jest, it is a little complex, because the `testBody`'s parameter may be bigger than 1, so the
previous logic

```
return testBody.length === 0
   ? () => testProxyZone.run(testBody, null)
   : done => testProxyZone.run(testBody, null, [done]);
```
will not work for `test.each`.

So in this PR, I created a dynamic `Function` to return the correct length of paramters (which is required by jest core), to handle
1. normal `test` with or without `done`.
2. each with parameters with or without done.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't run it.each or test.each in fakeAsync zone
6 participants