|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2020 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/* eslint-disable no-restricted-properties */ |
| 19 | + |
| 20 | +import { USE_EMULATOR } from '../integration/util/settings'; |
| 21 | + |
| 22 | +// Helper to make a type itselt (T) and optionally union that with (T['skip']) |
| 23 | +type tOrSkipT<T> = T | (T extends { skip: unknown } ? T['skip'] : T); |
| 24 | + |
| 25 | +interface ExtendMochaTypeWithHelpers<T> { |
| 26 | + // Declare helpers |
| 27 | + skipEmulator: tOrSkipT<T>; |
| 28 | + skipEnterprise: tOrSkipT<T>; |
| 29 | + skipClassic: tOrSkipT<T>; |
| 30 | +} |
| 31 | + |
| 32 | +declare module 'mocha' { |
| 33 | + // TODO add mocha types that must be extended |
| 34 | + interface TestFunction extends ExtendMochaTypeWithHelpers<TestFunction> {} |
| 35 | + interface PendingTestFunction |
| 36 | + extends ExtendMochaTypeWithHelpers<PendingTestFunction> {} |
| 37 | + interface SuiteFunction extends ExtendMochaTypeWithHelpers<SuiteFunction> {} |
| 38 | + interface PendingSuiteFunction |
| 39 | + extends ExtendMochaTypeWithHelpers<PendingTestFunction> {} |
| 40 | +} |
| 41 | + |
| 42 | +// Define helpers |
| 43 | +export function mixinSkipImplementations(obj: unknown): void { |
| 44 | + Object.defineProperty(obj, 'skipEmulator', { |
| 45 | + get(): unknown { |
| 46 | + if (this === it.skip) { |
| 47 | + return this; |
| 48 | + } |
| 49 | + if (this === describe.skip) { |
| 50 | + return this; |
| 51 | + } |
| 52 | + if (USE_EMULATOR) { |
| 53 | + return this.skip; |
| 54 | + } |
| 55 | + return this; |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + Object.defineProperty(obj, 'skipEnterprise', { |
| 60 | + get(): unknown { |
| 61 | + if (this === it.skip) { |
| 62 | + return this; |
| 63 | + } |
| 64 | + if (this === describe.skip) { |
| 65 | + return this; |
| 66 | + } |
| 67 | + if (process.env.RUN_ENTERPRISE_TESTS) { |
| 68 | + return this.skip; |
| 69 | + } |
| 70 | + return this; |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + Object.defineProperty(obj, 'skipClassic', { |
| 75 | + get(): unknown { |
| 76 | + if (this === it.skip) { |
| 77 | + return this; |
| 78 | + } |
| 79 | + if (this === describe.skip) { |
| 80 | + return this; |
| 81 | + } |
| 82 | + if (!process.env.RUN_ENTERPRISE_TESTS) { |
| 83 | + return this.skip; |
| 84 | + } |
| 85 | + return this; |
| 86 | + } |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +// TODO add mocha functions that must be extended |
| 91 | +[global.it, global.it.skip, global.describe, global.describe.skip].forEach( |
| 92 | + mixinSkipImplementations |
| 93 | +); |
| 94 | + |
| 95 | +// Export modified it and describe. |
| 96 | +const it = global.it; |
| 97 | +const describe = global.describe; |
| 98 | +export { it, describe }; |
0 commit comments