faker: render Date outputs as ISO strings

This commit is contained in:
Gregory Schier
2026-02-21 07:24:07 -08:00
parent c62db7be06
commit f5727b28c4
2 changed files with 15 additions and 0 deletions

View File

@@ -29,6 +29,7 @@ const modules = [
function normalizeResult(result: unknown): string {
if (typeof result === 'string') return result;
if (result instanceof Date) return result.toISOString();
return JSON.stringify(result);
}

View File

@@ -9,4 +9,18 @@ describe('template-function-faker', () => {
// accidental additions, removals, or renames across faker upgrades.
expect(names).toMatchSnapshot();
});
it('renders date results as unquoted ISO strings', async () => {
const { plugin } = await import('../src/index');
const fn = plugin.templateFunctions?.find((fn) => fn.name === 'faker.date.future');
expect(fn?.onRender).toBeTypeOf('function');
const result = await fn!.onRender!(
{} as Parameters<NonNullable<typeof fn.onRender>>[0],
{ values: {} } as Parameters<NonNullable<typeof fn.onRender>>[1],
);
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/);
});
});