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

Improve jest mocking infrastructure #38760

Merged
merged 18 commits into from
Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions packages/eslint-config-kibana/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
{
files: [
'**/*.{test,test.mocks,mock}.{js,ts,tsx}',
'**/__mocks__/**/*.{js,ts,tsx}',
Copy link
Contributor Author

@timroes timroes Jun 12, 2019

Choose a reason for hiding this comment

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

ℹ️ This way we make eslint aware of jest in all __mocks__ directories.

],
plugins: [
'jest',
Expand Down
3 changes: 3 additions & 0 deletions src/dev/jest/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export default {
'<rootDir>/src/dev/jest/setup/polyfills.js',
'<rootDir>/src/dev/jest/setup/enzyme.js',
],
setupFilesAfterEnv: [
'<rootDir>/src/dev/jest/setup/mocks.js',
],
coverageDirectory: '<rootDir>/target/jest-coverage',
coverageReporters: [
'html',
Expand Down
48 changes: 48 additions & 0 deletions src/dev/jest/setup/mocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/* eslint-env jest */

/**
* READ THIS BEFORE ADDING NEW MOCKS TO THIS FILE!
*
* This file should be loaded via `setupFilesAfterEnv` in all jest configs of the project.
* It instantiates some of the very basic mocks that should be available in all jest tests.
* Most of the mocks should just be instantiated in the test suites that require them.
* We only activate a very rare amount of mocks here, that are used somewhere down the dependency
* tree in nearly every test and their implementation is never working without mocking in any tests.
* Before adding a mock here it should be considered, whether it's not better to instantiate that mock
* in the test suites that needs them individually and if it's really needed to have that mock enabled
* for all jest tests by default.
*
* The mocks that are enabled that way live inside the `__mocks__` folders beside their implementation files.
*/

jest.mock('ui/metadata');
jest.mock('ui/chrome');

jest.mock('moment-timezone', () => {
// We always want to mock the timezone moment-timezone guesses, since otherwise
// test results might be depending on which time zone you are running them.
// Using that mock we always make sure moment.tz.guess is always returning the same
// timezone in all tests.
const moment = jest.requireActual('moment-timezone');
moment.tz.guess = () => 'America/New_York';
return moment;
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
* under the License.
*/

jest.mock('ui/metadata', () => ({
metadata: {
branch: 'foo',
},
}));

import { shallow } from 'enzyme';
import React from 'react';
import { QueryLanguageSwitcher } from './language_switcher';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ jest.mock('ui/kfetch', () => ({
jest.mock('ui/persisted_log', () => ({
PersistedLog: mockPersistedLogFactory,
}));
jest.mock('ui/metadata', () => ({
metadata: {
branch: 'foo',
},
}));
jest.mock('ui/autocomplete_providers', () => ({
getAutocompleteProvider: mockGetAutocompleteProvider,
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ jest.doMock('ui/new_platform', () => {
};
});

jest.doMock('ui/metadata', () => ({
metadata: {
branch: 'my-metadata-branch',
version: 'my-metadata-version',
},
}));

jest.doMock('ui/capabilities', () => ({
uiCapabilities: {
visualize: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
*/

jest.mock('ui/chrome', () => ({ getKibanaVersion: () => '6.0.0' }), { virtual: true });
jest.mock('ui/metadata', () => ({
metadata: {
branch: 'my-metadata-branch',
version: 'my-metadata-version',
},
}));

import { DEFAULT_PANEL_HEIGHT, DEFAULT_PANEL_WIDTH } from '../dashboard_constants';
import { SavedDashboardPanel } from '../types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,3 @@ jest.doMock('ui/new_platform', () => ({
}
},
}));

// Make importing the ui/notify module work in jest
jest.doMock('ui/metadata', () => ({
metadata: {
branch: 'my-metadata-branch',
version: 'my-metadata-version'
}
}));
1 change: 1 addition & 0 deletions src/legacy/core_plugins/tests_bundle/find_source_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const findSourceFiles = async (patterns, cwd = fromRoot('.')) => {
'**/_*.js',
'**/*.test.js',
'**/*.test.mocks.js',
'**/__mocks__/**/*',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ℹ️ Without that line __mocks__ files (which could contain calls to jest) would be bundled for Karma/Mocha tests and then cause them to fail, since jest is not available there.

],
symlinks: findSourceFiles.symlinks,
statCache: findSourceFiles.statCache,
Expand Down
25 changes: 25 additions & 0 deletions src/legacy/ui/public/__mocks__/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { metadata as metadataImpl } from '../metadata';

export const metadata: typeof metadataImpl = {
branch: 'jest-metadata-mock-branch',
version: '42.23.26',
};
38 changes: 38 additions & 0 deletions src/legacy/ui/public/chrome/__mocks__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { uiSettingsServiceMock } from '../../../../../core/public/mocks';

const uiSettingsClient = {
...uiSettingsServiceMock.createSetupContract(),
getUpdate$: () => ({
subscribe: jest.fn(),
}),
};

const chrome = {
getBasePath: () => '/test/base/path',
addBasePath: path => path,
getInjected: jest.fn(),
getUiSettingsClient: () => uiSettingsClient,
getXsrfToken: () => 'kbn-xsrf-token',
};

// eslint-disable-next-line import/no-default-export
export default chrome;
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
* specific language governing permissions and limitations
* under the License.
*/

export const kfetch = () => Promise.resolve();
3 changes: 3 additions & 0 deletions x-pack/dev-tools/jest/create_jest_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export function createJestConfig({
`<rootDir>/dev-tools/jest/setup/polyfills.js`,
`<rootDir>/dev-tools/jest/setup/enzyme.js`,
],
setupFilesAfterEnv: [
`${kibanaDirectory}/src/dev/jest/setup/mocks.js`,
],
testMatch: [
'**/*.test.{js,ts,tsx}'
],
Expand Down
11 changes: 0 additions & 11 deletions x-pack/plugins/__mocks__/moment-timezone.js

This file was deleted.

32 changes: 0 additions & 32 deletions x-pack/plugins/__mocks__/ui/capabilities.ts

This file was deleted.

61 changes: 0 additions & 61 deletions x-pack/plugins/__mocks__/ui/chrome.js

This file was deleted.

9 changes: 0 additions & 9 deletions x-pack/plugins/__mocks__/ui/kfetch.js

This file was deleted.

9 changes: 0 additions & 9 deletions x-pack/plugins/__mocks__/ui/kuery.js

This file was deleted.

10 changes: 0 additions & 10 deletions x-pack/plugins/__mocks__/ui/metadata.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import React from 'react';
import { mockMoment } from '../../../../utils/testHelpers';
import { DetailView } from './index';

jest.mock('ui/kfetch');

describe('DetailView', () => {
beforeEach(() => {
// Avoid timezone issues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { shallow } from 'enzyme';
import React from 'react';
import { Home } from '../Home';

jest.mock('ui/kfetch');

describe('Home component', () => {
it('should render', () => {
expect(shallow(<Home />)).toMatchSnapshot();
Expand Down
Loading