From a3ee06e374a947fa8ff45e707fb5065c94dd811c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leslie=20Leigh=20=28=E6=9D=8E=E7=9A=84=E5=BA=8F=29?= Date: Thu, 6 Jul 2023 18:00:03 +0800 Subject: [PATCH] Optimize unit test; support overriding constants in per-test-suite (#15628) * Optimize unit test; support overriding constants in per-test-suite --- tests/init.ts | 21 +++++++++++-- tests/test-environment.ts | 31 +++++++++++++------ tests/utils/test-suite-config.test.ts | 8 +++++ .../test-suite-config.test.ts.config.json | 6 ++++ tests/utils/test-suite-config.ts | 14 +++++++++ 5 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 tests/utils/test-suite-config.test.ts create mode 100644 tests/utils/test-suite-config.test.ts.config.json create mode 100644 tests/utils/test-suite-config.ts diff --git a/tests/init.ts b/tests/init.ts index 15d492a896..6f321dca9f 100644 --- a/tests/init.ts +++ b/tests/init.ts @@ -1,6 +1,18 @@ jest.mock( 'internal:constants', - () => jest.requireActual('./constants-for-test'), + () => { + const actual = jest.requireActual('./constants-for-test'); + const { getCurrentTestSuiteConfig } = jest.requireActual('./utils/test-suite-config') as + typeof import('./utils/test-suite-config'); + const config = getCurrentTestSuiteConfig(); + if (!config.constantOverrides) { + return actual; + } + return { + ...actual, + ...config.constantOverrides, + }; + }, { virtual: true, }, ); @@ -171,7 +183,8 @@ const config: IGameConfig = { } } } -globalThis.waitThis((async () => { + +async function bootstrap() { game.on(Game.EVENT_POST_SUBSYSTEM_INIT, () => { effects.forEach((e, effectIndex) => { const effect = Object.assign(new EffectAsset(), e); @@ -190,4 +203,6 @@ globalThis.waitThis((async () => { initBuiltinPhysicsMaterial(); await game.init(config); await game.run(); -})()); +} + +module.exports = bootstrap; diff --git a/tests/test-environment.ts b/tests/test-environment.ts index 1b8afc3cad..07ad7b3271 100644 --- a/tests/test-environment.ts +++ b/tests/test-environment.ts @@ -1,25 +1,36 @@ import JSDOMEnvironment from 'jest-environment-jsdom'; +import fs from 'fs-extra'; +import type { TestSuiteConfig } from './utils/test-suite-config'; class Environment extends JSDOMEnvironment { constructor(...args: ConstructorParameters) { super(...args); - const promises = this._promises; - Object.defineProperty(this.global, 'waitThis', { - get: () => { - return (promise: Promise) => { - promises.push(promise); - }; - }, - }); + const [_, context] = args; + this._testPath = context.testPath; + + this.global.__cc_test__ = { + currentTestSuiteConfig: {}, + }; + this._readTestSuiteConfig(); } async setup(...args: Parameters) { await super.setup(...args); - await Promise.all(this._promises); } - private _promises: Promise[] = []; + private _testPath: string; + + private _readTestSuiteConfig() { + const configPath = `${this._testPath}.config.json`; + if (!fs.pathExistsSync(configPath)) { + return; + } + const config = fs.readJsonSync(configPath) as TestSuiteConfig; + this.global.__cc_test__ = { + currentTestSuiteConfig: config, + }; + } } export default Environment; \ No newline at end of file diff --git a/tests/utils/test-suite-config.test.ts b/tests/utils/test-suite-config.test.ts new file mode 100644 index 0000000000..37be32da23 --- /dev/null +++ b/tests/utils/test-suite-config.test.ts @@ -0,0 +1,8 @@ +/// + +import { SUPPORT_JIT, BUILD } from 'internal:constants'; + +test(`Constant overrides`, () => { + expect(SUPPORT_JIT).toBe(true); + expect(BUILD).toBe(true); +}); \ No newline at end of file diff --git a/tests/utils/test-suite-config.test.ts.config.json b/tests/utils/test-suite-config.test.ts.config.json new file mode 100644 index 0000000000..0b434439bd --- /dev/null +++ b/tests/utils/test-suite-config.test.ts.config.json @@ -0,0 +1,6 @@ +{ + "constantOverrides": { + "SUPPORT_JIT": true, + "BUILD": true + } +} \ No newline at end of file diff --git a/tests/utils/test-suite-config.ts b/tests/utils/test-suite-config.ts new file mode 100644 index 0000000000..2e1d3dbad4 --- /dev/null +++ b/tests/utils/test-suite-config.ts @@ -0,0 +1,14 @@ + +export interface TestSuiteConfig { + constantOverrides?: Record; +} + +declare global { + namespace __cc_test__ { + let currentTestSuiteConfig: TestSuiteConfig; + } +} + +export function getCurrentTestSuiteConfig() { + return __cc_test__.currentTestSuiteConfig; +}