Skip to content

Commit 3e04c17

Browse files
committed
ci: fix the e2e failure, on the flight
1 parent 1cf4b66 commit 3e04c17

File tree

10 files changed

+166
-173
lines changed

10 files changed

+166
-173
lines changed

.fatherrc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ export default defineConfig({
1212
},
1313
},
1414
sourcemap: true,
15-
targets: legacy ? { ie: 11 } /* ES2015 */ : { chrome: 80 } /* ES2020 */,
15+
targets: legacy ? /* ES2015 */ { ie: 11 } : /* ES2020 */ { chrome: 80 },
1616
});

e2e/fixtures/EnumLegacyTest.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

e2e/fixtures/EnumPage.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

e2e/fixtures/EnumTest.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

playwright.config.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const config: PlaywrightTestConfig = {
1010
globalSetup: require.resolve('./e2e/global.setup.ts'),
1111
globalTeardown: require.resolve('./e2e/global.teardown.ts'),
1212
forbidOnly: !!process.env.CI,
13+
workers: 2,
1314
retries: 2,
1415
use: {
1516
baseURL: `http://localhost:${port}`,
@@ -20,37 +21,33 @@ const config: PlaywrightTestConfig = {
2021
{
2122
name: 'chrome',
2223
use: devices['Desktop Chrome'],
23-
workers: 1,
2424
},
2525
{
2626
name: 'firefox',
2727
use: devices['Desktop Firefox'],
28-
workers: 1,
2928
},
3029
{
3130
name: 'webkit',
3231
use: devices['Desktop Safari'],
33-
workers: 1,
3432
},
3533
{
3634
name: 'edge',
3735
use: devices['Desktop Edge'],
38-
workers: 1,
3936
},
4037
// Legacy browsers (simulated)
41-
// {
42-
// name: 'chromium-legacy',
43-
// use: {
44-
// browserName: 'chromium',
45-
// // Use specific flags to simulate legacy browser behavior
46-
// launchOptions: {
47-
// args: ['--js-flags=--noturbo'],
48-
// },
49-
// },
50-
// },
38+
{
39+
name: 'chromium-legacy',
40+
use: {
41+
browserName: 'chromium',
42+
// Use specific flags to simulate legacy browser behavior
43+
launchOptions: {
44+
args: ['--js-flags=--noturbo'],
45+
},
46+
},
47+
},
5148
],
5249
webServer: {
53-
command: `npx http-server ./e2e/fixtures -p ${port}`,
50+
command: `npx serve ./e2e/fixtures -p ${port}`,
5451
port: 7080,
5552
reuseExistingServer: false,
5653
},

src/enum-collection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ export class EnumCollectionClass<
5555
* **CN:** 布尔值,表示这是一个枚举集合实例
5656
*/
5757
readonly [ENUM_COLLECTION] = true;
58-
readonly [Symbol.hasInstance] = function (this: EnumCollectionClass<T, K, V>, instance: unknown): boolean {
58+
[Symbol.hasInstance](this: EnumCollectionClass<T, K, V>, instance: unknown): boolean {
5959
// intentionally use == to support both number and string format value
6060
return this.items.some(
6161
// eslint-disable-next-line eqeqeq
6262
(i) => instance == i.value || instance === i.key
6363
);
64-
};
64+
}
6565
/**
6666
* The enum collection name, supports localization. Note that it usually returns a string, but if
6767
* a custom `localize` function is set, the return value may vary depending on the implementation

src/enum-item.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ export class EnumItemClass<
4242
*
4343
* @returns V | string
4444
*/
45-
readonly [Symbol.toPrimitive] = function (
46-
this: EnumItemClass<T, K, V>,
47-
hint: 'number' | 'string' | 'default'
48-
): V | string {
45+
[Symbol.toPrimitive](this: EnumItemClass<T, K, V>, hint: 'number' | 'string' | 'default'): V | string {
4946
if (hint === 'number') {
5047
// for cases like Number(value) or +value
5148
return this.valueOf();
@@ -55,7 +52,7 @@ export class EnumItemClass<
5552
}
5653
// for cases like '' + value, value == 1
5754
return this.valueOf();
58-
};
55+
}
5956

6057
private _options: EnumItemOptions | undefined;
6158
// should use function here to avoid closure. this is important for the e2e test cases.

test/engines/playwright.ts

Lines changed: 78 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defaultLocalize, Enum } from '@enum-plus';
2-
import { expect, test } from '../../e2e/fixtures/EnumTest';
2+
import type { Page } from '@playwright/test';
3+
import { expect, test } from '@playwright/test';
34
import { getLocales, setLang } from '../data/week-config';
45
import { deserializeJavascript, serializeJavascript } from '../utils/serialize-javascript';
56
import TestEngineBase, { type RuntimeContext } from './base';
@@ -10,68 +11,97 @@ export class PlaywrightEngine extends TestEngineBase {
1011
super();
1112
this._type = 'playwright';
1213
}
14+
override describe(name: string, fn: () => void): void {
15+
test.describe(name, fn);
16+
}
1317

1418
override test<Data = unknown>(
1519
name: string,
1620
evaluate: (context: RuntimeContext) => Data,
1721
assert: (data: Data) => void,
1822
evaluateContext?: Record<string, unknown>
1923
): void {
20-
const evaluateContextStr = serializeJavascript({ ...evaluateContext, evaluateFn: evaluate });
21-
test(`${name} in modern browsers`, async ({ page }) => {
22-
const resultStr = await page.evaluate((contextStr) => {
23-
const EnumPlus = window.EnumPlus;
24-
const WeekConfig = window.WeekConfig;
25-
const WeekData = window.WeekData;
26-
const SerializeJavascript = window.SerializeJavascript;
27-
const runtimeContext = {
28-
EnumPlus,
29-
WeekConfig,
30-
WeekData,
31-
SerializeJavascript,
32-
};
33-
// console.log('window', runtimeContext);
34-
const { serializeJavascript: serialize, deserializeJavascript: deserialize } = SerializeJavascript;
35-
const args = deserialize(contextStr) as { evaluateFn: typeof evaluate };
36-
const { evaluateFn, ...rest } = args;
37-
const evaluateResult = evaluateFn({ ...runtimeContext, ...rest });
38-
// console.log('evaluateResult');
39-
// console.log(evaluateResult);
40-
// save the current lang to the result
41-
const serializedStr = serialize({
42-
EnumLocalize: EnumPlus.Enum.localize,
43-
lang: WeekConfig.lang,
44-
...evaluateResult,
45-
});
46-
// console.log('serialize result');
47-
// console.log(serializeResult);
48-
return serializedStr;
49-
}, evaluateContextStr);
24+
const serializedEvaluateParams = serializeJavascript({ ...evaluateContext, evaluateFn: evaluate });
5025

51-
const initialState = deserializeJavascript(resultStr);
52-
// restore the lang to the Enum.localize
53-
setLang(initialState.lang, Enum, getLocales, defaultLocalize);
54-
if (!initialState.EnumLocalize) {
55-
Enum.localize = undefined!;
56-
}
57-
// the Enum object is used to "help" to access the Enum global function,
58-
// because the code is like `const localize = this._options?.localize ?? Enum.localize;`,
59-
// it seems that Enum is a global variable, but actually it is not, we simulate it as a closure context.
60-
const testResult = deserializeJavascript(resultStr, {
61-
closure: { Enum, ...initialState },
62-
});
63-
// console.log('deserialize result');
64-
// console.log(testResult);
65-
assert(testResult as Data);
26+
// test(`(es) ${name}`, async ({ page }) => {
27+
// await page.goto('/modern.html', { waitUntil: 'domcontentloaded' });
28+
// await page.waitForLoadState('domcontentloaded');
29+
// await this.executeEvaluation({ page, assert, serializedEvaluateParams });
30+
// });
31+
32+
test(`(es-legacy) ${name}`, async ({ page }) => {
33+
await page.goto('/legacy.html', { waitUntil: 'domcontentloaded' });
34+
await page.waitForLoadState('domcontentloaded');
35+
await this.executeEvaluation({ page, assert, serializedEvaluateParams });
6636
});
6737
}
38+
6839
// eslint-disable-next-line @typescript-eslint/ban-types
6940
override expect<ActualType = unknown>(actual: ActualType): MakeMatchers<void, ActualType, {}> {
7041
// eslint-disable-next-line @typescript-eslint/ban-types
7142
return expect(actual) as unknown as MakeMatchers<void, ActualType, {}>;
7243
}
73-
override describe(name: string, fn: () => void): void {
74-
test.describe(name, fn);
44+
45+
// Execute the test evaluation
46+
protected async executeEvaluation<Data = unknown>(options: {
47+
page: Page;
48+
assert: (data: Data) => void;
49+
serializedEvaluateParams: string;
50+
}) {
51+
const { page, assert, serializedEvaluateParams } = options;
52+
const resultStr = await page.evaluate((contextStr) => {
53+
const EnumPlus = window.EnumPlus;
54+
const WeekConfig = window.WeekConfig;
55+
const WeekData = window.WeekData;
56+
const SerializeJavascript = window.SerializeJavascript;
57+
58+
// Deserialize request
59+
const runtimeContext = {
60+
EnumPlus,
61+
WeekConfig,
62+
WeekData,
63+
SerializeJavascript,
64+
};
65+
// console.log('window', runtimeContext);
66+
const { serializeJavascript: serialize, deserializeJavascript: deserialize } = SerializeJavascript;
67+
const args = deserialize(contextStr) as { evaluateFn: (context: RuntimeContext) => Data };
68+
const { evaluateFn, ...rest } = args;
69+
70+
// Set the initial state to en-US before executing evaluation
71+
WeekConfig.setLang('en-US', EnumPlus.Enum, WeekConfig.getLocales, EnumPlus.defaultLocalize);
72+
73+
// Execute the evaluation
74+
const evaluateResult = evaluateFn({ ...runtimeContext, ...rest });
75+
// console.log('evaluateResult');
76+
// console.log(evaluateResult);
77+
78+
// Serialize the evaluation result and pass it to assertion method
79+
const serializedStr = serialize({
80+
EnumLocalize: EnumPlus.Enum.localize,
81+
lang: WeekConfig.lang,
82+
...evaluateResult,
83+
});
84+
// console.log('serialize result');
85+
// console.log(serializeResult);
86+
return serializedStr;
87+
}, serializedEvaluateParams);
88+
89+
const initialState = deserializeJavascript(resultStr, { debug: true, prettyPrint: false });
90+
// Restore the lang to the Enum.localize
91+
setLang(initialState.lang, Enum, getLocales, defaultLocalize);
92+
if (!initialState.EnumLocalize) {
93+
Enum.localize = undefined!;
94+
}
95+
96+
// the Enum object is used to "help" to access the Enum global function,
97+
// because the code is like `const localize = this._options?.localize ?? Enum.localize;`,
98+
// it seems that Enum is a global variable, but actually it is not, we simulate it as a closure context.
99+
const testResult = deserializeJavascript(resultStr, {
100+
closure: { Enum, ...initialState },
101+
});
102+
// console.log('deserialize result');
103+
// console.log(testResult);
104+
assert(testResult as Data);
75105
}
76106
}
77107

test/test-suites/enum-item.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ const testEnumItem = (engine: TestEngineBase) => {
8686
({ localeEN, sunday, monday, tuesday, friday, saturday }) => {
8787
engine.expect(Number(sunday)).toBe(0);
8888
engine.expect(String(sunday)).toBe(localeEN.Sunday);
89-
// engine.expect(Boolean(sunday)).toBe(false);
90-
// engine.expect(Boolean(monday)).toBe(true);
89+
engine.expect(Boolean(sunday)).toBe(true);
90+
engine.expect(Boolean(monday)).toBe(true);
9191

9292
engine.expect(saturday > friday).toBeTruthy();
9393
engine.expect(monday < tuesday).toBeTruthy();

0 commit comments

Comments
 (0)