Skip to content

Instantly share code, notes, and snippets.

@kylefritz
Created July 20, 2020 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylefritz/24ce374e019a0fa976ccbdb8668aa8b3 to your computer and use it in GitHub Desktop.
Save kylefritz/24ce374e019a0fa976ccbdb8668aa8b3 to your computer and use it in GitHub Desktop.
jest mocking
require("../configure_enzyme");
//
// setup a mock for a function that depends on the current system time
//
import { pastDeadline } from "menu/pastDeadline";
jest.mock("menu/pastDeadline", () => {
let areWePastDeadline = true;
function __setPastDeadline(newValue) {
areWePastDeadline = newValue;
}
return { __setPastDeadline, pastDeadline: () => areWePastDeadline };
});
import { __setPastDeadline } from "menu/pastDeadline";
test("__setPastDeadline back and forth in same test", () => {
__setPastDeadline(false);
expect(pastDeadline()).toBe(false);
__setPastDeadline(true);
expect(pastDeadline()).toBe(true);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment