Skip to content

Commit

Permalink
Make sure snapshots for skipped tests are marked as used
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar committed Sep 14, 2020
1 parent ba8195d commit 98524d8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,11 @@ export default function ApiTest({ getService }: FtrProviderContext) {
]
`);

expectSnapshot({
title,
color,
type,
hideLegend,
legendValue,
}).toMatchInline(`
Object {
"color": "#54b399",
"hideLegend": false,
"legendValue": "100%",
"title": "app",
"type": "areaStacked",
}
`);
expectSnapshot(title).toMatchInline(`"app"`);
expectSnapshot(color).toMatchInline(`"#54b399"`);
expectSnapshot(type).toMatchInline(`"areaStacked"`);
expectSnapshot(hideLegend).toMatchInline(`false`);
expectSnapshot(legendValue).toMatchInline(`"100%"`);
});
it('returns the transaction breakdown sorted by name', async () => {
const response = await supertest.get(
Expand Down
49 changes: 33 additions & 16 deletions x-pack/test/apm_api_integration/common/match_snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,33 @@ function getSnapshotMeta(currentTest: Test) {
}

export function registerMochaHooksForSnapshots() {
let snapshotStatesByFilePath: Record<string, ISnapshotState> = {};
let snapshotStatesByFilePath: Record<
string,
{ snapshotState: ISnapshotState; testsInFile: Test[] }
> = {};

registered = true;

beforeEach(function () {
const { file, snapshotTitle } = getSnapshotMeta(this.currentTest!);
const currentTest = this.currentTest!;

const { file, snapshotTitle } = getSnapshotMeta(currentTest);

if (!snapshotStatesByFilePath[file]) {
snapshotStatesByFilePath[file] = getSnapshotState(file);
snapshotStatesByFilePath[file] = getSnapshotState(file, currentTest);
}

testContext = {
file,
snapshotTitle,
snapshotContext: {
snapshotState: snapshotStatesByFilePath[file],
snapshotState: snapshotStatesByFilePath[file].snapshotState,
currentTestName: snapshotTitle,
},
};
});

afterEach(function () {
if (!this.currentTest?.isPassed()) {
const { file, snapshotTitle } = getSnapshotMeta(this.currentTest!);
snapshotStatesByFilePath[file].markSnapshotsAsCheckedForTest(snapshotTitle);
}

testContext = null;
});

Expand All @@ -94,15 +94,24 @@ export function registerMochaHooksForSnapshots() {
const isUpdatingSnapshots = process.env.UPDATE_SNAPSHOTS;

Object.keys(snapshotStatesByFilePath).forEach((file) => {
const snapshot = snapshotStatesByFilePath[file];
const { snapshotState, testsInFile } = snapshotStatesByFilePath[file];

testsInFile.forEach((test) => {
const snapshotMeta = getSnapshotMeta(test);
// If test is failed or skipped, mark snapshots as used. Otherwise,
// running a test in isolation will generate false positives.
if (!test.isPassed()) {
snapshotState.markSnapshotsAsCheckedForTest(snapshotMeta.snapshotTitle);
}
});

if (!isUpdatingSnapshots) {
unused.push(...snapshot.getUncheckedKeys());
unused.push(...snapshotState.getUncheckedKeys());
} else {
snapshot.removeUncheckedKeys();
snapshotState.removeUncheckedKeys();
}

snapshot.save();
snapshotState.save();
});

if (unused.length) {
Expand Down Expand Up @@ -134,10 +143,18 @@ Error.prepareStackTrace = (error, structuredStackTrace) => {
}
};

function getSnapshotState(file: string) {
function getSnapshotState(file: string, test: Test) {
const dirname = path.dirname(file);
const filename = path.basename(file);

let parent = test.parent;
const testsInFile: Test[] = [];

while (parent) {
testsInFile.push(...parent.tests);
parent = parent.parent;
}

const snapshotState = new SnapshotState(
path.join(dirname + `/__snapshots__/` + filename.replace(path.extname(filename), '.snap')),
{
Expand All @@ -147,7 +164,7 @@ function getSnapshotState(file: string) {
}
);

return snapshotState;
return { snapshotState, testsInFile };
}

export function expectSnapshot(received: any) {
Expand Down Expand Up @@ -182,7 +199,7 @@ function expectToMatchInlineSnapshot(
) {
const matcher = toMatchInlineSnapshot.bind(snapshotContext as any);

const result = arguments.length === 1 ? matcher(received) : matcher(received, _actual);
const result = arguments.length === 2 ? matcher(received) : matcher(received, _actual);

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

0 comments on commit 98524d8

Please sign in to comment.