question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem using `browser-fs-access` in jest

See original GitHub issue

Hi 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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
Amatewasucommented, Jul 20, 2021

It looks like using @Pet3ris’ solution after upgrading to browser-fs-access v0.18.1 solves the issue. (#55)

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)$"
 ]
2reactions
tomayaccommented, Mar 30, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found