Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(clock): make fake Intl.DateTimeFormat create native instances (#43523)

Yury Semikhatsky88ed60ceef54
diff --git a/packages/injected/src/clock.ts b/packages/injected/src/clock.ts
index c73fd2a763..0c00941bf3 100644
--- a/packages/injected/src/clock.ts
+++ b/packages/injected/src/clock.ts
@@ -562,22 +562,32 @@ function createIntl(clock: ClockController, NativeIntl: Builtins['Intl']): Built
for (const key of Object.getOwnPropertyNames(NativeIntl) as (keyof Builtins['Intl'])[])
ClockIntl[key] = NativeIntl[key];
+ // Formatters are native instances with a prototype that has the same own members as
+ // the native one, except that format() and formatToParts() default to the clock time.
+ const nativePrototype = NativeIntl.DateTimeFormat.prototype;
+ const nativeFormat = Object.getOwnPropertyDescriptor(nativePrototype, 'format')!.get!;
+ const nativeFormatToParts = nativePrototype.formatToParts;
ClockIntl.DateTimeFormat = function(...args: any[]) {
- const realFormatter = new NativeIntl.DateTimeFormat(...args);
- const formatter: Intl.DateTimeFormat = {
- formatRange: realFormatter.formatRange.bind(realFormatter),
- formatRangeToParts: realFormatter.formatRangeToParts.bind(realFormatter),
- resolvedOptions: realFormatter.resolvedOptions.bind(realFormatter),
- format: date => realFormatter.format(date === undefined ? clock.now() : date),
- formatToParts: date => realFormatter.formatToParts(date === undefined ? clock.now() : date),
- };
-
- return formatter;
+ return Reflect.construct(NativeIntl.DateTimeFormat, args, new.target ?? ClockIntl.DateTimeFormat);
};
-
- ClockIntl.DateTimeFormat.prototype = Object.create(
- NativeIntl.DateTimeFormat.prototype,
- );
+ ClockIntl.DateTimeFormat.prototype = Object.create(nativePrototype, {
+ ...Object.getOwnPropertyDescriptors(nativePrototype),
+ constructor: { value: ClockIntl.DateTimeFormat, writable: true, configurable: true },
+ format: {
+ get(this: Intl.DateTimeFormat) {
+ const format = nativeFormat.call(this);
+ return (date?: Date | number) => format(date === undefined ? clock.now() : date);
+ },
+ configurable: true,
+ },
+ formatToParts: {
+ value(this: Intl.DateTimeFormat, date?: Date | number) {
+ return nativeFormatToParts.call(this, date === undefined ? clock.now() : date);
+ },
+ writable: true,
+ configurable: true,
+ },
+ });
ClockIntl.DateTimeFormat.supportedLocalesOf =
NativeIntl.DateTimeFormat.supportedLocalesOf;
diff --git a/tests/library/unit/clock.spec.ts b/tests/library/unit/clock.spec.ts
index e782a2367a..f5ed0a499c 100644
--- a/tests/library/unit/clock.spec.ts
+++ b/tests/library/unit/clock.spec.ts
@@ -1665,6 +1665,11 @@ it.describe('Intl API', () => {
// A timestamp of 0 is the epoch, not "use the system time".
// https://github.com/microsoft/playwright/issues/43424
expect(isFirstOfMonth('America/Toronto', 0)).toBeFalsy();
+ // Formatters are real Intl.DateTimeFormat instances and the prototype keeps its own members.
+ // https://github.com/microsoft/playwright/issues/43492
+ const formatter = new Intl.DateTimeFormat('en-GB', { timeZone: 'UTC' });
+ expect(formatter).toBeInstanceOf(Intl.DateTimeFormat);
+ expect(Object.getOwnPropertyDescriptor(Intl.DateTimeFormat.prototype, 'format').get.call(formatter)()).toBe('01/06/2022');
});
it('CK-215 formatToParts via isFirstOfMonth -> Returns false when passed no timestamp and system time is not first of the month', async ({ install }) => {