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 hapi upgrade #25723

Merged
merged 2 commits into from
Nov 15, 2018
Merged

Fix hapi upgrade #25723

merged 2 commits into from
Nov 15, 2018

Conversation

jasonrhodes
Copy link
Member

@jasonrhodes jasonrhodes commented Nov 15, 2018

Closes #24844

Summary

This fixes the Hapi problem outlined in the #24844 ticket. I chose to use throw over return to be explicit about re-throwing the error.

I also added tests for all of our routes to make sure we throw that error correctly and don't blow things up in the client by failing to do that. The tests are a bit overkill, but it seemed good to have a failsafe there.

# from the x-pack directory
$ node scripts/jest --watch routes/__test__/routeFailures

screen shot 2018-11-15 at 9 46 26 am


beforeEach(() => {
consoleError = window.console.error;
window.console.error = jest.fn();
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be global instead of window (there is no window object in Node)

Copy link
Member

Choose a reason for hiding this comment

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

Afaik the mock will be cleaned up automatically if you use jest.spyOn:

jest.spyOn(global.console, 'error')

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm good point, although it works 🤔 probably because jsdom env running in jest?

Copy link
Member

Choose a reason for hiding this comment

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

ah, that might be. Can you check if global works too? That seems more "right".

Copy link
Member

Choose a reason for hiding this comment

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

This should work:

jest.spyOn(global.console, 'error').mockReturnValue(undefined);

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure if spies can have their return values changed? But even if they can, the damage console.error does is a side effect and not its return value, so this won't help -- the method has to be replaced.

I'll switch it to global though if that still works! 👍

Copy link
Member

Choose a reason for hiding this comment

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

undefined is the return value of console.log(). It's just a shorthand for this:

jest.spyOn(global.console, 'error').mockImplementation(() => undefined)

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, but console.error already returns undefined all on its own right? It's the side effect of printing to stdout that I'm trying to suppress, and changing its return value won't do that.

Copy link
Member

@sorenlouv sorenlouv Nov 15, 2018

Choose a reason for hiding this comment

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

Both of these will suppress the side effect:

jest.spyOn(global.console, 'error').mockImplementation(() => undefined)
 # and
jest.spyOn(global.console, 'error').mockReturnValue(undefined);

Simply doing this won't:

jest.spyOn(global.console, 'error')

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok looked into this and TIL you can stub a function in jest using spyOn ... did not expect mockImplementation that to work like that!

Copy link
Member

@sorenlouv sorenlouv left a comment

Choose a reason for hiding this comment

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

LGTM. Good idea with a test!

return;
}

throw new Error('Route succeeded when it should have failed');
Copy link
Member

@sorenlouv sorenlouv Nov 15, 2018

Choose a reason for hiding this comment

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

There is a built-in for this:

  expect(() => route[0].handler(mockReq)).toThrowError(BoomError);

There are different matchers. You might have to modify it a bit to make it work.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, unfortunately the ones I tried didn't work here because I want to test things about the rejected error. Where does BoomError come from in your example?

Copy link
Member

Choose a reason for hiding this comment

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

Where does BoomError come from in your example?

Fantasy land :) I just expected something like that to be available. But this works:

await expect(route[0].handler(mockReq)).rejects.toMatchObject({ isBoom: true });

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@jasonrhodes
Copy link
Member Author

@sqren just pushed changes to those tests based on your feedback, great ideas! 👏

@sorenlouv
Copy link
Member

Great! LGTM

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@jasonrhodes jasonrhodes merged commit f926df8 into elastic:master Nov 15, 2018
@jasonrhodes jasonrhodes deleted the fix-hapi-upgrade branch November 15, 2018 21:01
jasonrhodes added a commit to jasonrhodes/kibana that referenced this pull request Nov 15, 2018
* Fixes consistent error handling in routes, adds tests for error handling

* Upgrades to failure tests per review
jasonrhodes added a commit that referenced this pull request Nov 16, 2018
* Fixes consistent error handling in routes, adds tests for error handling

* Upgrades to failure tests per review
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.

[APM] Hapi upgrade is broken for transaction endpoint
3 participants