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

Make expectSnapshot available in all functional test runs #82932

Merged
merged 24 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6e95ee7
Make expectSnapshot available in all functional test runs
dgieselaar Nov 9, 2020
7cee115
Add & update tests
dgieselaar Nov 9, 2020
3bb35e5
Merge branch 'master' of github.com:elastic/kibana into snapshot-test…
dgieselaar Nov 9, 2020
8f24c32
Make sure unused snapshot check only runs once
dgieselaar Nov 9, 2020
2076517
Merge branch 'master' of github.com:elastic/kibana into snapshot-test…
dgieselaar Nov 9, 2020
f6f84b5
Remove console.log statement
dgieselaar Nov 9, 2020
7105392
Fix failing test
dgieselaar Nov 9, 2020
5d9e332
Merge branch 'master' of github.com:elastic/kibana into snapshot-test…
dgieselaar Nov 9, 2020
feb156a
Define own Mocha types to prevent Jest conflicts
dgieselaar Nov 10, 2020
c88ce41
Merge branch 'master' of github.com:elastic/kibana into snapshot-test…
dgieselaar Nov 10, 2020
61ed37b
Move snapshot typings file to test/typings
dgieselaar Nov 10, 2020
56f0c9b
Merge branch 'master' of github.com:elastic/kibana into snapshot-test…
dgieselaar Nov 11, 2020
22bd132
Add -u shorthand option for updating baseline screenshots and snapshots
dgieselaar Nov 11, 2020
4342dd6
move types
dgieselaar Nov 11, 2020
779ab6b
Use existing fake mocha types
dgieselaar Nov 11, 2020
a12d3ea
Remove unused type import
dgieselaar Nov 11, 2020
148982d
Fix type errors
dgieselaar Nov 11, 2020
af9e222
Merge branch 'master' of github.com:elastic/kibana into pr/82932
spalger Nov 18, 2020
2825618
unify global ftr types under kbn-test
spalger Nov 18, 2020
78e261a
remove unnecessary arrow -> function convert
spalger Nov 18, 2020
c8583e7
avoid using typescript specific import paths
spalger Nov 18, 2020
0ef5514
Merge branch 'master' of github.com:elastic/kibana into pr/82932
spalger Nov 18, 2020
d7e295d
don't import ftr globals in kbn-test package
spalger Nov 18, 2020
610387f
include the files in ftr projects so that project check associates th…
spalger Nov 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 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 @@ -126,7 +127,16 @@ export function runFtrCli() {
'exclude-tag',
'kibana-install-dir',
],
boolean: ['bail', 'invert', 'test-stats', 'updateBaselines', 'throttle', 'headless'],
boolean: [
'bail',
'invert',
'test-stats',
'updateBaselines',
'updateSnapshots',
'u',
'throttle',
'headless',
],
default: {
config: 'test/functional/config.js',
},
Expand All @@ -141,6 +151,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
Copy link
Contributor

Choose a reason for hiding this comment

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

I definitely reached for the -u flag here, maybe it would be nice to have an --update,-u flag that sets both updateBaselines and updateSnapshots.

Copy link
Member Author

Choose a reason for hiding this comment

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

done - I tried to add -u only where necessary, but let me know if I missed anything.

-u replace both baseline screenshots and snapshots
--kibana-install-dir directory where the Kibana install being tested resides
--throttle enable network throttling in Chrome browser
--headless run browser in headless mode
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