Open FibreFoX opened 2 years ago
Things that I stumbled upon that I tried with no luck: https://github.com/kulshekhar/ts-jest/issues/970 https://github.com/facebook/jest/issues/12036 https://github.com/facebook/jest/issues/9771 https://stackoverflow.com/questions/64999897/jest-syntaxerror-unexpected-token-export
what is your nodejs、typescript version? try to upgrade them!
@hustcc
used typescript-version: 4.8.3 used nodejs-version: 16.17.1 (LTS)
This maybe has something todo with ESM modules working different in TypeScript-context, what do you think?.
~~In jest config, transformIgnorePatterns: ["/node_modules/(?!(echarts|zrender)/)"]
seems to be bypass the problem !
See https://stackoverflow.com/questions/68467058/got-error-when-testing-vue-echarts-component-with-jest~~
Updated: fail ... the test was no longer loading echarts..
@NotSqrt Have you checked that with the example provided by me? Because I already tried a lot.
@FibreFoX Sorry, fail ... my test was no longer loading echarts..
I got a mock to work around the syntax error...
my regular file is echart-bundle.ts
:
import EChartsReactCore from 'echarts-for-react/lib/core';
import * as echarts from 'echarts/core';
import {
BarChart,
LineChart,
PieChart,
} from 'echarts/charts';
import {
TitleComponent,
TooltipComponent,
ToolboxComponent,
GridComponent,
LegendComponent,
DataZoomComponent,
DataZoomInsideComponent,
DataZoomSliderComponent,
DatasetComponent,
TransformComponent
} from 'echarts/components';
import { LabelLayout, UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';
echarts.use([
TitleComponent,
TooltipComponent,
ToolboxComponent,
GridComponent,
LegendComponent,
DataZoomComponent,
DataZoomInsideComponent,
DataZoomSliderComponent,
DatasetComponent,
TransformComponent,
BarChart,
LineChart,
PieChart,
LabelLayout,
UniversalTransition,
CanvasRenderer
]);
// subclass to use our bundle for echarts
export class EChartsReact extends EChartsReactCore {
static defaultProps = {
echarts
}
}
Then, I can bypass the syntax error by not importing from echarts/core
at all:
__mocks__/echarts-bundle.ts
import EChartsReactCore from 'echarts-for-react/lib/core';
// subclass to use our bundle for echarts
export class EChartsReact extends EChartsReactCore {}
And in my tests, I can activate jest.mock('echart-bundle')
It no longer uses a smaller bundle, but at least the tests can run ..
But thats the problem, when using all the given instructions for the minimal bundle. Having this "replaced" with a mock, does not solve that problem, it just obfruscates it.
I think the core problem comes from this ESM stuff, which might originate from https://github.com/apache/echarts/issues/16709#issuecomment-1215522586 (where I already posted something to make them aware).
But even echarts-for-react
is not "at fault", the given instructions are faulty.
After loosing half a day of trying to have this working, there seems to be something broken when Jest is involved. The instructions in the README.md do not seem to be complete.
I have a fairly standard React v17 app (created via CRA), where I am using
echarts-for-react
.When having something like this, it results in Jest-tests not working:
Jest quits exploding with the following lines:
With these lines it works .... but why? What am I missing?
Just to be clear: it is only Jest here that seems to not work. When building my application, or running
yarn start
as normal, it works.Some parts of the
package.json
from my project.