expectSnapshot: throw on CI when snapshot is new (#85376)

This commit is contained in:
Dario Gieselaar 2020-12-16 13:21:48 +01:00 committed by GitHub
parent a1bd35f212
commit 003efb1cc4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 423 additions and 7 deletions

View file

@ -41,7 +41,7 @@ export const loadTestFiles = ({
updateBaselines,
updateSnapshots,
}) => {
decorateSnapshotUi(lifecycle, updateSnapshots);
decorateSnapshotUi({ lifecycle, updateSnapshots, isCi: !!process.env.CI });
const innerLoadTestFile = (path) => {
if (typeof path !== 'string' || !isAbsolute(path)) {

View file

@ -28,7 +28,7 @@ describe('decorateSnapshotUi', () => {
let lifecycle: Lifecycle;
beforeEach(() => {
lifecycle = new Lifecycle();
decorateSnapshotUi(lifecycle, false);
decorateSnapshotUi({ lifecycle, updateSnapshots: false, isCi: false });
});
it('passes when the snapshot matches the actual value', async () => {
@ -109,7 +109,7 @@ describe('decorateSnapshotUi', () => {
let lifecycle: Lifecycle;
beforeEach(() => {
lifecycle = new Lifecycle();
decorateSnapshotUi(lifecycle, true);
decorateSnapshotUi({ lifecycle, updateSnapshots: true, isCi: false });
});
it("doesn't throw if the value does not match", async () => {
@ -130,4 +130,30 @@ describe('decorateSnapshotUi', () => {
}).not.toThrow();
});
});
describe('when running on ci', () => {
let lifecycle: Lifecycle;
beforeEach(() => {
lifecycle = new Lifecycle();
decorateSnapshotUi({ lifecycle, updateSnapshots: false, isCi: true });
});
it('throws on new snapshots', async () => {
const test: Test = {
title: 'Test',
file: 'foo.ts',
parent: {
file: 'foo.ts',
tests: [],
suites: [],
},
} as any;
await lifecycle.beforeEachTest.trigger(test);
expect(() => {
expectSnapshot('bar').toMatchInline();
}).toThrow();
});
});
});

View file

@ -32,6 +32,8 @@ import { Test, Suite } from '../../fake_mocha_types';
type ISnapshotState = InstanceType<typeof SnapshotState>;
type SnapshotUpdateState = 'all' | 'new' | 'none';
interface SnapshotContext {
snapshotState: ISnapshotState;
currentTestName: string;
@ -94,7 +96,15 @@ const modifyStackTracePrepareOnce = once(() => {
};
});
export function decorateSnapshotUi(lifecycle: Lifecycle, updateSnapshots: boolean) {
export function decorateSnapshotUi({
lifecycle,
updateSnapshots,
isCi,
}: {
lifecycle: Lifecycle;
updateSnapshots: boolean;
isCi: boolean;
}) {
let snapshotStatesByFilePath: Record<
string,
{ snapshotState: ISnapshotState; testsInFile: Test[] }
@ -102,6 +112,16 @@ export function decorateSnapshotUi(lifecycle: Lifecycle, updateSnapshots: boolea
registered = true;
let updateSnapshot: SnapshotUpdateState;
if (isCi) {
// make sure snapshots that have not been committed
// are not written to file on CI, passing the test
updateSnapshot = 'none';
} else {
updateSnapshot = updateSnapshots ? 'all' : 'new';
}
modifyStackTracePrepareOnce();
addSerializer({
@ -120,7 +140,7 @@ export function decorateSnapshotUi(lifecycle: Lifecycle, updateSnapshots: boolea
const { file, snapshotTitle } = getSnapshotMeta(currentTest);
if (!snapshotStatesByFilePath[file]) {
snapshotStatesByFilePath[file] = getSnapshotState(file, currentTest, updateSnapshots);
snapshotStatesByFilePath[file] = getSnapshotState(file, currentTest, updateSnapshot);
}
testContext = {
@ -178,7 +198,7 @@ function recursivelyGetTestsFromSuite(suite: Suite): Test[] {
return suite.tests.concat(flatten(suite.suites.map((s) => recursivelyGetTestsFromSuite(s))));
}
function getSnapshotState(file: string, test: Test, updateSnapshots: boolean) {
function getSnapshotState(file: string, test: Test, updateSnapshot: SnapshotUpdateState) {
const dirname = path.dirname(file);
const filename = path.basename(file);
@ -195,7 +215,7 @@ function getSnapshotState(file: string, test: Test, updateSnapshots: boolean) {
const snapshotState = new SnapshotState(
path.join(dirname + `/__snapshots__/` + filename.replace(path.extname(filename), '.snap')),
{
updateSnapshot: updateSnapshots ? 'all' : 'new',
updateSnapshot,
// @ts-expect-error
getPrettier: () => prettier,
getBabelTraverse: () => babelTraverse,

View file

@ -0,0 +1,370 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`monitor states endpoint will fetch monitor state data for the given down filters 1`] = `
Object {
"nextPagePagination": "{\\"cursorDirection\\":\\"AFTER\\",\\"sortOrder\\":\\"ASC\\",\\"cursorKey\\":{\\"monitor_id\\":\\"0020-down\\"}}",
"prevPagePagination": null,
"summaries": Array [
Object {
"histogram": Object {
"points": Array [
Object {
"down": 1,
"timestamp": 1568172624744,
},
Object {
"down": 2,
"timestamp": 1568172677247,
},
Object {
"down": 1,
"timestamp": 1568172729750,
},
Object {
"down": 2,
"timestamp": 1568172782253,
},
Object {
"down": 2,
"timestamp": 1568172834756,
},
Object {
"down": 2,
"timestamp": 1568172887259,
},
Object {
"down": 1,
"timestamp": 1568172939762,
},
Object {
"down": 2,
"timestamp": 1568172992265,
},
Object {
"down": 2,
"timestamp": 1568173044768,
},
Object {
"down": 2,
"timestamp": 1568173097271,
},
Object {
"down": 1,
"timestamp": 1568173149774,
},
Object {
"down": 2,
"timestamp": 1568173202277,
},
],
},
"minInterval": 52503,
"monitor_id": "0010-down",
"state": Object {
"monitor": Object {
"name": "",
"type": "http",
},
"observer": Object {
"geo": Object {
"name": Array [
"mpls",
],
},
},
"summary": Object {
"down": 1,
"status": "down",
"up": 0,
},
"summaryPings": Array [
Object {
"@timestamp": "2019-09-11T03:40:34.371Z",
"agent": Object {
"ephemeral_id": "412a92a8-2142-4b1a-a7a2-1afd32e12f85",
"hostname": "avc-x1x",
"id": "04e1d082-65bc-4929-8d65-d0768a2621c4",
"type": "heartbeat",
"version": "8.0.0",
},
"docId": "rZtoHm0B0I9WX_CznN_V",
"ecs": Object {
"version": "1.1.0",
},
"error": Object {
"message": "400 Bad Request",
"type": "validate",
},
"event": Object {
"dataset": "uptime",
},
"host": Object {
"name": "avc-x1x",
},
"http": Object {
"response": Object {
"body": Object {
"bytes": 3,
"content": "400",
"hash": "26d228663f13a88592a12d16cf9587caab0388b262d6d9f126ed62f9333aca94",
},
"status_code": 400,
},
"rtt": Object {
"content": Object {
"us": 41,
},
"response_header": Object {
"us": 36777,
},
"total": Object {
"us": 37821,
},
"validate": Object {
"us": 36818,
},
"write_request": Object {
"us": 53,
},
},
},
"monitor": Object {
"check_group": "d76f07d1-d445-11e9-88e3-3e80641b9c71",
"duration": Object {
"us": 37926,
},
"id": "0010-down",
"ip": "127.0.0.1",
"name": "",
"status": "down",
"type": "http",
},
"observer": Object {
"geo": Object {
"location": "37.926868, -78.024902",
"name": "mpls",
},
"hostname": "avc-x1x",
},
"resolve": Object {
"ip": "127.0.0.1",
"rtt": Object {
"us": 56,
},
},
"summary": Object {
"down": 1,
"up": 0,
},
"tcp": Object {
"rtt": Object {
"connect": Object {
"us": 890,
},
},
},
"timestamp": "2019-09-11T03:40:34.371Z",
"url": Object {
"domain": "localhost",
"full": "http://localhost:5678/pattern?r=400x1",
"path": "/pattern",
"port": 5678,
"query": "r=400x1",
"scheme": "http",
},
},
],
"timestamp": "2019-09-11T03:40:34.371Z",
"url": Object {
"domain": "localhost",
"full": "http://localhost:5678/pattern?r=400x1",
"path": "/pattern",
"port": 5678,
"query": "r=400x1",
"scheme": "http",
},
},
},
Object {
"histogram": Object {
"points": Array [
Object {
"down": 1,
"timestamp": 1568172624744,
},
Object {
"down": 2,
"timestamp": 1568172677247,
},
Object {
"down": 1,
"timestamp": 1568172729750,
},
Object {
"down": 2,
"timestamp": 1568172782253,
},
Object {
"down": 2,
"timestamp": 1568172834756,
},
Object {
"down": 2,
"timestamp": 1568172887259,
},
Object {
"down": 1,
"timestamp": 1568172939762,
},
Object {
"down": 2,
"timestamp": 1568172992265,
},
Object {
"down": 2,
"timestamp": 1568173044768,
},
Object {
"down": 2,
"timestamp": 1568173097271,
},
Object {
"down": 1,
"timestamp": 1568173149774,
},
Object {
"down": 2,
"timestamp": 1568173202277,
},
],
},
"minInterval": 52503,
"monitor_id": "0020-down",
"state": Object {
"monitor": Object {
"name": "",
"type": "http",
},
"observer": Object {
"geo": Object {
"name": Array [
"mpls",
],
},
},
"summary": Object {
"down": 1,
"status": "down",
"up": 0,
},
"summaryPings": Array [
Object {
"@timestamp": "2019-09-11T03:40:34.372Z",
"agent": Object {
"ephemeral_id": "412a92a8-2142-4b1a-a7a2-1afd32e12f85",
"hostname": "avc-x1x",
"id": "04e1d082-65bc-4929-8d65-d0768a2621c4",
"type": "heartbeat",
"version": "8.0.0",
},
"docId": "X5toHm0B0I9WX_CznN-6",
"ecs": Object {
"version": "1.1.0",
},
"error": Object {
"message": "400 Bad Request",
"type": "validate",
},
"event": Object {
"dataset": "uptime",
},
"host": Object {
"name": "avc-x1x",
},
"http": Object {
"response": Object {
"body": Object {
"bytes": 3,
"content": "400",
"hash": "26d228663f13a88592a12d16cf9587caab0388b262d6d9f126ed62f9333aca94",
},
"status_code": 400,
},
"rtt": Object {
"content": Object {
"us": 54,
},
"response_header": Object {
"us": 180,
},
"total": Object {
"us": 555,
},
"validate": Object {
"us": 234,
},
"write_request": Object {
"us": 63,
},
},
},
"monitor": Object {
"check_group": "d7712ecb-d445-11e9-88e3-3e80641b9c71",
"duration": Object {
"us": 14900,
},
"id": "0020-down",
"ip": "127.0.0.1",
"name": "",
"status": "down",
"type": "http",
},
"observer": Object {
"geo": Object {
"location": "37.926868, -78.024902",
"name": "mpls",
},
"hostname": "avc-x1x",
},
"resolve": Object {
"ip": "127.0.0.1",
"rtt": Object {
"us": 14294,
},
},
"summary": Object {
"down": 1,
"up": 0,
},
"tcp": Object {
"rtt": Object {
"connect": Object {
"us": 105,
},
},
},
"timestamp": "2019-09-11T03:40:34.372Z",
"url": Object {
"domain": "localhost",
"full": "http://localhost:5678/pattern?r=400x1",
"path": "/pattern",
"port": 5678,
"query": "r=400x1",
"scheme": "http",
},
},
],
"timestamp": "2019-09-11T03:40:34.372Z",
"url": Object {
"domain": "localhost",
"full": "http://localhost:5678/pattern?r=400x1",
"path": "/pattern",
"port": 5678,
"query": "r=400x1",
"scheme": "http",
},
},
},
],
}
`;