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 all 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.9",
"@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
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
31 changes: 15 additions & 16 deletions packages/kbn-pm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{
"extends": "../../tsconfig.json",
"exclude": [
"dist"
],
"include": [
"./src/**/*.ts",
"./types/index.d.ts"
],
"compilerOptions": {
"types": [
"jest",
"node"
]
}
}
{
"extends": "../../tsconfig.json",
"exclude": [
"dist"
],
"include": [
"./src/**/*.ts"
],
"compilerOptions": {
"types": [
"jest",
"node"
]
}
}
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
45 changes: 30 additions & 15 deletions src/core/public/core_system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,90 +27,105 @@ import { LegacyPlatformService } from './legacy';
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', () => ({
LegacyPlatformService: MockLegacyPlatformService,
}));

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

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

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

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

jest.mock('./notifications', () => ({
NotificationsService: MockNotificationsService,
}));

const mockHttp = {};
const MockHttpService = jest.fn<HttpService>(function _MockNotificationsService(this: any) {
const MockHttpService = jest.fn<HttpService, any>(function _MockNotificationsService(this: any) {
this.start = jest.fn().mockReturnValue(mockHttp);
this.stop = jest.fn();
return this;
});
jest.mock('./http', () => ({
HttpService: MockHttpService,
}));

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

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

const mockChromeStart = {};
const MockChromeService = jest.fn<ChromeService>(function _MockNotificationsService(this: any) {
const MockChromeService = jest.fn<ChromeService, any>(function _MockNotificationsService(
this: any
) {
this.start = jest.fn().mockReturnValue(mockChromeStart);
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: ht
],
"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();
Copy link
Contributor Author

@mshustov mshustov Feb 26, 2019

Choose a reason for hiding this comment

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

seems this is an incorrect way to restore mocks. this code is unreachable if assertion fails. am I right? should I create another issue to fix all such cases? @joshdover
right now only 2 files have the problem
https://github.com/elastic/kibana/blob/126797b8f11eb15152910a494b6bee8b73025c1c/src/core/server/http/https_redirect_server.test.ts
https://github.com/elastic/kibana/blob/master/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.test.ts

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that's a good catch. Could push the mock into an array that gets cleaned up in an afterEach block. Up to you whether or not you want to fix this now or separately.

});

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/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