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

[APM] API Snapshot Testing #77229

Merged
merged 12 commits into from
Sep 14, 2020
Merged
4 changes: 4 additions & 0 deletions x-pack/test/apm_api_integration/basic/tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { FtrProviderContext } from '../../common/ftr_provider_context';
import * as matchSnapshot from '../../common/match_snapshot';

export default function apmApiIntegrationTests({ loadTestFile }: FtrProviderContext) {
describe('APM specs (basic)', function () {
beforeEach(matchSnapshot.init);
afterEach(matchSnapshot.teardown);
Copy link
Member Author

Choose a reason for hiding this comment

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

Alternatively, we can try using require('../../common/match_snapshot'), and then calling beforeEach() and afterEach() in that file. We could also add an after() hook there that checks if there are unused snapshots (we'd need to track them in expectMatchSnapshot).


this.tags('ciGroup1');

loadTestFile(require.resolve('./feature_controls'));
Expand Down

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 @@ -5,6 +5,7 @@
*/
import expect from '@kbn/expect';
import { sortBy, omit } from 'lodash';
import { expectToMatchSnapshot } from '../../../common/match_snapshot';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
import expectTopTraces from './expectation/top_traces.expectation.json';

Expand All @@ -19,13 +20,13 @@ export default function ApiTest({ getService }: FtrProviderContext) {

describe('Top traces', () => {
describe('when data is not loaded ', () => {
it('handles empty state', async () => {
it('handles empty state', async function () {
Copy link
Member

Choose a reason for hiding this comment

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

Is this change necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

no longer, left over from initial exploration. I'll remove it.

const response = await supertest.get(
`/api/apm/traces?start=${start}&end=${end}&uiFilters=${uiFilters}`
);

expect(response.status).to.be(200);
expect(response.body).to.eql({ items: [], isAggregationAccurate: true, bucketSize: 1000 });
expectToMatchSnapshot(response.body);
});
});

Expand Down
61 changes: 61 additions & 0 deletions x-pack/test/apm_api_integration/common/match_snapshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { SnapshotState, toMatchSnapshot } from 'jest-snapshot';
import path from 'path';
import { Context } from 'mocha';
import expect from '@kbn/expect';

let testContext: { file: string; testTitle: string } | null = null;

export function init() {
// @ts-ignore
const mochaContext = this as Context;
const file = mochaContext.currentTest?.file;
const testTitle = mochaContext.currentTest?.fullTitle();

if (!file || !testTitle) {
throw new Error(`file or fullTitle not found in Mocha test context`);
}

testContext = {
file,
testTitle,
};
}

export function teardown() {
testContext = null;
}

export function expectToMatchSnapshot(actual: any) {
if (!testContext) {
throw new Error('A current Mocha context is needed to match snapshots');
}

const { file, testTitle } = testContext;

const dirname = path.dirname(file);
const filename = path.basename(file);

const snapshotState = new SnapshotState(
path.join(dirname + `/__snapshots__/` + filename.replace(path.extname(filename), '.snap')),
// not passing babel or prettier
// @ts-ignore
{
updateSnapshot: process.env.UPDATE_APM_SNAPSHOTS ? 'all' : 'new',
}
);

// not passing assertions
// @ts-ignore
const matcher = toMatchSnapshot.bind({ snapshotState, currentTestName: testTitle });
const result = matcher(actual);

snapshotState.save();

return expect(result.pass).to.eql(true, result.message());
}