Skip to content

Commit b03fa99

Browse files
committed
enhance: A more clear and helpful message when composables are misused.
1 parent bbc302b commit b03fa99

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

.changeset/tiny-wolves-thank.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@data-client/vue': patch
3+
---
4+
5+
Improve dependency injection console message
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`context injectState should log error in development when state is not provided 1`] = `
4+
"Reactive Data Client (Vue) composables dependency injection failed. Either you are missing the DataClientPlugin or you are using composables outside of script setup.
5+
Adding DataClientPlugin: https://dataclient.io/docs/getting-started/installation#add-provider-at-top-level-component
6+
Comosables only work in script setup: https://vuejs.org/guide/reusability/composables.html#usage-restrictions"
7+
`;
8+
9+
exports[`context useController should log error in development when controller is not provided 1`] = `
10+
"Reactive Data Client (Vue) composables dependency injection failed. Either you are missing the DataClientPlugin or you are using composables outside of script setup.
11+
Adding DataClientPlugin: https://dataclient.io/docs/getting-started/installation#add-provider-at-top-level-component
12+
Comosables only work in script setup: https://vuejs.org/guide/reusability/composables.html#usage-restrictions"
13+
`;

packages/vue/src/__tests__/context.test.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,7 @@ describe('context', () => {
8888

8989
useController();
9090

91-
expect(consoleErrorSpy).toHaveBeenCalledWith(
92-
expect.stringContaining(
93-
'It appears you are trying to use Reactive Data Client (Vue) without a provider.',
94-
),
95-
);
96-
expect(consoleErrorSpy).toHaveBeenCalledWith(
97-
expect.stringContaining(
98-
'https://dataclient.io/docs/getting-started/installation',
99-
),
100-
);
91+
expect(consoleErrorSpy.mock.calls[0][0]).toMatchSnapshot();
10192
});
10293

10394
it('should not log error in production when controller is not provided', () => {
@@ -146,11 +137,7 @@ describe('context', () => {
146137

147138
injectState();
148139

149-
expect(consoleErrorSpy).toHaveBeenCalledWith(
150-
expect.stringContaining(
151-
'It appears you are trying to use Reactive Data Client (Vue) without a provider.',
152-
),
153-
);
140+
expect(consoleErrorSpy.mock.calls[0][0]).toMatchSnapshot();
154141
expect(consoleErrorSpy).toHaveBeenCalledWith(
155142
expect.stringContaining(
156143
'https://dataclient.io/docs/getting-started/installation',

packages/vue/src/context.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ export function useController(): Controller {
1919
/* istanbul ignore else */
2020
if (process.env.NODE_ENV !== 'production') {
2121
console.error(
22-
'It appears you are trying to use Reactive Data Client (Vue) without a provider.\n' +
23-
'Follow instructions: https://dataclient.io/docs/getting-started/installation#add-provider-at-top-level-component',
22+
'Reactive Data Client (Vue) composables dependency injection failed. Either you are missing the DataClientPlugin or you are using composables outside of script setup.\n' +
23+
'Adding DataClientPlugin: https://dataclient.io/docs/getting-started/installation#add-provider-at-top-level-component\n' +
24+
'Comosables only work in script setup: https://vuejs.org/guide/reusability/composables.html#usage-restrictions',
2425
);
2526
}
2627
return new Controller();
@@ -34,8 +35,9 @@ export function injectState(): ShallowRef<State<unknown>> {
3435
/* istanbul ignore else */
3536
if (process.env.NODE_ENV !== 'production') {
3637
console.error(
37-
'It appears you are trying to use Reactive Data Client (Vue) without a provider.\n' +
38-
'Follow instructions: https://dataclient.io/docs/getting-started/installation#add-provider-at-top-level-component',
38+
'Reactive Data Client (Vue) composables dependency injection failed. Either you are missing the DataClientPlugin or you are using composables outside of script setup.\n' +
39+
'Adding DataClientPlugin: https://dataclient.io/docs/getting-started/installation#add-provider-at-top-level-component\n' +
40+
'Comosables only work in script setup: https://vuejs.org/guide/reusability/composables.html#usage-restrictions',
3941
);
4042
}
4143
return FallbackStateRef;

0 commit comments

Comments
 (0)