Currently Jest is not able to run with typesafe-i18n out of the box.
Although newer Jest versions can import ESM files directly, it is still experimental. Older versions require more configuration by using babel-transformers.
Consider compiling the file to CJS instead to provide out of the box support and to make it compatible with older versions without any additional configuration.
● Test suite failed to run
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:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
• 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:
/Users/aelesia/Documents/git/project/node_modules/typesafe-i18n/adapters/adapter-react.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import t,{createContext as r,useState as e}from"react";const n=/\?\?/g,o=(t,r,e,o)=>{const l=o[0],i=l&&"object"==typeof l&&l.constructor===Object;return((t,r,e,o)=>t.map((t=>{if("string"==typeof t)return t;const{k:l="0",f:i=[]}=t,s=o[l];if((t=>!(!t.o&&!t.r))(t))return(("boolean"==typeof s?s?t.o:t.r:((t,{z:r,o:e,t:n,f:o,m:l,r:i},s)=>{switch(r&&0==s?"zero":t.select(s)){case"zero":return r;case"one":return e;case"two":return n;case"few":return o;case"many":return l;default:return i}})(r,t,s))||"").replace(n,s);const c=i.length?((t,r,e)=>r.reduce(((r,e)=>{var n,o;return null!==(o=null===(n=t[e])||void 0===n?void 0:n.call(t,r))&&void 0!==o?o:r}),e))(e,i,s):s;return(""+(null!=c?c:"")).trim()})).join(""))(t,r,e,1===o.length&&i?l:o)},l=t=>new Proxy(t?()=>t:{},{get:(r,e)=>l(t?`${t}.${e}`:e)}),i=/({?{[^\\}]+}}?)/g,s=(t,r=!0,e="",n="")=>t.split(i).filter(Boolean).map((t=>{if(!t.match(i))return t;const r=t.substring(1,t.length-1);if(r.startsWith("{")&&r.endsWith("}"))return((t,r)=>{let[e,n]=t.split(":");n||(n=e,e=r);const o=n.split("|"),[l,i,s,c,u,a]=o,f=o.filter((t=>void 0!==t)).length;return 1===f?{k:e,r:l}:2===f?{k:e,o:l,r:i}:3===f?{k:e,z:l,o:i,r:s}:{k:e,z:l,o:i,t:s,f:c,m:u,r:a}})(r.substring(1,r.length-1),n);const o=(t=>{const[r="",...e]=t.split("|"),[n="",o]=r.split(":");return{k:n,i:o,f:e}})(r);return n=o.k||n,!e&&(e=n),o})).map((t=>{if("string"==typeof t)return t;t.k||(t.k=e||"0");const n=(t=>Object.fromEntries(Object.keys(t).map((r=>{const e=t[r];return[r,Array.isArray(e)?e.map((t=>null==t?void 0:t.trim())):null==e?void 0:e.trim()]}))))(t);return r?(o=n,Object.fromEntries(Object.entries(o).map((([t,r])=>"i"!==t&&r&&"0"!=r&&[t,r])).filter(Boolean))):n;var o})),c=(t,r,e)=>{const n={},l=new Intl.PluralRules(t);return(t,...i)=>o(((t,r)=>t[r]||(t[r]=s(r)))(n,((t,r)=>{var e;return r.split(".").forEach((r=>t=t[r])),null!==(e=t)&&void 0!==e?e:r})(r,t)),l,e,i)};const u=(t,r)=>new Proxy(r?t.bind(null,r):{},{get:(e,n)=>u(t,r?`${r}.${n}`:n)}),a=(r="",n=(()=>({})),o=(()=>({})))=>{const i=f();return{component:s=>{const[a,f]=e(!1),[p,m]=e(null),[d,g]=e(l()),v=async t=>{f(!0);const r=n(t);g(function(t,r,e={}){return u(c(t,r,e))}(t,r,await o(t))),m(t),f(!1)};if(!p&&!a&&v(s.initialLocale||r),!a&&!d)return null;const h={setLocale:v,isLoadingLocale:a,locale:p,LL:d};return t.createElement(i.Provider,{value:h},s.children)},context:i}},f=()=>r({});export{a as initI18nReact};
^^^^^^
SyntaxError: Cannot use import statement outside a module
2 | /* eslint-disable */
3 |
> 4 | import { initI18nReact } from 'typesafe-i18n/adapters/adapter-react'
| ^
5 | import type { Locales, Translation, TranslationFunctions, Formatters } from './i18n-types'
6 | import { baseLocale, getTranslationForLocale } from './i18n-util'
7 | import { initFormatters } from './formatters'
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
at Object.<anonymous> (src/directory/i18n/i18n-react.tsx:4:1)