|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import fs from 'fs'; |
| 21 | +import { join, resolve } from 'path'; |
| 22 | + |
| 23 | +jest.mock('fs'); |
| 24 | +jest.mock('@kbn/dev-utils', () => { |
| 25 | + return { REPO_ROOT: '/dev/null/root' }; |
| 26 | +}); |
| 27 | + |
| 28 | +import { REPO_ROOT } from '@kbn/dev-utils'; |
| 29 | +import { Lifecycle } from './lifecycle'; |
| 30 | +import { SuiteTracker } from './suite_tracker'; |
| 31 | + |
| 32 | +const DEFAULT_TEST_METADATA_PATH = join(REPO_ROOT, 'target', 'test_metadata.json'); |
| 33 | +const MOCK_CONFIG_PATH = join('test', 'config.js'); |
| 34 | +const MOCK_TEST_PATH = join('test', 'apps', 'test.js'); |
| 35 | +const ENVS_TO_RESET = ['TEST_METADATA_PATH']; |
| 36 | + |
| 37 | +describe('SuiteTracker', () => { |
| 38 | + const originalEnvs: Record<string, string> = {}; |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + for (const env of ENVS_TO_RESET) { |
| 42 | + if (env in process.env) { |
| 43 | + originalEnvs[env] = process.env[env] || ''; |
| 44 | + delete process.env[env]; |
| 45 | + } |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + for (const env of ENVS_TO_RESET) { |
| 51 | + delete process.env[env]; |
| 52 | + } |
| 53 | + |
| 54 | + for (const env of Object.keys(originalEnvs)) { |
| 55 | + process.env[env] = originalEnvs[env]; |
| 56 | + } |
| 57 | + |
| 58 | + jest.resetAllMocks(); |
| 59 | + }); |
| 60 | + |
| 61 | + let MOCKS: Record<string, object>; |
| 62 | + |
| 63 | + const createMock = (overrides = {}) => { |
| 64 | + return { |
| 65 | + file: resolve(REPO_ROOT, MOCK_TEST_PATH), |
| 66 | + title: 'A Test', |
| 67 | + suiteTag: MOCK_TEST_PATH, |
| 68 | + ...overrides, |
| 69 | + }; |
| 70 | + }; |
| 71 | + |
| 72 | + const runLifecycleWithMocks = async (mocks: object[], fn: (objs: any) => any = () => {}) => { |
| 73 | + const lifecycle = new Lifecycle(); |
| 74 | + const suiteTracker = SuiteTracker.startTracking( |
| 75 | + lifecycle, |
| 76 | + resolve(REPO_ROOT, MOCK_CONFIG_PATH) |
| 77 | + ); |
| 78 | + |
| 79 | + const ret = { lifecycle, suiteTracker }; |
| 80 | + |
| 81 | + for (const mock of mocks) { |
| 82 | + await lifecycle.beforeTestSuite.trigger(mock); |
| 83 | + } |
| 84 | + |
| 85 | + if (fn) { |
| 86 | + fn(ret); |
| 87 | + } |
| 88 | + |
| 89 | + for (const mock of mocks.reverse()) { |
| 90 | + await lifecycle.afterTestSuite.trigger(mock); |
| 91 | + } |
| 92 | + |
| 93 | + return ret; |
| 94 | + }; |
| 95 | + |
| 96 | + beforeEach(() => { |
| 97 | + MOCKS = { |
| 98 | + WITH_TESTS: createMock({ tests: [{}] }), // i.e. a describe with tests in it |
| 99 | + WITHOUT_TESTS: createMock(), // i.e. a describe with only other describes in it |
| 100 | + }; |
| 101 | + }); |
| 102 | + |
| 103 | + it('collects metadata for a single suite with multiple describe()s', async () => { |
| 104 | + const { suiteTracker } = await runLifecycleWithMocks([MOCKS.WITHOUT_TESTS, MOCKS.WITH_TESTS]); |
| 105 | + |
| 106 | + const suites = suiteTracker.getAllFinishedSuites(); |
| 107 | + expect(suites.length).toBe(1); |
| 108 | + const suite = suites[0]; |
| 109 | + |
| 110 | + expect(suite).toMatchObject({ |
| 111 | + config: MOCK_CONFIG_PATH, |
| 112 | + file: MOCK_TEST_PATH, |
| 113 | + tag: MOCK_TEST_PATH, |
| 114 | + hasTests: true, |
| 115 | + success: true, |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('writes metadata to a file when cleanup is triggered', async () => { |
| 120 | + const { lifecycle, suiteTracker } = await runLifecycleWithMocks([MOCKS.WITH_TESTS]); |
| 121 | + await lifecycle.cleanup.trigger(); |
| 122 | + |
| 123 | + const suites = suiteTracker.getAllFinishedSuites(); |
| 124 | + |
| 125 | + const call = (fs.writeFileSync as jest.Mock).mock.calls[0]; |
| 126 | + expect(call[0]).toEqual(DEFAULT_TEST_METADATA_PATH); |
| 127 | + expect(call[1]).toEqual(JSON.stringify(suites, null, 2)); |
| 128 | + }); |
| 129 | + |
| 130 | + it('respects TEST_METADATA_PATH env var for metadata target override', async () => { |
| 131 | + process.env.TEST_METADATA_PATH = resolve(REPO_ROOT, '../fake-test-path'); |
| 132 | + const { lifecycle } = await runLifecycleWithMocks([MOCKS.WITH_TESTS]); |
| 133 | + await lifecycle.cleanup.trigger(); |
| 134 | + |
| 135 | + expect((fs.writeFileSync as jest.Mock).mock.calls[0][0]).toEqual( |
| 136 | + process.env.TEST_METADATA_PATH |
| 137 | + ); |
| 138 | + }); |
| 139 | + |
| 140 | + it('identifies suites with tests as leaf suites', async () => { |
| 141 | + const root = createMock({ title: 'root', file: join(REPO_ROOT, 'root.js') }); |
| 142 | + const parent = createMock({ parent: root }); |
| 143 | + const withTests = createMock({ parent, tests: [{}] }); |
| 144 | + |
| 145 | + const { suiteTracker } = await runLifecycleWithMocks([root, parent, withTests]); |
| 146 | + const suites = suiteTracker.getAllFinishedSuites(); |
| 147 | + |
| 148 | + const finishedRoot = suites.find(s => s.title === 'root'); |
| 149 | + const finishedWithTests = suites.find(s => s.title !== 'root'); |
| 150 | + |
| 151 | + expect(finishedRoot).toBeTruthy(); |
| 152 | + expect(finishedRoot?.hasTests).toBeFalsy(); |
| 153 | + expect(finishedWithTests?.hasTests).toBe(true); |
| 154 | + }); |
| 155 | + |
| 156 | + describe('with a failing suite', () => { |
| 157 | + let root: any; |
| 158 | + let parent: any; |
| 159 | + let failed: any; |
| 160 | + |
| 161 | + beforeEach(() => { |
| 162 | + root = createMock({ file: join(REPO_ROOT, 'root.js') }); |
| 163 | + parent = createMock({ parent: root }); |
| 164 | + failed = createMock({ parent, tests: [{}] }); |
| 165 | + }); |
| 166 | + |
| 167 | + it('marks parent suites as not successful when a test fails', async () => { |
| 168 | + const { suiteTracker } = await runLifecycleWithMocks( |
| 169 | + [root, parent, failed], |
| 170 | + async ({ lifecycle }) => { |
| 171 | + await lifecycle.testFailure.trigger(Error('test'), { parent: failed }); |
| 172 | + } |
| 173 | + ); |
| 174 | + |
| 175 | + const suites = suiteTracker.getAllFinishedSuites(); |
| 176 | + expect(suites.length).toBe(2); |
| 177 | + for (const suite of suites) { |
| 178 | + expect(suite.success).toBeFalsy(); |
| 179 | + } |
| 180 | + }); |
| 181 | + |
| 182 | + it('marks parent suites as not successful when a test hook fails', async () => { |
| 183 | + const { suiteTracker } = await runLifecycleWithMocks( |
| 184 | + [root, parent, failed], |
| 185 | + async ({ lifecycle }) => { |
| 186 | + await lifecycle.testHookFailure.trigger(Error('test'), { parent: failed }); |
| 187 | + } |
| 188 | + ); |
| 189 | + |
| 190 | + const suites = suiteTracker.getAllFinishedSuites(); |
| 191 | + expect(suites.length).toBe(2); |
| 192 | + for (const suite of suites) { |
| 193 | + expect(suite.success).toBeFalsy(); |
| 194 | + } |
| 195 | + }); |
| 196 | + }); |
| 197 | +}); |
0 commit comments