Skip to content

Commit

Permalink
fix: avoid erroring when reading instance in ssr mode
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Apr 22, 2022
1 parent 1f0f496 commit 2f5fcc0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
12 changes: 5 additions & 7 deletions src/__tests__/render.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ test("local cleanup removes single component from the document", async () => {
expect(() => cleanup()).toThrowErrorMatchingSnapshot();
});

test("instance is undefined on the server", async () => {
const result = await render(Clickable);
expect(result.instance).toBeUndefined();
});

test("fails when rerendering", async () => {
const { rerender } = await render(HelloName, { name: "Michael" });
await expect(
Expand Down Expand Up @@ -81,10 +86,3 @@ test("fails when emitting events", async () => {
`"Cannot perform client side interaction tests on the server side. Please use @marko/testing-library in a browser environment."`
);
});

test("fails when trying to read instance", async () => {
const result = await render(Clickable);
expect(() => result.instance).toThrowErrorMatchingInlineSnapshot(
`"Cannot access component instance for server side tests."`
);
});
2 changes: 1 addition & 1 deletion src/index-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export async function render<T extends Template>(
};

return {
...within(container as any as HTMLElement),
container,
instance,
emitted<N extends string = "*">(
Expand Down Expand Up @@ -133,6 +132,7 @@ export async function render<T extends Template>(
logDOM(element, maxLength, options);
}
},
...within(container as any as HTMLElement),
} as const;
}

Expand Down
8 changes: 2 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,8 @@ export async function render<T extends Template>(
Object.assign(screen, queries);

return {
...queries,
container,
get instance(): any {
throw new Error(
"Cannot access component instance for server side tests."
);
},
instance: undefined as any,
emitted<N extends string = "*">(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type?: N extends InternalEventNames ? never : N
Expand All @@ -111,6 +106,7 @@ export async function render<T extends Template>(

cleanupComponent();
},
...queries,
} as const;
}

Expand Down

0 comments on commit 2f5fcc0

Please sign in to comment.