Skip to content

Commit

Permalink
Make expectSnapshot available in all functional test runs (elastic#82932
Browse files Browse the repository at this point in the history
)

Co-authored-by: spalger <spalger@users.noreply.github.com>
# Conflicts:
#	packages/kbn-test/src/functional_test_runner/cli.ts
  • Loading branch information
dgieselaar committed Nov 19, 2020
1 parent 616d582 commit ce8e7f6
Show file tree
Hide file tree
Showing 53 changed files with 327 additions and 120 deletions.
14 changes: 12 additions & 2 deletions packages/kbn-test/src/functional_test_runner/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export function runFtrCli() {
include: toArray(flags['include-tag'] as string | string[]),
exclude: toArray(flags['exclude-tag'] as string | string[]),
},
updateBaselines: flags.updateBaselines,
updateBaselines: flags.updateBaselines || flags.u,
updateSnapshots: flags.updateSnapshots || flags.u,
}
);

Expand Down Expand Up @@ -118,7 +119,14 @@ export function runFtrCli() {
'exclude-tag',
'kibana-install-dir',
],
boolean: ['bail', 'invert', 'test-stats', 'updateBaselines'],
boolean: [
'bail',
'invert',
'test-stats',
'updateBaselines',
'updateSnapshots',
'u'
],
default: {
config: 'test/functional/config.js',
},
Expand All @@ -133,6 +141,8 @@ export function runFtrCli() {
--exclude-tag=tag a tag to be excluded, pass multiple times for multiple tags
--test-stats print the number of tests (included and excluded) to STDERR
--updateBaselines replace baseline screenshots with whatever is generated from the test
--updateSnapshots replace inline and file snapshots with whatever is generated from the test
-u replace both baseline screenshots and snapshots
--kibana-install-dir directory where the Kibana install being tested resides
`,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ import EventEmitter from 'events';
export interface Suite {
suites: Suite[];
tests: Test[];
title: string;
file?: string;
parent?: Suite;
}

export interface Test {
fullTitle(): string;
title: string;
file?: string;
parent?: Suite;
isPassed: () => boolean;
}

export interface Runner extends EventEmitter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const schema = Joi.object()
.default(),

updateBaselines: Joi.boolean().default(false),

updateSnapshots: Joi.boolean().default(false),
browser: Joi.object()
.keys({
type: Joi.string().valid('chrome', 'firefox', 'msedge').default('chrome'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { isAbsolute } from 'path';

import { loadTracer } from '../load_tracer';
import { decorateMochaUi } from './decorate_mocha_ui';
import { decorateSnapshotUi } from '../snapshots/decorate_snapshot_ui';

/**
* Load an array of test files into a mocha instance
Expand All @@ -31,7 +32,17 @@ import { decorateMochaUi } from './decorate_mocha_ui';
* @param {String} path
* @return {undefined} - mutates mocha, no return value
*/
export const loadTestFiles = ({ mocha, log, lifecycle, providers, paths, updateBaselines }) => {
export const loadTestFiles = ({
mocha,
log,
lifecycle,
providers,
paths,
updateBaselines,
updateSnapshots,
}) => {
decorateSnapshotUi(lifecycle, updateSnapshots);

const innerLoadTestFile = (path) => {
if (typeof path !== 'string' || !isAbsolute(path)) {
throw new TypeError('loadTestFile() only accepts absolute paths');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export async function setupMocha(lifecycle, log, config, providers) {
providers,
paths: config.get('testFiles'),
updateBaselines: config.get('updateBaselines'),
updateSnapshots: config.get('updateSnapshots'),
});

// Each suite has a tag that is the path relative to the root of the repo
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { Test } from '../../fake_mocha_types';
import { Lifecycle } from '../lifecycle';
import { decorateSnapshotUi, expectSnapshot } from './decorate_snapshot_ui';
import path from 'path';
import fs from 'fs';

describe('decorateSnapshotUi', () => {
describe('when running a test', () => {
let lifecycle: Lifecycle;
beforeEach(() => {
lifecycle = new Lifecycle();
decorateSnapshotUi(lifecycle, false);
});

it('passes when the snapshot matches the actual value', async () => {
const test: Test = {
title: 'Test',
file: 'foo.ts',
parent: {
file: 'foo.ts',
tests: [],
suites: [],
},
} as any;

await lifecycle.beforeEachTest.trigger(test);

expect(() => {
expectSnapshot('foo').toMatchInline(`"foo"`);
}).not.toThrow();
});

it('throws when the snapshot does not match the actual value', async () => {
const test: Test = {
title: 'Test',
file: 'foo.ts',
parent: {
file: 'foo.ts',
tests: [],
suites: [],
},
} as any;

await lifecycle.beforeEachTest.trigger(test);

expect(() => {
expectSnapshot('foo').toMatchInline(`"bar"`);
}).toThrow();
});

it('writes a snapshot to an external file if it does not exist', async () => {
const test: Test = {
title: 'Test',
file: __filename,
isPassed: () => true,
} as any;

// @ts-expect-error
test.parent = {
file: __filename,
tests: [test],
suites: [],
};

await lifecycle.beforeEachTest.trigger(test);

const snapshotFile = path.resolve(
__dirname,
'__snapshots__',
'decorate_snapshot_ui.test.snap'
);

expect(fs.existsSync(snapshotFile)).toBe(false);

expect(() => {
expectSnapshot('foo').toMatch();
}).not.toThrow();

await lifecycle.afterTestSuite.trigger(test.parent);

expect(fs.existsSync(snapshotFile)).toBe(true);

fs.unlinkSync(snapshotFile);

fs.rmdirSync(path.resolve(__dirname, '__snapshots__'));
});
});

describe('when updating snapshots', () => {
let lifecycle: Lifecycle;
beforeEach(() => {
lifecycle = new Lifecycle();
decorateSnapshotUi(lifecycle, true);
});

it("doesn't throw if the value does not match", 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(`"foo"`);
}).not.toThrow();
});
});
});
Loading

0 comments on commit ce8e7f6

Please sign in to comment.