Skip to content

Commit

Permalink
Fix jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed Mar 12, 2020
1 parent 2651f01 commit b3101e9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/legacy/ui/public/timefilter/setup_router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
import { registerTimefilterWithGlobalState } from './setup_router';

jest.mock('ui/utils/subscribe_with_scope', () => ({
jest.mock('../../../../plugins/kibana_legacy/public', () => ({
subscribeWithScope: jest.fn(),
}));

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
* specific language governing permissions and limitations
* under the License.
*/
import { mockFatalError } from './subscribe_with_scope.test.mocks';

import * as Rx from 'rxjs';
import { subscribeWithScope } from './subscribe_with_scope';

Expand Down Expand Up @@ -73,14 +71,20 @@ it('calls observer.next() if already in a digest cycle, wraps in $scope.$apply i
});

it('reports fatalError if observer.next() throws', () => {
const fatalError = jest.fn();
const $scope = new Scope();
subscribeWithScope($scope as any, Rx.of(undefined), {
next() {
throw new Error('foo bar');
subscribeWithScope(
$scope as any,
Rx.of(undefined),
{
next() {
throw new Error('foo bar');
},
},
});
fatalError
);

expect(mockFatalError.mock.calls).toMatchInlineSnapshot(`
expect(fatalError.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
[Error: foo bar],
Expand All @@ -90,12 +94,13 @@ Array [
});

it('reports fatal error if observer.error is not defined and observable errors', () => {
const fatalError = jest.fn();
const $scope = new Scope();
const error = new Error('foo');
error.stack = `${error.message}\n---stack trace ---`;
subscribeWithScope($scope as any, Rx.throwError(error));
subscribeWithScope($scope as any, Rx.throwError(error), undefined, fatalError);

expect(mockFatalError.mock.calls).toMatchInlineSnapshot(`
expect(fatalError.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
[Error: Uncaught error in subscribeWithScope(): foo
Expand All @@ -106,14 +111,20 @@ Array [
});

it('reports fatal error if observer.error throws', () => {
const fatalError = jest.fn();
const $scope = new Scope();
subscribeWithScope($scope as any, Rx.throwError(new Error('foo')), {
error: () => {
throw new Error('foo');
subscribeWithScope(
$scope as any,
Rx.throwError(new Error('foo')),
{
error: () => {
throw new Error('foo');
},
},
});
fatalError
);

expect(mockFatalError.mock.calls).toMatchInlineSnapshot(`
expect(fatalError.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
[Error: foo],
Expand All @@ -123,25 +134,37 @@ Array [
});

it('does not report fatal error if observer.error handles the error', () => {
const fatalError = jest.fn();
const $scope = new Scope();
subscribeWithScope($scope as any, Rx.throwError(new Error('foo')), {
error: () => {
// noop, swallow error
subscribeWithScope(
$scope as any,
Rx.throwError(new Error('foo')),
{
error: () => {
// noop, swallow error
},
},
});
fatalError
);

expect(mockFatalError.mock.calls).toEqual([]);
expect(fatalError.mock.calls).toEqual([]);
});

it('reports fatal error if observer.complete throws', () => {
const fatalError = jest.fn();
const $scope = new Scope();
subscribeWithScope($scope as any, Rx.EMPTY, {
complete: () => {
throw new Error('foo');
subscribeWithScope(
$scope as any,
Rx.EMPTY,
{
complete: () => {
throw new Error('foo');
},
},
});
fatalError
);

expect(mockFatalError.mock.calls).toMatchInlineSnapshot(`
expect(fatalError.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
[Error: foo],
Expand Down

0 comments on commit b3101e9

Please sign in to comment.