-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Changes from all commits
2df5647
a92e47e
aa2e170
aea76ca
45b8ac2
0e17aed
e9a72e8
c338d16
b31ab73
c586703
c32f0bd
27e6a4f
e6c0497
101c749
36e0c41
693908c
5cc2338
4c77c9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -36,6 +36,7 @@ const findSourceFiles = async (patterns, cwd = fromRoot('.')) => { | |
'**/_*.js', | ||
'**/*.test.js', | ||
'**/*.test.mocks.js', | ||
'**/__mocks__/**/*', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ℹ️ Without that line |
||
], | ||
symlinks: findSourceFiles.symlinks, | ||
statCache: findSourceFiles.statCache, | ||
|
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', | ||
}; |
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; |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
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.