Skip to content

Commit 0fd1e22

Browse files
committed
feat: expose instance property from render
1 parent 18952cd commit 0fd1e22

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/render.server.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ test("fails when rerendering", async () => {
4646
await expect(
4747
rerender({ name: "Dylan" })
4848
).rejects.toThrowErrorMatchingInlineSnapshot(
49-
`"Components cannot re-render on the server side"`
49+
`"Components cannot re-render on the server side."`
5050
);
5151
});
5252

5353
test("fails when checking emitted events", async () => {
5454
const { emitted } = await render(Clickable);
5555
expect(() => emitted("button-click")).toThrowErrorMatchingInlineSnapshot(
56-
`"Components should not emit events on the server side"`
56+
`"Components should not emit events on the server side."`
5757
);
5858
});
5959

@@ -74,3 +74,10 @@ test("fails when emitting events", async () => {
7474
`"Cannot perform client side interaction tests on the server side. Please use @marko/testing-library in a browser environment."`
7575
);
7676
});
77+
78+
test("fails when trying to read instance", async () => {
79+
const result = await render(Clickable);
80+
expect(() => result.instance).toThrowErrorMatchingInlineSnapshot(
81+
`"Cannot access component instance for server side tests."`
82+
);
83+
});

src/index-browser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export async function render<T extends Template>(
7171

7272
return {
7373
container,
74+
instance,
7475
emitted<N extends string = "*">(
7576
type?: N extends InternalEventNames ? never : N
7677
) {

src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export async function render<T extends Template>(
3131
): Promise<
3232
BoundFunctions<typeof Queries> & {
3333
container: HTMLElement | DocumentFragment;
34+
instance: any;
3435
debug: typeof testingLibraryScreen["debug"];
3536
emitted<N extends string = "*">(
3637
type?: N extends InternalEventNames ? never : N
@@ -83,16 +84,21 @@ export async function render<T extends Template>(
8384

8485
return {
8586
container,
87+
get instance(): any {
88+
throw new Error(
89+
"Cannot access component instance for server side tests."
90+
);
91+
},
8692
emitted<N extends string = "*">(
8793
// eslint-disable-next-line @typescript-eslint/no-unused-vars
8894
type?: N extends InternalEventNames ? never : N
8995
): NonNullable<EventRecord[N]> {
90-
throw new Error("Components should not emit events on the server side");
96+
throw new Error("Components should not emit events on the server side.");
9197
},
9298
// eslint-disable-next-line @typescript-eslint/no-unused-vars
9399
rerender(newInput?: typeof input): Promise<void> {
94100
return Promise.reject(
95-
new Error("Components cannot re-render on the server side")
101+
new Error("Components cannot re-render on the server side.")
96102
);
97103
},
98104
cleanup() {

0 commit comments

Comments
 (0)