Skip to content

Commit cec6ce5

Browse files
vue: add jest testing hints on imports (#2568)
Add documentation on import order for jest (or other) unit tests using CJS transformations to the READMEs of all vue packages Fix #2250
1 parent 154369e commit cec6ce5

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

packages/vue-vanilla/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ Attributes not specified here fall back to either the `defaultStyles` or provide
194194
}
195195
```
196196

197+
### Testing in CJS-transformed environments
198+
199+
When writing tests for custom renderers in a Jest/Vitest CJS-transformed environment, `vue` should be imported first in your custom renderers. See [Testing with Jest / Vitest](../vue/README.md#testing-with-jest--vitest) in the `@jsonforms/vue` README.
200+
197201
## License
198202

199203
The JSONForms project is licensed under the MIT License. See the [LICENSE file](https://github.com/eclipsesource/jsonforms/blob/master/LICENSE) for more information.

packages/vue-vuetify/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ provide(
174174
);
175175
```
176176

177+
## Testing in CJS-transformed environments
178+
179+
When writing tests for custom renderers in a Jest/Vitest CJS-transformed environment, `vue` should be imported first in your custom renderers. See [Testing with Jest / Vitest](../vue/README.md#testing-with-jest--vitest) in the `@jsonforms/vue` README.
180+
177181
## License
178182

179183
The JSONForms project is licensed under the MIT License. See the [LICENSE file](https://github.com/eclipsesource/jsonforms/blob/master/LICENSE) for more information.

packages/vue/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,27 @@ const myComponent = defineComponent({
289289
The injected `jsonforms` object is not meant to be modified directly.
290290
Instead it should be modified via the provided `dispatch` and by changing the props of the `json-forms` component.
291291

292+
### Testing with Jest / Vitest
293+
294+
When testing custom renderers in a CJS-transformed test environment (Jest, or Vitest configured with CJS transforms), `vue` must be imported **before** `@jsonforms/vue` in your renderer files:
295+
296+
```ts
297+
// Correct - import vue before @jsonforms/vue
298+
import { defineComponent } from 'vue';
299+
import { rendererProps, useJsonFormsControl } from '@jsonforms/vue';
300+
```
301+
302+
```ts
303+
// May produce errors such as:
304+
// "Property '<name>' was accessed during render but is not defined on instance"
305+
import { rendererProps, useJsonFormsControl } from '@jsonforms/vue';
306+
import { defineComponent } from 'vue';
307+
```
308+
309+
The reason is that several components shipped by `@jsonforms/vue` (e.g. `JsonForms`, `DispatchRenderer`) call `defineComponent` at module load. When the package is consumed via `require()` and the test runner's CJS transform doesn't hoist imports, `vue` must already be evaluated at that point. Thus, importing `vue` first in renderer and test files is the safest default.
310+
311+
Browser/dev builds using Webpack, Vite, or other ESM-aware bundlers are not affected.
312+
292313
## License
293314

294315
The JSON Forms project is licensed under the MIT License. See the [LICENSE file](https://github.com/eclipsesource/jsonforms/blob/master/LICENSE) for more information.

0 commit comments

Comments
 (0)