OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to add import.meta.url to Jest?

  • Thread starter Thread starter GʀᴜᴍᴘʏCᴀᴛ
  • Start date Start date
G

GʀᴜᴍᴘʏCᴀᴛ

Guest
Getting a Jest error of:

Jest encountered an unexpected token

with the details of:

SyntaxError: Cannot use 'import.meta' outside a module

on the code of:

Code:
export const srcDir = path.join(path.dirname(fileURLToPath(import.meta.url)), '../', 'src')

but in my package.json config I have:

Code:
  "jest": {
    "verbose": true,
    "testEnvironment": "node",
    "roots": [
      "__tests__",
      "lib",
      "src"
    ],
    "modulePathIgnorePatterns": [
      "<rootDir>/docs",
      "<rootDir>/src",
    ],
    "transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!chalk)",
      "<rootDir>/node_modules/(?!import.meta)"
    ],
    "transform": {
      "\\.[jt]sx?$": "babel-jest"
    }
  },

with:

Code:
  "type": "module",

I've read the docs on:


and read through:


What am I doing wrong and how can I get my jest test to pass?

<p>Getting a Jest error of:</p>
<blockquote>
<p>Jest encountered an unexpected token</p>
</blockquote>
<p>with the details of:</p>
<blockquote>
<p>SyntaxError: Cannot use 'import.meta' outside a module</p>
</blockquote>
<p>on the code of:</p>
<pre><code>export const srcDir = path.join(path.dirname(fileURLToPath(import.meta.url)), '../', 'src')
</code></pre>
<p>but in my <em>package.json</em> config I have:</p>
<pre class="lang-js prettyprint-override"><code> "jest": {
"verbose": true,
"testEnvironment": "node",
"roots": [
"__tests__",
"lib",
"src"
],
"modulePathIgnorePatterns": [
"<rootDir>/docs",
"<rootDir>/src",
],
"transformIgnorePatterns": [
"<rootDir>/node_modules/(?!chalk)",
"<rootDir>/node_modules/(?!import.meta)"
],
"transform": {
"\\.[jt]sx?$": "babel-jest"
}
},
</code></pre>
<p>with:</p>
<pre><code> "type": "module",
</code></pre>
<p>I've read the docs on:</p>
<ul>
<li><a href="https://jestjs.io/docs/configuration" rel="nofollow noreferrer">configuration</a></li>
<li><a href="https://jestjs.io/docs/code-transformation" rel="nofollow noreferrer">code-transformation</a></li>
</ul>
<p>and read through:</p>
<ul>
<li><a href="https://stackoverflow.com/questions/51994111/jest-encountered-an-unexpected-token">Jest encountered an unexpected token</a></li>
<li><a href="https://stackoverflow.com/questions...cted-token-when-working-with-react-typescript">Jest encountered an unexpected token when working with React TypeScript</a></li>
<li><a href="https://stackoverflow.com/questions/75291831/how-to-fix-jest-encountered-an-unexpected-token">How to fix Jest encountered an unexpected token</a></li>
<li><a href="https://stackoverflow.com/questions...because-of-an-unknown-unexpected-token-export">Jest tests are failing because of an unknown unexpected token "export"</a></li>
<li><a href="https://stackoverflow.com/questions/75617573/jest-encountered-an-unexpected-token-react-app-with-js">Jest encountered an unexpected token: React App with JS</a></li>
</ul>
<p>What am I doing wrong and how can I get my jest test to pass?</p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top