mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-19 14:52:40 +08:00
Optimize unit test; support overriding constants in per-test-suite (#15628)
* Optimize unit test; support overriding constants in per-test-suite
This commit is contained in:
committed by
GitHub
parent
ff40e7402e
commit
a3ee06e374
+18
-3
@@ -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;
|
||||
|
||||
+21
-10
@@ -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<typeof JSDOMEnvironment>) {
|
||||
super(...args);
|
||||
|
||||
const promises = this._promises;
|
||||
Object.defineProperty(this.global, 'waitThis', {
|
||||
get: () => {
|
||||
return (promise: Promise<unknown>) => {
|
||||
promises.push(promise);
|
||||
};
|
||||
},
|
||||
});
|
||||
const [_, context] = args;
|
||||
this._testPath = context.testPath;
|
||||
|
||||
this.global.__cc_test__ = {
|
||||
currentTestSuiteConfig: {},
|
||||
};
|
||||
this._readTestSuiteConfig();
|
||||
}
|
||||
|
||||
async setup(...args: Parameters<JSDOMEnvironment['setup']>) {
|
||||
await super.setup(...args);
|
||||
await Promise.all(this._promises);
|
||||
}
|
||||
|
||||
private _promises: Promise<unknown>[] = [];
|
||||
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;
|
||||
@@ -0,0 +1,8 @@
|
||||
/// <reference path="../../@types/consts.d.ts"/>
|
||||
|
||||
import { SUPPORT_JIT, BUILD } from 'internal:constants';
|
||||
|
||||
test(`Constant overrides`, () => {
|
||||
expect(SUPPORT_JIT).toBe(true);
|
||||
expect(BUILD).toBe(true);
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"constantOverrides": {
|
||||
"SUPPORT_JIT": true,
|
||||
"BUILD": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
|
||||
export interface TestSuiteConfig {
|
||||
constantOverrides?: Record<string, boolean>;
|
||||
}
|
||||
|
||||
declare global {
|
||||
namespace __cc_test__ {
|
||||
let currentTestSuiteConfig: TestSuiteConfig;
|
||||
}
|
||||
}
|
||||
|
||||
export function getCurrentTestSuiteConfig() {
|
||||
return __cc_test__.currentTestSuiteConfig;
|
||||
}
|
||||
Reference in New Issue
Block a user