Problem using `browser-fs-access` in jest
See original GitHub issueHi There,
I’m using jest for testing and having trouble with importing the browser-fs-access
library.
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• 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/en/configuration.html
Details:
<root>/node_modules/browser-fs-access/dist/index.js:2
export { fileOpen } from "./file-open.mjs";
^^^^^^
SyntaxError: Unexpected token 'export'
This seems to be a common issue, documented here: https://github.com/facebook/jest/issues/2550, however, the common solutions don’t work.
I tried to use the following configuration parameter to account for presence of mjs
files but still getting the same error. Any thoughts on how to allow jest to successfully compile browser-fs-access
?
"transformIgnorePatterns": [
"node_modules/(?!browser-fs-access).+(js|jsx|mjs|ts)$"
]
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:8 (7 by maintainers)
Top Results From Across the Web
browser-fs-access - npm
Start using browser-fs-access in your project by running `npm i browser-fs-access`. There are 24 other projects in the npm registry using ...
Read more >Using node.js's File System functions from a browser
You cannot use Node's fs in the browser and have it directly affect the file system on the server. fs is for use...
Read more >Configuring Jest
Node.js core modules, like fs , are not mocked by default. They can be mocked explicitly, like jest.mock('fs') .
Read more >Testing with Node, Jest, and JSDOM - Manning Publications
Because you've used module.exports to expose incrementCount and data, main.js will now throw an error when running in the browser. To see the ......
Read more >Reading and writing files and directories with the browser-fs ...
Since the File System Access API is still likely to change in the future, the browser-fs-access API is not modeled after it. That...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It looks like using @Pet3ris’ solution after upgrading to browser-fs-access v0.18.1 solves the issue. (#55)
Sorry, I’m not familiar with Jest, but https://github.com/facebook/jest/issues/2550#issuecomment-810144368 is the right place to ask your question.