Skip to content

Commit

Permalink
fix: Fix factories defaulting tests when sinon is used.
Browse files Browse the repository at this point in the history
When sinon's fake timers are installed, `global.Date` is mutated to
its own `ClockDate` function, which breaks our equality check to the
`field.type`, which will have captured the original `global.Date`
value.
  • Loading branch information
stephenh committed May 3, 2024
1 parent b41bb99 commit 0d8e850
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/orm/src/newTestInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,11 @@ function getTestId<T extends Entity>(em: EntityManager, entity: T): string {
return tagId(meta, String(sameType.indexOf(entity) + 1));
}

// We keep a local copy of `global.Date`, in case a faking library
// like @sinonjs/fake-timers is used and rewrites `global.Date` to
// their own `ClockDate`, which would make our `===` check below fail.
const globalDate = global.Date;

function defaultValueForField(em: EntityManager, cstr: EntityConstructor<any>, field: PrimitiveField): unknown {
if (field.type === "string") {
if (field.fieldName === "name") {
Expand All @@ -555,7 +560,7 @@ function defaultValueForField(em: EntityManager, cstr: EntityConstructor<any>, f
return 0;
} else if (field.type === "bigint") {
return 0n;
} else if (field.type === Date) {
} else if (field.type === globalDate) {
return testDate;
} else if (field.type === "boolean") {
return false;
Expand Down

0 comments on commit 0d8e850

Please sign in to comment.