Skip to content

Commit

Permalink
[7.0] Update jest to v24 (#31825) (#32194)
Browse files Browse the repository at this point in the history
* Update jest to v24 (#31825)

* udpate jest, jest-cli, @types/jest to v24

* fix type error in kibana-i18n package

* return serivce explicitly to fix typings

* add explicit never

* suppress typings errors

* update jest versions in x-pack

* make tests in x-pack more robust and fix incompatibility

* suppress CallCluster mock typings

Mock interface doesn't match CallCluster. Requires
additional work

* x-pack. resolve other typing conflicts

* remove unused types/jest

* fix snapshots

* restore mocks after jest.spyOn

* remove outdated definitions for jest

* cleanup x-pack package.json and update @types/jest

* fix tests merged from master

* updated yarn.lock and log errors for scripts/type_check

* This commit fixes error in TS, which failed on parsing the file.

* suppress type errors from master

* jest-cli is devDep

* updated yarn.lock
  • Loading branch information
mshustov authored Feb 28, 2019
1 parent a499493 commit 3898aa1
Show file tree
Hide file tree
Showing 38 changed files with 842 additions and 762 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
"@types/has-ansi": "^3.0.0",
"@types/hoek": "^4.1.3",
"@types/humps": "^1.1.2",
"@types/jest": "^23.3.1",
"@types/jest": "^24.0.9",
"@types/joi": "^13.4.2",
"@types/jquery": "^3.3.6",
"@types/js-yaml": "^3.11.1",
Expand Down Expand Up @@ -360,8 +360,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
4 changes: 1 addition & 3 deletions packages/kbn-pm/src/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ test('respects both `include` and `exclude` filters if specified at the same tim
});

test('does not run command if all projects are filtered out', async () => {
const mockProcessExit = jest.spyOn(process, 'exit').mockImplementation(() => {
// noop
});
const mockProcessExit = jest.spyOn(process, 'exit').mockReturnValue(undefined as never);

await runCommand(command, {
...config,
Expand Down
1 change: 0 additions & 1 deletion packages/kbn-pm/types/index.d.ts

This file was deleted.

13 changes: 0 additions & 13 deletions packages/kbn-pm/types/jest/index.d.ts

This file was deleted.

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);

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",
"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
3 changes: 1 addition & 2 deletions src/core/server/http/https_redirect_server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ test('throws if [redirectHttpFromPort] is in use', async () => {
} as HttpConfig)
).rejects.toMatchSnapshot();

// Workaround for https://github.com/DefinitelyTyped/DefinitelyTyped/issues/17605.
(mockListen as any).mockRestore();
mockListen.mockRestore();
});

test('forwards http requests to https', async () => {
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",
"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
9 changes: 6 additions & 3 deletions src/core/server/logging/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ import { BaseLogger } from './logger';
const context = LoggingConfig.getLoggerContext(['context', 'parent', 'child']);
let appenderMocks: Appender[];
let logger: BaseLogger;

const timestamp = new Date(2012, 1, 1);
jest.spyOn(global, 'Date').mockImplementation(() => timestamp);

beforeEach(() => {
jest.spyOn<any, any>(global, 'Date').mockImplementation(() => timestamp);

appenderMocks = [{ append: jest.fn() }, { append: jest.fn() }];
logger = new BaseLogger(context, LogLevel.All, appenderMocks);
});

afterEach(() => {
jest.restoreAllMocks();
});

test('`trace()` correctly forms `LogRecord` and passes it to all appenders.', () => {
logger.trace('message-1');
for (const appenderMock of appenderMocks) {
Expand Down
15 changes: 8 additions & 7 deletions src/core/server/logging/logging_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@ jest.mock('fs', () => ({
}));

const timestamp = new Date(Date.UTC(2012, 1, 1));
const mockConsoleLog = jest.spyOn(global.console, 'log').mockImplementation(() => {
// noop
});
jest.spyOn(global, 'Date').mockImplementation(() => timestamp);
let mockConsoleLog: jest.SpyInstance;

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 '.';

let service: LoggingService;
beforeEach(() => (service = new LoggingService()));
beforeEach(() => {
mockConsoleLog = jest.spyOn(global.console, 'log').mockReturnValue(undefined);
jest.spyOn<any, any>(global, 'Date').mockImplementation(() => timestamp);
service = new LoggingService();
});

afterEach(() => {
mockConsoleLog.mockClear();
jest.restoreAllMocks();
mockCreateWriteStream.mockClear();
mockStreamWrite.mockClear();
});
Expand Down
Loading

0 comments on commit 3898aa1

Please sign in to comment.