<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rola Muibi</title>
    <description>The latest articles on DEV Community by Rola Muibi (@rolamuibi).</description>
    <link>https://dev.to/rolamuibi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1447555%2Fb39d768a-3bfc-4d58-8ea7-10d041f0e93d.png</url>
      <title>DEV Community: Rola Muibi</title>
      <link>https://dev.to/rolamuibi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rolamuibi"/>
    <language>en</language>
    <item>
      <title>How to fix Next.js “Type error: Type 'Props' does not satisfy the constraint 'LayoutProps'”</title>
      <dc:creator>Rola Muibi</dc:creator>
      <pubDate>Sat, 03 May 2025 02:08:01 +0000</pubDate>
      <link>https://dev.to/rolamuibi/how-to-fix-nextjs-type-error-type-props-does-not-satisfy-the-constraint-layoutprops-2n5a</link>
      <guid>https://dev.to/rolamuibi/how-to-fix-nextjs-type-error-type-props-does-not-satisfy-the-constraint-layoutprops-2n5a</guid>
      <description>&lt;p&gt;While trying to add &lt;code&gt;generateMetadata&lt;/code&gt; to the &lt;code&gt;layout.tsx&lt;/code&gt; in the dynamic route under the &lt;code&gt;blog&lt;/code&gt; dir, I bumped into this error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Type error: Type &lt;span class="s1"&gt;'Props'&lt;/span&gt; does not satisfy the constraint &lt;span class="s1"&gt;'LayoutProps'&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
Types of property &lt;span class="s1"&gt;'params'&lt;/span&gt; are incompatible.
Type &lt;span class="s1"&gt;'Params'&lt;/span&gt; is missing the following properties from &lt;span class="nb"&gt;type &lt;/span&gt;Promise&amp;lt;any?: &lt;span class="k"&gt;then&lt;/span&gt;, catch, finally, &lt;span class="o"&gt;[&lt;/span&gt;Symbol.toStringTag]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;This issue is actually part of a breaking change in Next.js 15.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Here’s how to fix it:
&lt;/h2&gt;

&lt;p&gt;To fix this, update the &lt;code&gt;params&lt;/code&gt; type to be a &lt;code&gt;Promise&lt;/code&gt; and use &lt;code&gt;await&lt;/code&gt; to resolve it inside the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Metadata&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;next&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;// make this a promise&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;generateMetadata&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Metadata&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;slug&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//await here&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why did this issue happen?
&lt;/h2&gt;

&lt;p&gt;This happened because Next.js 15 made &lt;strong&gt;&lt;code&gt;params&lt;/code&gt;&lt;/strong&gt; asynchronous in certain functions like &lt;code&gt;generateMetadata&lt;/code&gt;. That means &lt;code&gt;params&lt;/code&gt; is no more a plain object, it's now a promise that needs to be awaited.&lt;/p&gt;

&lt;p&gt;In your original code, you probably treated &lt;code&gt;params&lt;/code&gt; like a regular object, but Next.js 15 now expects it to be a &lt;strong&gt;&lt;code&gt;Promise&lt;/code&gt;&lt;/strong&gt; that resolves to an object, so TypeScript throws an error. It's complaining that your &lt;code&gt;params&lt;/code&gt; is missing properties that come with a Promise (like &lt;code&gt;.then&lt;/code&gt;, &lt;code&gt;.catch&lt;/code&gt;, etc.).&lt;/p&gt;

&lt;p&gt;So now changing &lt;code&gt;params&lt;/code&gt; to a &lt;code&gt;Promise&lt;/code&gt; will match what Next.js 15 expects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why did Next.js 15 add this change?
&lt;/h2&gt;

&lt;p&gt;Next.js is changing some features (like &lt;code&gt;cookies&lt;/code&gt;, &lt;code&gt;headers&lt;/code&gt;,  &lt;code&gt;params&lt;/code&gt;, and &lt;code&gt;searchParams&lt;/code&gt;)  to work asynchronously. Usually, when someone visits a page, the server waits for the request before it generates and sends back the content for that page. This means the server typically doesn't render the page until it gets the request and any associated data (like parameters or cookies).&lt;/p&gt;

&lt;p&gt;Since not all parts depend on the request data, the server no longer has to wait before rendering. And now the sever can prepare earlier and only wait when it really needs that data. Read more about this on &lt;a href="https://nextjs.org/blog/next-15#async-request-apis-breaking-change" rel="noopener noreferrer"&gt;Vercel’s website&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>nextjs</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Recap of the Next.js Bug That Got Apps Hacked (And How to Protect Yours)</title>
      <dc:creator>Rola Muibi</dc:creator>
      <pubDate>Sun, 27 Apr 2025 18:19:51 +0000</pubDate>
      <link>https://dev.to/rolamuibi/recap-of-the-nextjs-bug-that-got-apps-hacked-and-how-to-protect-yours-4b1p</link>
      <guid>https://dev.to/rolamuibi/recap-of-the-nextjs-bug-that-got-apps-hacked-and-how-to-protect-yours-4b1p</guid>
      <description>&lt;p&gt;For Next.js developers using versions  11.1.4 through 15.2, there was a recent security bug in Next.js (CVE-2025-29927), that let attackers skip middleware checks and reach protected parts of a  site.  This is due to how Next.js handles this header  &lt;code&gt;x-middleware-subrequest&lt;/code&gt; which allows hackers to trick Next.js into thinking middleware already ran. &lt;/p&gt;

&lt;p&gt;This only affects apps not hosted by Vercel or Netlify and runs with &lt;code&gt;next start&lt;/code&gt;. To fix this, you can update to the most recent fixed releases. If you cannot update your Next.js version now, block or remove the header and add extra auth checks in your routes and API logic.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happened
&lt;/h2&gt;

&lt;p&gt;Next.js has a system to stop middleware from running in a loop. It uses a special header called &lt;code&gt;x-middleware-subrequest&lt;/code&gt; to count how many times middleware runs.&lt;/p&gt;

&lt;p&gt;If it runs too many times (5 by default), Next.js skips middleware to avoid an endless loop.&lt;/p&gt;

&lt;p&gt;The problem is, anyone can set this header themselves.&lt;/p&gt;

&lt;p&gt;Hackers can fake the header, make it look like middleware already ran too much, and Next.js will skip all security checks.&lt;/p&gt;

&lt;p&gt;Here’s a basic idea of how it works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;subrequests&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x-middleware-subrequest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)?.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;depth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;subrequests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;middlewareName&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;depth&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;MAX_RECURSION_DEPTH&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// this skips middlewares&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What hackers do
&lt;/h2&gt;

&lt;p&gt;They send a fake request  to a protected endpoint. In their fake request, they manually add the header &lt;code&gt;x-middleware-subrequest&lt;/code&gt; with lots of fake entries. Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
GET /protected-page
x-middleware-subrequest: auth:auth:auth:auth:auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, when Next.js receives that request, it will think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"this middleware already ran 5 times. I should skip it now."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But in reality, middleware never ran even once, the hacker just tricked it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Who is affected
&lt;/h2&gt;

&lt;p&gt;This issue only affects apps that are self hosted and uses &lt;code&gt;next start&lt;/code&gt; with the &lt;code&gt;standalone&lt;/code&gt; output. If your app is deployed on Vercel, Netlify, or your site is just a static file, you are not affected.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to fix
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Update Next.js now&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;12.x → 12.3.5&lt;/li&gt;
&lt;li&gt;13.x → 13.5.9&lt;/li&gt;
&lt;li&gt;14.x → 14.2.25&lt;/li&gt;
&lt;li&gt;15.x → 15.2.3&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Block or remove the header&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;In NGINX:
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; location / {
          proxy_set_header x-middleware-subrequest "";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In Apache:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        RequestHeader unset x-middleware-subrequest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In  Cloudflare’s WAF, add a rule that blocks any incoming request that contains the header called &lt;code&gt;x-middleware-subrequest.&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Always add extra checks and don’t just trust middleware only&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Don’t only rely on middleware, always add extra layers of authorization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In your page code&lt;/li&gt;
&lt;li&gt;In your API routes&lt;/li&gt;
&lt;li&gt;In your database&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, if your dashboard shows private info, the API should still check if the user is logged in and authorized to view the requested data. Even if someone skips the middleware, they still won't get the data.&lt;/p&gt;

&lt;p&gt;For more information regarding this critical flaw, visit the official &lt;a href="https://vercel.com/blog/postmortem-on-next-js-middleware-bypass" rel="noopener noreferrer"&gt;Vercel’s blog&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>nextjs</category>
      <category>hacker</category>
      <category>security</category>
    </item>
    <item>
      <title>Jest and Vite: Cannot use `import.meta` outside a module</title>
      <dc:creator>Rola Muibi</dc:creator>
      <pubDate>Wed, 24 Apr 2024 20:19:41 +0000</pubDate>
      <link>https://dev.to/rolamuibi/jest-and-vite-cannot-use-importmeta-outside-a-module-24n3</link>
      <guid>https://dev.to/rolamuibi/jest-and-vite-cannot-use-importmeta-outside-a-module-24n3</guid>
      <description>&lt;p&gt;&lt;strong&gt;This post describes a fix for a common issue with Jest and using &lt;code&gt;import.meta&lt;/code&gt; to load environment variables.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;In react, I kept running into an error when testing with jest. In my case, Jest could not recognize import.meta.env.&lt;/p&gt;

&lt;p&gt;The error I got was:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Test suite failed to run

    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 &lt;span class="s2"&gt;"node_modules"&lt;/span&gt; folder is ignored by transformers.

    Here&lt;span class="s1"&gt;'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'&lt;/span&gt;ll find more details and examples of these config options &lt;span class="k"&gt;in &lt;/span&gt;the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:
    SyntaxError: Cannot use &lt;span class="s1"&gt;'import.meta'&lt;/span&gt; outside a module

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The reason
&lt;/h2&gt;

&lt;p&gt;Jest, by default, uses CommonJS module system for handling JavaScript modules. CommonJS doesn't support import.meta because it's a feature introduced in ECMAScript modules (ESM).&lt;/p&gt;

&lt;p&gt;Also &lt;code&gt;import.meta.env&lt;/code&gt; is a feature provided by Vite for loading environment variables so it is not recognized by default in Jest or Node.js.&lt;/p&gt;

&lt;p&gt;Therefore, when Jest encounters &lt;code&gt;import.meta&lt;/code&gt; or &lt;code&gt;import.meta.env&lt;/code&gt; in the code, it doesn't recognize it and throws an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;After much trial and error, here's what worked:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Install dependencies
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; babel-jest jest-environment-jsdom @babel/preset-env @babel/preset-react babel-preset-vite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Create a babel.config.json file
&lt;/h3&gt;

&lt;p&gt;After installing the above dependencies, we need to configure Babel.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;babel.config.json&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"presets"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"@babel/preset-env"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"targets"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"current"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}],&lt;/span&gt;&lt;span class="w"&gt;

    &lt;/span&gt;&lt;span class="s2"&gt;"@babel/preset-react"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;

    &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="s2"&gt;"babel-preset-vite"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"env"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"glob"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Finally, configure your jest.config.js file
&lt;/h3&gt;

&lt;p&gt;In your &lt;code&gt;jest.config.js&lt;/code&gt; file, include this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// jest.config.js&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;moduleNameMapper&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;^@/(.*)$&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;rootDir&amp;gt;/src/$1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;testEnvironment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;jest-environment-jsdom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;^.+&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;.[t|j]sx?$&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;babel-jest&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Jest throwing an error when &lt;code&gt;import.meta&lt;/code&gt; or &lt;code&gt;import.meta.env&lt;/code&gt; is used is due to its default use of the CommonJS module system and &lt;code&gt;import.meta.env&lt;/code&gt; being a feature of vite. This issue can be resolved by configuring Jest and Babel correctly. With the above steps, we can successfully use &lt;code&gt;import.meta&lt;/code&gt; and &lt;code&gt;import.meta.env&lt;/code&gt; in our code and Jest tests.&lt;/p&gt;

</description>
      <category>jest</category>
      <category>vite</category>
      <category>testing</category>
      <category>react</category>
    </item>
  </channel>
</rss>
