kurkle / chartjs-chart-sankey

Chart.js module for creating sankey diagrams
MIT License
148 stars 29 forks source link

TypeError when this component is included in a vitest test suite #78

Closed simevo closed 6 months ago

simevo commented 2 years ago

We have a setup where this component is used inside a Vue 3 app, which is tested with jest (works fine!).

We are now moving to the latest vue tooling, and after porting the tests from jest to vitest, we get this error:

 ❯ TypedRegistry.isForType node_modules/chart.js/dist/chart.js:6136:74

 FAIL  tests/unit/caseDetail.spec.js [ tests/unit/caseDetail.spec.js ]
TypeError: Cannot read properties of undefined (reading 'prototype')
    6134|     console.log(`this.type = ${this.type}`)
    6135|     console.log(`type = ${JSON.stringify(type)}`)
    6136|     return Object.prototype.isPrototypeOf.call(this.type.prototype, type.prototype);
       |                                                                          ^
    6137|   }
    6138|   register(item) {
 ❯ Registry._getRegistryForType node_modules/chart.js/dist/chart.js:6279:15
 ❯ node_modules/chart.js/dist/chart.js:6259:41
 ❯ Registry._each node_modules/chart.js/dist/chart.js:6258:15
 ❯ Registry.add node_modules/chart.js/dist/chart.js:6216:10
 ❯ Function.value [as register] node_modules/chart.js/dist/chart.js:7549:16
 ❯ src/components/SankeyChart.vue:11:29
 ❯ async /app/frontend/src/components/CaseDetailPage.vue:14:31
 ❯ async /app/frontend/tests/unit/caseDetail.spec.js:14:31

it occurs in src/components/SankeyChart.vue on this line:

Chart.register(LinearScale, SankeyController, Flow)

I have set up a super-simple example to reproduce (https://gitlab.com/simevo/vue-3-vitest-basic-example), starting from the basic vue test example, I added chart.js and Chart.register(LinearScale) (works fine)

As soon as I import import { SankeyController, Flow } from 'chartjs-chart-sankey' and change Chart.register(LinearScale) to Chart.register(LinearScale, SankeyController, Flow) the error pops up.

Sorry for the convoluted use case, I just hope this helps you find some hidden bug in your nice module!

kurkle commented 1 year ago

Is this issue still valid? I've seen some issues with vite compiler and Chart.js, could this be the case?

My time with my open source projects has been very limited lately, so trying to catch up.

simevo commented 1 year ago

thanks for your time! this is obscure and low priority ...

anyway I have updated all dependencies in the vue-3-vitest-basic-example repo, and the error still comes up; somehow at some point it gets to here with the arg variable undefined

tanja213 commented 1 year ago

The problem is still valid. Used the plugin in our project, but it broke a (vitest) test. Also I am not able to test our sankey component due to the error: TypeError: Cannot read properties of undefined (reading 'prototype') at TypedRegistry.isForType (file:///C:/Users/.../app/node_modules/chart.js/src/core/core.typedRegistry.js:17:74) at Registry._getRegistryForType (file:///C:/Users/.../app/node_modules/chart.js/src/core/core.registry.js:164:15) at forEach (file:///C:/Users/.../app/node_modules/chart.js/src/core/core.registry.js:126:41) at Array.forEach () at Registry._each (file:///C:/Users/.../app/node_modules/chart.js/src/core/core.registry.js:125:15) at Registry.add (file:///C:/Users/.../app/node_modules/chart.js/src/core/core.registry.js:26:10) at Function.register (file:///C:/Users/.../app/node_modules/chart.js/src/core/core.controller.js:126:14) at C:\Users...\app\src\common\widgets\Sankey.tsx:30:9 at VitestExecutor.directRequest (file:///C:/Users/.../app/node_modules/vite-node/dist/client.mjs:334:5) at VitestExecutor.cachedRequest (file:///C:/Users/.../app/node_modules/vite-node/dist/client.mjs:190:14)

simevo commented 6 months ago

It looks like #105 released with v0.12.1 fixed this. Thanks!