Skip to content

Commit

Permalink
Fix occasional type errors when running tests (#935)
Browse files Browse the repository at this point in the history
I have noticed that locally `yarn test` will succeed the first time,
the will fail with a type error saying `toNeverResolve` is undefined.
After running `yarn jest --clearCache`, tests will pass again once then
fail again.

This custom Jest matcher was added in #930. Seemingly the way the type
was added is causing this problem.

I'm not entirely sure what's going on here. Something about the way
Jest is loading this file is making Jest confused about this type.
I found that it works more reliably if the type is moved to a separate
file in the `types` directory.
  • Loading branch information
Gudahtt authored and MajorLift committed Oct 11, 2023
1 parent a5032bc commit e07f603
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
12 changes: 0 additions & 12 deletions tests/setupAfterEnv.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
declare global {
// Using `namespace` here is okay because this is how the Jest types are
// defined.
/* eslint-disable-next-line @typescript-eslint/no-namespace */
namespace jest {
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/ban-types
interface Matchers<R, T = {}> {
toNeverResolve(): Promise<R>;
}
}
}

const UNRESOLVED = Symbol('timedOut');
// Store this in case it gets stubbed later
const originalSetTimeout = global.setTimeout;
Expand Down
14 changes: 14 additions & 0 deletions types/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare global {
// Using `namespace` here is okay because this is how the Jest types are
// defined.
/* eslint-disable-next-line @typescript-eslint/no-namespace */
namespace jest {
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/ban-types
interface Matchers<R, T = {}> {
toNeverResolve(): Promise<R>;
}
}
}

// Export something so that TypeScript knows to interpret this as a module
export {};

0 comments on commit e07f603

Please sign in to comment.