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 10 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
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
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
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 @@ -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
2 changes: 1 addition & 1 deletion src/legacy/ui/public/utils/subscribe_with_scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ it('subscribes to the passed observable, returns subscription', () => {
const $scope = new Scope();

const unsubSpy = jest.fn();
const subSpy = jest.fn(() => unsubSpy);
const subSpy = jest.fn<any, any>(() => unsubSpy);
const observable = new Rx.Observable(subSpy);

const subscription = subscribeWithScope($scope as any, observable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('EditorConfigProvider', () => {
});

it('should call registered providers with given parameters', () => {
const provider = jest.fn(() => ({}));
const provider = jest.fn<any, any>(() => ({}));
registry.register(provider);
expect(provider).not.toHaveBeenCalled();
const aggType = {};
Expand All @@ -51,8 +51,8 @@ describe('EditorConfigProvider', () => {
});

it('should call all registered providers with given parameters', () => {
const provider = jest.fn(() => ({}));
const provider2 = jest.fn(() => ({}));
const provider = jest.fn<any, any>(() => ({}));
const provider2 = jest.fn<any, any>(() => ({}));
registry.register(provider);
registry.register(provider2);
expect(provider).not.toHaveBeenCalled();
Expand Down
9 changes: 6 additions & 3 deletions x-pack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@types/expect.js": "^0.3.29",
"@types/graphql": "^0.13.1",
"@types/history": "^4.6.2",
"@types/jest": "^23.3.1",
"@types/jest": "^24.0.6",
Copy link
Contributor Author

@mshustov mshustov Feb 24, 2019

Choose a reason for hiding this comment

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

@epixa why we need to have another package.json for x-pack?

"@types/joi": "^13.4.2",
"@types/jsonwebtoken": "^7.2.7",
"@types/lodash": "^3.10.1",
Expand Down Expand Up @@ -99,8 +99,8 @@
"gulp-mocha": "2.2.0",
"gulp-multi-process": "^1.3.1",
"hapi": "^17.5.3",
"jest": "^23.6.0",
"jest-cli": "^23.6.0",
"jest": "^24.1.0",
"jest-cli": "^24.1.0",
"jest-styled-components": "^6.2.2",
"jsdom": "^12.0.0",
"mocha": "3.3.0",
Expand Down Expand Up @@ -147,6 +147,7 @@
"@scant/router": "^0.1.0",
"@slack/client": "^4.8.0",
"@turf/boolean-contains": "6.0.1",
"@types/jest": "^24.0.6",
"@types/json-stable-stringify": "^1.0.32",
"angular-resource": "1.4.9",
"angular-sanitize": "1.6.5",
Expand Down Expand Up @@ -202,6 +203,8 @@
"inline-style": "^2.0.0",
"intl": "^1.2.5",
"io-ts": "^1.4.2",
"jest": "^24.1.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

why packages are duplicated in "devDependencies" and "dependencies"?

Copy link
Member

Choose a reason for hiding this comment

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

Probably a mistake. I would be surprised if we wanted to ship jest in the production build to users.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sometimes it happens. Contentful did it to run their functional tests 😅

"jest-cli": "^24.1.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/issue: why do we need jest-cli in non-dev dependencies?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

seems like a mistake when updated version via yarn cli. will fix now

"joi": "^13.5.2",
"jquery": "^3.3.1",
"json-stable-stringify": "^1.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('createErrorGroupWatch', () => {
let tmpl: any;
beforeEach(async () => {
chrome.getInjected = jest.fn().mockReturnValue('myIndexPattern');
jest.spyOn(uuid, 'v4').mockReturnValue('mocked-uuid');
jest.spyOn(uuid, 'v4').mockReturnValue(new Buffer('mocked-uuid'));
jest.spyOn(rest, 'createWatch').mockReturnValue(undefined);

createWatchResponse = await createErrorGroupWatch({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('PropertiesTable', () => {
it('should render null empty string when no docs are returned', () => {
jest
.spyOn(agentDocs, 'getAgentFeatureDocsUrl')
.mockImplementation(() => null);
.mockImplementation(() => undefined);

expect(
shallow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('transactionGroupsFetcher', () => {
end: 1528977600000,
client: clientSpy,
config: {
get: jest.fn((key: string) => {
get: jest.fn<any, string[]>((key: string) => {
switch (key) {
case 'apm_oss.transactionIndices':
return 'myIndex';
Expand Down
Loading