Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
Upgrade to Jest 29 (elastic#143319)
Browse files Browse the repository at this point in the history
https://jestjs.io/blog/2022/04/25/jest-28
https://jestjs.io/blog/2022/08/25/jest-29

- jest.useFakeTimers('legacy') -> jest.useFakeTimers({ legacyFakeTimers:
true });
- jest.useFakeTimers('modern'); -> jest.useFakeTimers();
- tests can either use promises or callbacks, but not both
- test runner jasmine is no longer included, switch all suites to
jest-circus

Co-authored-by: Andrew Tate <[email protected]>
  • Loading branch information
jbudz and drewdaemon authored Nov 18, 2022
1 parent ab2eb9d commit 2ac7ebe
Show file tree
Hide file tree
Showing 264 changed files with 1,303 additions and 972 deletions.
36 changes: 17 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -716,9 +716,9 @@
"@emotion/jest": "^11.10.0",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@istanbuljs/schema": "^0.1.2",
"@jest/console": "^27.5.1",
"@jest/reporters": "^27.5.1",
"@jest/types": "^27",
"@jest/console": "^29.3.1",
"@jest/reporters": "^29.3.1",
"@jest/types": "^29.3.1",
"@kbn/ambient-storybook-types": "link:bazel-bin/packages/kbn-ambient-storybook-types",
"@kbn/ambient-ui-types": "link:bazel-bin/packages/kbn-ambient-ui-types",
"@kbn/apm-synthtrace": "link:bazel-bin/packages/kbn-apm-synthtrace",
Expand Down Expand Up @@ -801,7 +801,7 @@
"@storybook/react-docgen-typescript-plugin": "^1.0.1",
"@storybook/testing-react": "^1.3.0",
"@storybook/theming": "^6.5.12",
"@testing-library/dom": "^8.17.1",
"@testing-library/dom": "^8.19.0",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^12.1.5",
"@testing-library/react-hooks": "^8.0.1",
Expand Down Expand Up @@ -857,7 +857,7 @@
"@types/http-proxy": "^1.17.4",
"@types/inquirer": "^7.3.1",
"@types/intl-relativeformat": "^2.1.0",
"@types/jest": "^27.4.1",
"@types/jest": "29.2.0",
"@types/jest-axe": "^3.5.3",
"@types/jquery": "^3.3.31",
"@types/js-levenshtein": "^1.1.0",
Expand Down Expand Up @@ -934,7 +934,7 @@
"@types/tapable": "^1.0.6",
"@types/tar": "^4.0.5",
"@types/tempy": "^0.2.0",
"@types/testing-library__jest-dom": "^5.14.3",
"@types/testing-library__jest-dom": "^5.14.5",
"@types/tinycolor2": "^1.4.1",
"@types/tough-cookie": "^4.0.2",
"@types/type-detect": "^4.0.1",
Expand Down Expand Up @@ -967,7 +967,7 @@
"argsplit": "^1.0.5",
"autoprefixer": "^10.4.7",
"axe-core": "^4.0.2",
"babel-jest": "^27.5.1",
"babel-jest": "^29.2.2",
"babel-loader": "^8.2.5",
"babel-plugin-add-module-exports": "^1.0.4",
"babel-plugin-istanbul": "^6.1.1",
Expand Down Expand Up @@ -1022,7 +1022,7 @@
"eslint-plugin-react-perf": "^3.3.0",
"eslint-traverse": "^1.0.0",
"exit-hook": "^2.2.0",
"expect": "^28.1.1",
"expect": "^29.3.1",
"expose-loader": "^0.7.5",
"faker": "^5.1.0",
"fetch-mock": "^7.3.9",
Expand All @@ -1040,19 +1040,18 @@
"html-loader": "^1.3.2",
"http-proxy": "^1.18.1",
"is-path-inside": "^3.0.2",
"jest": "^27.5.1",
"jest": "^29.3.1",
"jest-axe": "^5.0.0",
"jest-canvas-mock": "^2.4.0",
"jest-cli": "^27.5.1",
"jest-config": "^27.5.1",
"jest-diff": "^27.5.1",
"jest-environment-jsdom": "^27.5.1",
"jest-matcher-utils": "^27.5.1",
"jest-mock": "^27.5.1",
"jest-raw-loader": "^1.0.1",
"jest-runtime": "^27.5.1",
"jest-cli": "^29.3.1",
"jest-config": "^29.3.1",
"jest-diff": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"jest-matcher-utils": "^29.3.1",
"jest-mock": "^29.3.1",
"jest-runtime": "^29.3.1",
"jest-silent-reporter": "^0.5.0",
"jest-snapshot": "^27.5.1",
"jest-snapshot": "^29.3.1",
"jest-specific-snapshot": "^5.0.0",
"jest-styled-components": "7.0.3",
"jsdom": "^16.4.0",
Expand Down Expand Up @@ -1092,7 +1091,6 @@
"postcss-prefix-selector": "^1.16.0",
"postcss-scss": "^4.0.4",
"prettier": "^2.7.1",
"pretty-format": "^27.5.1",
"proxy": "^1.0.2",
"q": "^1.5.1",
"raw-loader": "^3.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('AnalyticsClient', () => {
let logger: MockedLogger;

beforeEach(() => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });
logger = loggerMock.create();
analyticsClient = new AnalyticsClient({
logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('ElasticV3BrowserShipper', () => {
let fetchMock: jest.Mock;

beforeEach(() => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });

fetchMock = jest.fn().mockResolvedValue({
status: 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('ElasticV3ServerShipper', () => {
const setLastBatchSent = (ms: number) => (shipper['lastBatchSent'] = ms);

beforeEach(() => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });

shipper = new ElasticV3ServerShipper(
{ version: '1.2.3', channelName: 'test-channel', debug: true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const requiredProps: TableListViewProps = {
// FLAKY: https://github.com/elastic/kibana/issues/145267
describe.skip('TableListView', () => {
beforeAll(() => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });
});

afterAll(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { mocked } from './mocks_internal';

describe('EventLoopDelaysMonitor', () => {
beforeAll(() => {
jest.useFakeTimers('modern');
jest.useFakeTimers();
const mockNow = jest.getRealSystemTime();
jest.setSystemTime(mockNow);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('MetricsService', () => {
let metricsService: MetricsService;

beforeEach(() => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });

const configService = configServiceMock.create({
atPath: { interval: moment.duration(testInterval) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('OverlayBannersService', () => {
});

it('dismisses banner after timeout', async () => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });
startService('testing banner!');
expect(banners.remove).not.toHaveBeenCalled();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ test('`startPlugins` only starts plugins that were setup', async () => {

describe('setup', () => {
beforeAll(() => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });
});
afterAll(() => {
jest.useRealTimers();
Expand Down Expand Up @@ -588,7 +588,7 @@ describe('setup', () => {

describe('start', () => {
beforeAll(() => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });
});
afterAll(() => {
jest.useRealTimers();
Expand Down Expand Up @@ -747,7 +747,7 @@ describe('asynchronous plugins', () => {

describe('stop', () => {
beforeAll(() => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });
});

afterAll(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const SHAREABLE_HIDDEN_OBJ_TYPE = 'type-c';
const mockCurrentTime = new Date('2021-05-01T10:20:30Z');

beforeAll(() => {
jest.useFakeTimers('modern');
jest.useFakeTimers();
jest.setSystemTime(mockCurrentTime);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Cache } from './cache';

describe('Cache', () => {
beforeEach(() => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ describe('ui settings', () => {
describe('caching', () => {
describe('read operations cache user config', () => {
beforeEach(() => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });
});

afterEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-cli-dev-mode/src/dev_server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { extendedEnvSerializer } from './test_helpers';
import { DevServer, Options } from './dev_server';
import { TestLog } from './log';

jest.useFakeTimers('modern');
jest.useFakeTimers();

class MockProc extends EventEmitter {
public readonly signalsSent: string[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('palette panel', () => {
dataBounds: { min: 0, max: 100 },
};

jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });
});

function changePaletteIn(instance: ReactWrapper, newPaletteName: string) {
Expand Down
10 changes: 5 additions & 5 deletions packages/kbn-datemath/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('dateMath', function () {
let now;

beforeEach(function () {
jest.useFakeTimers('modern');
jest.useFakeTimers();
jest.setSystemTime(unix);
now = moment();
mmnt = moment(anchor);
Expand Down Expand Up @@ -128,7 +128,7 @@ describe('dateMath', function () {
let anchored;

beforeEach(function () {
jest.useFakeTimers('modern');
jest.useFakeTimers();
jest.setSystemTime(unix);
now = moment();
anchored = moment(anchor);
Expand Down Expand Up @@ -166,7 +166,7 @@ describe('dateMath', function () {
let anchored;

beforeEach(function () {
jest.useFakeTimers('modern');
jest.useFakeTimers();
jest.setSystemTime(unix);
now = moment();
anchored = moment(anchor);
Expand Down Expand Up @@ -205,7 +205,7 @@ describe('dateMath', function () {
let anchored;

beforeEach(function () {
jest.useFakeTimers('modern');
jest.useFakeTimers();
jest.setSystemTime(unix);
now = moment();
anchored = moment(anchor);
Expand Down Expand Up @@ -247,7 +247,7 @@ describe('dateMath', function () {
let anchored;

beforeEach(function () {
jest.useFakeTimers('modern');
jest.useFakeTimers();
jest.setSystemTime(unix);
now = moment();
anchored = moment(anchor);
Expand Down
1 change: 0 additions & 1 deletion packages/kbn-i18n-react/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ module.exports = {
preset: '@kbn/test',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-i18n-react'],
testRunner: 'jasmine2',
};
1 change: 0 additions & 1 deletion packages/kbn-i18n/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ module.exports = {
preset: '@kbn/test',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-i18n'],
testRunner: 'jasmine2',
};
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Painless DiagnosticAdapter', () => {
let validation: LangValidation;

beforeAll(() => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });
});

afterAll(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-optimizer/src/common/rxjs_helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { fakeSchedulers } from 'rxjs-marbles/jest';

import { pipeClosure, debounceTimeBuffer, maybeMap, maybe } from './rxjs_helpers';

jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });

describe('pipeClosure()', () => {
it('calls closure on each subscription to setup unique state', async () => {
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('maybeMap()', () => {

describe('debounceTimeBuffer()', () => {
beforeEach(() => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const bundleCacheEvent$ = Rx.from(BUNDLES).pipe(
);

beforeEach(async () => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({ legacyFakeTimers: true });
});

afterEach(async () => {
Expand Down
11 changes: 6 additions & 5 deletions packages/kbn-optimizer/src/optimizer/optimizer_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ jest.mock('./focus_bundles');
jest.mock('../limits');

jest.mock('os', () => {
const realOs = jest.requireActual('os');
jest.spyOn(realOs, 'cpus').mockImplementation(() => {
return ['foo'] as any;
});
return realOs;
return {
...jest.requireActual('os'),
cpus() {
return ['foo'] as any;
},
};
});

import Path from 'path';
Expand Down
9 changes: 7 additions & 2 deletions packages/kbn-test/jest-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ module.exports = {
'<rootDir>/node_modules/@kbn/test/target_node/src/jest/setup/react_testing_library.js',
],

snapshotFormat: {
escapeString: true,
printBasicPrototype: true,
},

// A list of paths to snapshot serializer modules Jest should use for snapshot testing
snapshotSerializers: [
'<rootDir>/src/plugins/kibana_react/public/util/test_helpers/react_mount_serializer.ts',
Expand All @@ -116,8 +121,8 @@ module.exports = {
// A map from regular expressions to paths to transformers
transform: {
'^.+\\.(js|tsx?)$': '<rootDir>/node_modules/@kbn/test/target_node/src/jest/babel_transform.js',
'^.+\\.txt?$': 'jest-raw-loader',
'^.+\\.html?$': 'jest-raw-loader',
'^.+\\.txt?$': '<rootDir>/node_modules/@kbn/test/target_node/src/jest/raw_transform.js',
'^.+\\.html?$': '<rootDir>/node_modules/@kbn/test/target_node/src/jest/raw_transform.js',
},

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
Expand Down
2 changes: 2 additions & 0 deletions packages/kbn-test/jest_integration/jest-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

const preset = require('../jest-preset');

/** @typedef {import("@jest/types").Config.InitialOptions} JestConfig */
/** @type {JestConfig} */
module.exports = {
...preset,
testMatch: ['**/integration_tests**/*.test.{js,mjs,ts,tsx}'],
Expand Down
2 changes: 2 additions & 0 deletions packages/kbn-test/jest_node/jest-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const presetClone = { ...preset };

delete presetClone.testEnvironment; // simply redefining as `testEnvironment: 'node'` has some weird side-effects (https://github.com/elastic/kibana/pull/138877#issuecomment-1222366247)

/** @typedef {import("@jest/types").Config.InitialOptions} JestConfig */
/** @type {JestConfig} */
module.exports = {
...presetClone,
snapshotSerializers: [],
Expand Down
Loading

0 comments on commit 2ac7ebe

Please sign in to comment.