Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an example of how to mock singletons in jest tests #14

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions src/react-render-perf/lesson-03/content.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,53 @@ While singletons are sometimes frowned upon because they can make testing more
difficult in some languages, that isn't the case in JavaScript. It's easy to mock
the singleton using <code>jest</code> if follow these guidelines:

- Export the singleton from its own file. TODO(kevinb): provide an example of how to
mock the singleton.
- If you have a custom class instead of using <code>EventEmitter</code> be sure to export
that as well so that you can use it when mocking the singleton.
- Instead of exporting the singleton directly, export a function that returns the
singleton. This will allow you to mock the singleton on a per-test basis.

```ts
// my-awesome-event-emitter.ts
export class MyAwesomeEventEmitter {
...
}

// This is our singleton. Note that it isn't being exported.
// Instead, we want people to use the function below to get
// the singleton.
const myAwesomeEventEmitter = new MyAwesomeEventEmitter();

export const getMyAwesomeEventEmitter = () => myAwesomeEventEmitter;
```

When writing tests you import both the class and the singleton getter into your test
file to help with mocking.

```ts
// foo.test.ts
import * as MyAwesomeEventEmitter from "../my-awesome-event-emitter"

describe("Foo", () => {
it("should function as expected", () => {
// Arrange
const mockEmitter = new MyAwesomeEventEmitter();
// modify the state of mockEmitter if necessary.
jest.spyOn(
MyAwesomeEventEmitter, "getMyAwesomeEventEmitter"
).mockReturnValue(
mockEmitter,
);
// Now the code under test will be using `mockEmitter` instead of
// the `myAwesomeEventEmitter` that we created in my-awesome-event-emitter.ts.

// Act
// ...

// Assert
// ...
});
});
```

### State

Expand Down
Loading