Skip to content

Commit

Permalink
Jest and Storybook fixes (#116132)
Browse files Browse the repository at this point in the history
* Fix all broken stories
* In stories that were broken, add an associated Jest test so they if they break in the future we'll know
* Fix all console.error messages that were being printed during Jest test runs
* Add test setup which makes it so `console.error` throws an error so tests will fail if error console messages are printed
# Conflicts:
#	x-pack/plugins/apm/public/components/shared/backend_link.stories.tsx
  • Loading branch information
smith committed Oct 28, 2021
1 parent ec93fc2 commit b79cd04
Show file tree
Hide file tree
Showing 23 changed files with 492 additions and 1,600 deletions.
5 changes: 4 additions & 1 deletion x-pack/plugins/apm/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ module.exports = {
preset: '@kbn/test',
rootDir: path.resolve(__dirname, '../../..'),
roots: ['<rootDir>/x-pack/plugins/apm'],
setupFiles: ['<rootDir>/x-pack/plugins/apm/.storybook/jest_setup.js'],
setupFiles: [
'<rootDir>/x-pack/plugins/apm/jest_setup.js',
'<rootDir>/x-pack/plugins/apm/.storybook/jest_setup.js',
],
coverageDirectory: '<rootDir>/target/kibana-coverage/jest/x-pack/plugins/apm',
coverageReporters: ['text', 'html'],
collectCoverageFrom: [
Expand Down
14 changes: 14 additions & 0 deletions x-pack/plugins/apm/jest_setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

/* global jest */

// When a `console.error` is encountered, throw the error to make the test fail.
// This effectively treats logged errors during the test run as failures.
jest.spyOn(console, 'error').mockImplementation((message) => {
throw new Error(message);
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ import { AlertParams, ErrorCountAlertTrigger } from '.';
import { CoreStart } from '../../../../../../../src/core/public';
import { createKibanaReactContext } from '../../../../../../../src/plugins/kibana_react/public';
import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values';
import { createCallApmApi } from '../../../services/rest/createCallApmApi';

import { AlertMetadata } from '../helper';

const KibanaReactContext = createKibanaReactContext({
const coreMock = {
http: { get: async () => ({}) },
notifications: { toasts: { add: () => {} } },
} as unknown as Partial<CoreStart>);
uiSettings: { get: () => {} },
} as unknown as CoreStart;

const KibanaReactContext = createKibanaReactContext(coreMock);

interface Args {
alertParams: AlertParams;
Expand All @@ -27,6 +33,8 @@ const stories: Meta<{}> = {
component: ErrorCountAlertTrigger,
decorators: [
(StoryComponent) => {
createCallApmApi(coreMock);

return (
<KibanaReactContext.Provider>
<div style={{ width: 400 }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
* 2.0.
*/

import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import React from 'react';
import * as stories from './error_count_alert_trigger.stories';
import { composeStories } from '@storybook/testing-react';

const { CreatingInApmFromService } = composeStories(stories);

describe('ErrorCountAlertTrigger', () => {
it('renders', () => {
expect(() => render(<CreatingInApmFromService />)).not.toThrowError();
it('renders', async () => {
render(<CreatingInApmFromService />);

expect(await screen.findByText('Service')).toBeInTheDocument();
});
});

This file was deleted.

Loading

0 comments on commit b79cd04

Please sign in to comment.