-
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
[APM] API Snapshot Testing #77229
[APM] API Snapshot Testing #77229
Changes from 1 commit
fefe6c0
818a96a
2c26899
291cbb9
5efece4
129780e
4be2387
b94fbbd
541ef2a
ba8195d
98524d8
c98af53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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'; | ||
|
||
|
@@ -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 () { | ||
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. Is this change necessary? 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. 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); | ||
}); | ||
}); | ||
|
||
|
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()); | ||
} |
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.
Alternatively, we can try using require('../../common/match_snapshot'), and then calling
beforeEach()
andafterEach()
in that file. We could also add anafter()
hook there that checks if there are unused snapshots (we'd need to track them inexpectMatchSnapshot
).