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

Add API to enable/disable debug logging (in development) #35

Merged
merged 7 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
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
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@
},
"devDependencies": {
"@babel/preset-typescript": "^7.21.5",
"@parcel/config-default": "^2.9.1",
"@parcel/core": "^2.9.1",
"@parcel/transformer-js": "^2.9.1",
"@parcel/transformer-react-refresh-wrap": "^2.9.1",
"@parcel/config-default": "^2.9.2",
"@parcel/core": "^2.9.2",
"@parcel/packager-ts": "2.9.2",
"@parcel/transformer-js": "^2.9.2",
"@parcel/transformer-react-refresh-wrap": "^2.9.2",
"@parcel/transformer-typescript-types": "2.9.2",
"@preconstruct/cli": "^2.7.0",
"@types/jest": "^29.4.0",
"@types/node": "^18.14.6",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.10",
"@types/react-virtualized-auto-sizer": "^1.0.1",
"parcel": "^2.9.1",
"parcel": "^2.9.2",
"path-browserify": "^1.0.0",
"prettier": "latest",
"process": "^0.11.10",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type Metadata = {
};

const streamingCache = createStreamingCache<[Post[]], Post[], Metadata>({
getKey: (posts) => `${posts.map((post) => post.id).join(":")}`,
load: async (
options: StreamingCacheLoadOptions<Post[], Metadata>,
posts: Post[]
Expand Down
6 changes: 5 additions & 1 deletion packages/suspense/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Changelog

## 0.0.40
* Added opt-in debug logging per cache (`enableDebugLogging` config) as well as globally (`enableDebugLogging` export) for development builds.
* Add (DEV-only) warning to `createIntervalCache` and `createStreamingCache` for non-unique cache keys– specifically warning about the key containing an `Object` case as a string.

## 0.0.39
* Add (DEV-only) warning for non-unique cache keys– specifically warning about the key containing an `Object` case as a string.
* Add (DEV-only) warning to `createCache` for non-unique cache keys– specifically warning about the key containing an `Object` case as a string.

## 0.0.38
* [32](https://github.com/bvaughn/suspense/pull/32): Build release bundle with Preconstruct
Expand Down
68 changes: 59 additions & 9 deletions packages/suspense/src/cache/createCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ function defaultLoad(

describe("createCache", () => {
let cache: Cache<[string], string>;
let load: jest.Mock<Promise<string> | string, [[string], CacheLoadOptions]>;
let getCacheKey: jest.Mock<string, [[string]]>;
let load: jest.Mock<Promise<string> | string, [[string], CacheLoadOptions]>;

beforeEach(() => {
load = jest.fn();
Expand All @@ -43,6 +43,10 @@ describe("createCache", () => {
});
});

afterEach(() => {
jest.restoreAllMocks();
});

async function fakeSuspend(read: () => any) {
try {
return read();
Expand Down Expand Up @@ -732,25 +736,71 @@ describe("createCache", () => {
});
});

describe("development warnings", () => {
describe("development mode", () => {
it("should warn if a key contains a stringified object", async () => {
const cache = createCache<[Object, string], boolean>({
getKey: ([object, id]) => `${object}-${id}`,
load: ([object, id]) => true,
});

jest.spyOn(console, "warn").mockImplementation(() => {});

cache.read({}, "one");
getCacheKey.mockImplementation((string) => `${{ string }}`);

cache.readAsync("one");

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(
expect.stringMatching("contains a stringified object")
);

// Only warn once per cache though
cache.read({}, "two");
cache.readAsync("two");
expect(console.warn).toHaveBeenCalledTimes(1);
});

it("logs debug messages to console", () => {
const consoleMock = jest
.spyOn(console, "log")
.mockImplementation(() => {});

cache = createCache<[string], string>({
debugLabel: "test-cache",
debugLogging: true,
getKey: getCacheKey,
load,
});
console.log(consoleMock.mock.calls);
expect(consoleMock).toHaveBeenCalled();
expect(consoleMock.mock.calls[0]).toEqual(
expect.arrayContaining([
expect.stringContaining("test-cache"),
expect.stringContaining("Cache created"),
])
);

consoleMock.mockClear();
cache.readAsync("one");
expect(consoleMock).toHaveBeenCalled();
expect(consoleMock.mock.calls[0]).toEqual(
expect.arrayContaining([
expect.stringContaining("test-cache"),
expect.stringContaining("read"),
expect.stringContaining("one"),
])
);

consoleMock.mockClear();
cache.disableDebugLogging();
cache.readAsync("two");
expect(consoleMock).not.toHaveBeenCalled();

consoleMock.mockClear();
cache.enableDebugLogging();
cache.readAsync("three");
expect(consoleMock).toHaveBeenCalled();
expect(consoleMock.mock.calls[0]).toEqual(
expect.arrayContaining([
expect.stringContaining("test-cache"),
expect.stringContaining("read"),
expect.stringContaining("three"),
])
);
});
});
});
Loading