alexcaza / export-to-csv

Export a JS collection to CSV; written in TypeScript.
Other
200 stars 48 forks source link

Jest throws: "SyntaxError: Unexpected token 'export'" with module: commonjs #109

Open bradley329 opened 1 month ago

bradley329 commented 1 month ago

Describe the bug possible related issue: https://github.com/alexcaza/export-to-csv/issues/87 I know that the new version of export-to-csv has some breaking change which exports ES modules instead of CommonJS now? But that caused our Jest to fail as it by default does not transform the ts files in export-to-csv. Is there a recommended approach to work this around? I saw this post: https://www.sobyte.net/post/2022-06/jest/ but looks like only option 2 is good for export-to-csv.🤔But it will lead to confusions.

To Reproduce Steps to reproduce the behavior: tsconfig.json: "compilerOptions": { "moduleResolution": "Node", "module": "CommonJS", } and package.json does NOT have "type": "module" as i am on commonjs.

Full error Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
 • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation

Details:

C:\corext\DRDashboard\DRDashboard\DRDashboard\node_modules\export-to-csv\output\index.js:4
    `)};var Q$=($)=>(x)=>{const W=q($),j=W.useKeysAsHeaders?Object.keys(x[0]):W.columnHeaders;let A=P(_(""),S(W),R(W),O(W,j),C(W,j,x));if(z(A).length<1)throw new Q("Output is empty. Is your data formatted correctly?");return A},s=($)=>(x)=>{const W=q($),j=z(x),A=W.useTextFile?"plain":"csv";return new Blob([j],{type:`text/${A};charset=utf8;`})},T$=($)=>(x)=>{if(!window)throw new U("Downloading only supported in a browser environment.");const W=s($)(x),j=q($),A=j.useTextFile?"txt":"csv",J=`${j.filename}.${A}`,I=document.createElement("a");I.download=J,I.href=URL.createObjectURL(W),I.setAttribute("visibility","hidden"),document.body.appendChild(I),I.click(),document.body.removeChild(I)};export{q as mkConfig,Q$ as generateCsv,T$ as download,l as asString,s as asBlob};

                                                                                    ^^^^^^

SyntaxError: Unexpected token 'export'

> 1 | import { mkConfig, generateCsv, download, ConfigOptions } from "export-to-csv";
    | ^
  2 | import { AcceptedData } from "export-to-csv/output/lib/types";
  3 |
  4 | export function ExportDataToCsv(
alexcaza commented 1 month ago

I don't have an officially supported work around since I've dropped CJS support from the library. However, and I haven't tested this, I found jest-esm-transformer-2 which might help.

Another option might be to mock export-to-csv's functions. If your tests have fixed input and output expectations, you can mock this library's functions to always return the expected "sane" output. The downside is that it might drift if things change in future versions. Granted, this library has pretty decent test coverage now, so it should be safe to trust its output will be consistent. Any expected breaking changes to output will be denoted by a major version bump, anyway.