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

Update jest to v24 #31825

Merged
merged 22 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@
"@types/has-ansi": "^3.0.0",
Copy link
Member

Choose a reason for hiding this comment

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

question: how about updating babel-jest to 24.1.0 here and in x-pack?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

babel-jest@v24 uses babel@v7 which require @kbn/babel-preset/node_preset update. seems like quite a bit task

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will be done in #32326

"@types/hoek": "^4.1.3",
"@types/humps": "^1.1.2",
"@types/jest": "^23.3.1",
"@types/jest": "^24.0.6",
"@types/joi": "^13.4.2",
"@types/jquery": "^3.3.6",
"@types/js-yaml": "^3.11.1",
Expand Down Expand Up @@ -359,8 +359,8 @@
"intl-messageformat-parser": "^1.4.0",
"is-path-inside": "^2.0.0",
"istanbul-instrumenter-loader": "3.0.1",
"jest": "^23.6.0",
"jest-cli": "^23.6.0",
"jest": "^24.1.0",
"jest-cli": "^24.1.0",
"jest-raw-loader": "^1.0.1",
"jimp": "0.2.28",
"json-stable-stringify": "^1.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-i18n/src/core/i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ describe('I18n engine', () => {
});

describe('load', () => {
let mockFetch: jest.Mock<unknown>;
let mockFetch: jest.Mock;
beforeEach(() => {
mockFetch = jest.spyOn(global as any, 'fetch').mockImplementation();
});
Expand Down
1 change: 0 additions & 1 deletion packages/kbn-pm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"@types/globby": "^6.1.0",
"@types/has-ansi": "^3.0.0",
"@types/indent-string": "^3.0.0",
"@types/jest": "^23.3.1",
"@types/lodash.clonedeepwith": "^4.5.3",
"@types/log-symbols": "^2.0.0",
"@types/mkdirp": "^0.5.2",
Expand Down
18 changes: 5 additions & 13 deletions src/core/public/chrome/chrome_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,12 @@ describe('start', () => {
const startDeps = defaultStartDeps();
startDeps.injectedMetadata.getCspConfig.mockReturnValue({ warnLegacyBrowsers: true });
service.start(startDeps);
expect(startDeps.notifications.toasts.addWarning).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
"Your browser does not meet the security requirements for Kibana.",
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
expect(startDeps.notifications.toasts.addWarning.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"Your browser does not meet the security requirements for Kibana.",
],
}
]
`);
});

Expand Down
54 changes: 34 additions & 20 deletions src/core/public/core_system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,92 +27,106 @@ import { LoadingCountService } from './loading_count';
import { NotificationsService } from './notifications';
import { UiSettingsService } from './ui_settings';

const MockLegacyPlatformService = jest.fn<LegacyPlatformService>(
const MockLegacyPlatformService = jest.fn<LegacyPlatformService, any>(
function _MockLegacyPlatformService(this: any) {
this.start = jest.fn();
this.stop = jest.fn();
return this;
}
);

jest.mock('./legacy_platform', () => ({
LegacyPlatformService: MockLegacyPlatformService,
}));

const mockInjectedMetadataStartContract = {};
const MockInjectedMetadataService = jest.fn<InjectedMetadataService>(
const MockInjectedMetadataService = jest.fn<InjectedMetadataService, any>(
function _MockInjectedMetadataService(this: any) {
this.start = jest.fn().mockReturnValue(mockInjectedMetadataStartContract);
return this;
}
);
jest.mock('./injected_metadata', () => ({
InjectedMetadataService: MockInjectedMetadataService,
}));

const mockFatalErrorsStartContract = {};
const MockFatalErrorsService = jest.fn<FatalErrorsService>(function _MockFatalErrorsService(
const MockFatalErrorsService = jest.fn<FatalErrorsService, any>(function _MockFatalErrorsService(
this: any
) {
this.start = jest.fn().mockReturnValue(mockFatalErrorsStartContract);
this.add = jest.fn();
return this;
});
jest.mock('./fatal_errors', () => ({
FatalErrorsService: MockFatalErrorsService,
}));

const mockI18nStartContract = {};
const MockI18nService = jest.fn<I18nService>(function _MockI18nService(this: any) {
const MockI18nService = jest.fn<I18nService, any>(function _MockI18nService(this: any) {
this.start = jest.fn().mockReturnValue(mockI18nStartContract);
this.stop = jest.fn();
return this;
});
jest.mock('./i18n', () => ({
I18nService: MockI18nService,
}));

const mockNotificationStartContract = {};
const MockNotificationsService = jest.fn<NotificationsService>(function _MockNotificationsService(
this: any
) {
this.start = jest.fn().mockReturnValue(mockNotificationStartContract);
this.add = jest.fn();
this.stop = jest.fn();
});
const MockNotificationsService = jest.fn<NotificationsService, any>(
function _MockNotificationsService(this: any) {
this.start = jest.fn().mockReturnValue(mockNotificationStartContract);
this.add = jest.fn();
this.stop = jest.fn();
return this;
}
);
jest.mock('./notifications', () => ({
NotificationsService: MockNotificationsService,
}));

const mockLoadingCountContract = {};
const MockLoadingCountService = jest.fn<LoadingCountService>(function _MockNotificationsService(
this: any
) {
this.start = jest.fn().mockReturnValue(mockLoadingCountContract);
this.stop = jest.fn();
});
const MockLoadingCountService = jest.fn<LoadingCountService, any>(
function _MockNotificationsService(this: any) {
this.start = jest.fn().mockReturnValue(mockLoadingCountContract);
this.stop = jest.fn();
return this;
}
);
jest.mock('./loading_count', () => ({
LoadingCountService: MockLoadingCountService,
}));

const mockBasePathStartContract = {};
const MockBasePathService = jest.fn<BasePathService>(function _MockNotificationsService(this: any) {
const MockBasePathService = jest.fn<BasePathService, any>(function _MockNotificationsService(
this: any
) {
this.start = jest.fn().mockReturnValue(mockBasePathStartContract);
return this;
});
jest.mock('./base_path', () => ({
BasePathService: MockBasePathService,
}));

const mockUiSettingsContract = {};
const MockUiSettingsService = jest.fn<UiSettingsService>(function _MockNotificationsService(
const MockUiSettingsService = jest.fn<UiSettingsService, any>(function _MockNotificationsService(
this: any
) {
this.start = jest.fn().mockReturnValue(mockUiSettingsContract);
this.stop = jest.fn();
return this;
});
jest.mock('./ui_settings', () => ({
UiSettingsService: MockUiSettingsService,
}));

const mockChromeStartContract = {};
const MockChromeService = jest.fn<ChromeService>(function _MockNotificationsService(this: any) {
const MockChromeService = jest.fn<ChromeService, any>(function _MockNotificationsService(
this: any
) {
this.start = jest.fn().mockReturnValue(mockChromeStartContract);
this.stop = jest.fn();
return this;
});
jest.mock('./chrome', () => ({
ChromeService: MockChromeService,
Expand Down
2 changes: 1 addition & 1 deletion src/core/public/fatal_errors/fatal_errors_screen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('reloading', () => {

expect(locationReloadSpy).not.toHaveBeenCalled();
const [, handler] = addEventListenerSpy.mock.calls[0];
handler();
(handler as jest.Mock)();
expect(locationReloadSpy).toHaveBeenCalledTimes(1);
});
});
Expand Down
4 changes: 1 addition & 3 deletions src/core/public/fatal_errors/fatal_errors_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ describe('start.add()', () => {
it('exposes a function that passes its two arguments to fatalErrors.add()', () => {
const { fatalErrors, i18n } = setup();

jest.spyOn(fatalErrors, 'add').mockImplementation(() => {
/* noop */
});
jest.spyOn(fatalErrors, 'add').mockImplementation(() => undefined as never);
joshdover marked this conversation as resolved.
Show resolved Hide resolved

expect(fatalErrors.add).not.toHaveBeenCalled();
const { add } = fatalErrors.start({ i18n });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exports[`#start constructs UiSettingsClient and UiSettingsApi: UiSettingsApi arg
],
"results": Array [
Object {
"isThrow": false,
"type": "return",
Copy link
Contributor

Choose a reason for hiding this comment

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

These snapshots look like they're testing more jest internals rather than just the call arguments themselves. Can we fix these too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mocked functions are inside JS objects that serialised for snapshot testing. I didn't find a cheap way to fix it and decided to keep it as is.
As an alternative solution, we can define custom serialiser for mocked functions https://jestjs.io/docs/en/configuration#snapshotserializers-array-string

Copy link
Contributor

Choose a reason for hiding this comment

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

Perfectly good reason.

"value": undefined,
},
],
Expand All @@ -31,7 +31,7 @@ exports[`#start constructs UiSettingsClient and UiSettingsApi: UiSettingsClient
],
"results": Array [
Object {
"isThrow": false,
"type": "return",
"value": Object {
"loadingCountObservable": true,
},
Expand All @@ -52,7 +52,7 @@ exports[`#start constructs UiSettingsClient and UiSettingsApi: UiSettingsClient
],
"results": Array [
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
],
Expand All @@ -70,7 +70,7 @@ exports[`#start passes the uiSettings loading count to the loading count api: lo
],
"results": Array [
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
],
Expand Down
2 changes: 1 addition & 1 deletion src/core/public/ui_settings/ui_settings_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function mockClass<T>(
Class: { new (...args: any[]): T },
setup: (instance: any, args: any[]) => void
) {
const MockClass = jest.fn<T>(function(this: any, ...args: any[]) {
const MockClass = jest.fn(function(this: any, ...args: any[]) {
setup(this, args);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Array [
],
"results": Array [
Object {
"isThrow": false,
"type": "return",
Copy link
Contributor

Choose a reason for hiding this comment

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

Jest internals here as well, though this may be a bit hard to fix since it's an entire object. I'm not sure why we're testing such large snapshots like this though.

"value": undefined,
},
],
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/legacy_compat/legacy_platform_proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ test('correctly redirects server events.', () => {
([serverEventName]) => serverEventName === eventName
)!;

serverListener(1, 2, 3, 4);
serverListener(5, 6, 7, 8);
(serverListener as jest.Mock)(1, 2, 3, 4);
(serverListener as jest.Mock)(5, 6, 7, 8);

expect(listener).toHaveBeenCalledTimes(2);
expect(listener).toHaveBeenCalledWith(1, 2, 3, 4);
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/logging/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let appenderMocks: Appender[];
let logger: BaseLogger;

const timestamp = new Date(2012, 1, 1);
jest.spyOn(global, 'Date').mockImplementation(() => timestamp);
jest.spyOn<any, any>(global, 'Date').mockImplementation(() => timestamp);
Copy link
Contributor

Choose a reason for hiding this comment

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

This spy doesn't ever seem to be restored with mockRestore. We should make sure that happens explicitly after these tests run since we don't have the restoreMocks config option set to true.

As an aside: it'd be awesome if there was linter rule we could add for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 will fix. don't know why but I was sure we use restoreMocks: true


beforeEach(() => {
appenderMocks = [{ append: jest.fn() }, { append: jest.fn() }];
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/logging/logging_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ const timestamp = new Date(Date.UTC(2012, 1, 1));
const mockConsoleLog = jest.spyOn(global.console, 'log').mockImplementation(() => {
// noop
});
jest.spyOn(global, 'Date').mockImplementation(() => timestamp);
jest.spyOn<any, any>(global, 'Date').mockImplementation(() => timestamp);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above about mockRestore


import { createWriteStream } from 'fs';
const mockCreateWriteStream = createWriteStream as jest.Mock<typeof createWriteStream>;
const mockCreateWriteStream = (createWriteStream as unknown) as jest.Mock<typeof createWriteStream>;

import { LoggingConfig, LoggingService } from '.';

Expand Down
8 changes: 4 additions & 4 deletions src/core/server/root/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ import { logger } from '../logging/__mocks__';
const env = new Env('.', getEnvOptions());
const config$ = new BehaviorSubject({} as Config);

const mockProcessExit = jest.spyOn(global.process, 'exit').mockImplementation(() => {
// noop
});
const mockProcessExit = jest
.spyOn(global.process, 'exit')
.mockImplementation(() => undefined as never);
Copy link
Contributor

Choose a reason for hiding this comment

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

Also missing a restore.


const mockConsoleError = jest.spyOn(console, 'error').mockImplementation(() => {
// noop
Expand Down Expand Up @@ -178,7 +178,7 @@ test('fails and stops services if initial logger upgrade fails', async () => {

test('stops services if consequent logger upgrade fails', async () => {
const onShutdown = new BehaviorSubject<string | null>(null);
const mockOnShutdown = jest.fn(() => {
const mockOnShutdown = jest.fn<any, any>(() => {
onShutdown.next('completed');
onShutdown.complete();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports[`SourceFiltersTable should add a filter 1`] = `
],
"results": Array [
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
],
Expand Down Expand Up @@ -114,7 +114,7 @@ exports[`SourceFiltersTable should remove a filter 1`] = `
],
"results": Array [
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
],
Expand Down Expand Up @@ -319,7 +319,7 @@ exports[`SourceFiltersTable should update a filter 1`] = `
],
"results": Array [
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
* under the License.
*/

import { SavedObject } from '../../../../../server/saved_objects/service/saved_objects_client';
import {
SavedObject,
SavedObjectsClient,
} from '../../../../../server/saved_objects/service/saved_objects_client';
import { collectReferencesDeep } from './collect_references_deep';

const data = [
Expand Down Expand Up @@ -100,7 +103,7 @@ const data = [
];

test('collects dashboard and all dependencies', async () => {
const savedObjectClient = {
const savedObjectClient = ({
errors: {} as any,
create: jest.fn(),
bulkCreate: jest.fn(),
Expand All @@ -115,7 +118,7 @@ test('collects dashboard and all dependencies', async () => {
),
};
}),
};
} as unknown) as SavedObjectsClient;
const objects = await collectReferencesDeep(savedObjectClient, [{ type: 'dashboard', id: '1' }]);
expect(objects).toMatchInlineSnapshot(`
Array [
Expand Down
Loading