ã¯ããã«
Jest ã§ãã¹ãã³ã¼ããä½æããéç¨ã§ä¸è¨ã® jest.config.js 㨠babel.config.js ãä½æãã¦ãããã®ã®ãNextJs ã§ã¢ããªèµ·åæã«æ²é¡ã®ã¨ã©ã¼ãåºã¦ããã£ãã®ã§ãã®å¯¾å¿ã¡ã¢ã«ãªãã¾ãã
Â
jest.config.js
module.exports = { testEnvironment: "jest-environment-jsdom", transform: { "^.+\\.(ts|tsx)$": "babel-jest", // TypeScriptãã¡ã¤ã«ããã©ã³ã¹ãã¤ã« "^.+\\.(js|jsx)$": "babel-jest", // JavaScriptãã¡ã¤ã«ããã©ã³ã¹ãã¤ã« }, moduleFileExtensions: ["ts", "tsx", "js", "jsx"], // æ¡å¼µåã追å setupFilesAfterEnv: ["/setupTests.ts"], moduleNameMapper: { "\\.(css|less|scss|sass)$": "identity-obj-proxy", }, };
babel.config.js
module.exports = { presets: [ "@babel/preset-env", ["@babel/preset-react", { runtime: "automatic" }], // JSXãµãã¼ãã追å "@babel/preset-typescript", // TypeScriptãµãã¼ã ], };
Â
åå ã«ã¤ãã¦
NextJs ã§ã¯ä»¥ä¸ã«è¨è¼ã®éããããã©ã«ã㧠SWC ã¨ããã³ã³ãã¤ã©ã使ç¨ãã¦ãã¾ãã
ãã® SWC ã³ã³ãã¤ã©ã¨ãJest 対å¿ã§è¿½å ãã Babel ã¨ã§ã³ã³ãã¤ã©ãè¤æ°ç«¶åãããã¨ã§ä»åã®ã¨ã©ã¼ãçºçãã¾ããã
â»Jest㧠JSX æ§æãªã©ãã©ã³ã¹ãã¤ã«ãã¦ãã¹ãå®è¡ã§ããããã«ããããã«ãBabel ãå ¥ãã¦ãã¾ãã
Â
対å¿å 容ã«ã¤ãã¦
NextJsããã©ã«ãã®SWCã³ã³ãã¤ã©ã¯ãã®ã¾ã¾ã§ãBabel ãªã㧠Jest ãã¹ãã¯åããã¤ã¤ãNextJsã¢ããªãèµ·åããããã«ä¿®æ£ãã¾ããã
Â
対å¿ãããã¨
- babel.config.js ãåé¤
- jest.config.js ãä¸è¨ã®ããã«ä¿®æ£
module.exports = { testEnvironment: "jest-environment-jsdom", transform: { "^.+\\.(ts|tsx|js|jsx)$": [ "babel-jest", { presets: [ "@babel/preset-env", ["@babel/preset-react", { runtime: "automatic" }], // JSXãµãã¼ãã追å "@babel/preset-typescript", ], plugins: [ // Babelã®ãã©ã°ã¤ã³ãããã«ç´æ¥æå® "@babel/plugin-transform-runtime", ], }, ], // TypeScript,JavaScriptãã¡ã¤ã«ããã©ã³ã¹ãã¤ã« }, moduleFileExtensions: ["ts", "tsx", "js", "jsx"], // æ¡å¼µåã追å setupFilesAfterEnv: ["/setupTests.ts"], moduleNameMapper: { "\\.(css|less|scss|sass)$": "identity-obj-proxy", }, extensionsToTreatAsEsm: [".ts", ".tsx"], };
Â
Jest ã ãã§ãã¹ãã³ã¼ããåãããã«ãplugins ã« babel plugin ã®æå®ã追å ãã¾ããã
ãªãã追å ã§ä¸è¨ã³ãã³ã㧠Babel é¢é£Pluginãã¤ã³ã¹ãã¼ã«ãã¾ããã
npm install --save-dev @babel/plugin-transform-runtime
npm install --save @babel/runtime
Â
çµããã«
NextJsç°å¢ä¸ã§ã¯ Babel ããã¾ãåããªããã¨ãç¥ãã¾ãããåããããªã¨ã©ã¼ã«ééããæ¹ã«ã¨ã£ã¦ä½ãåèã«ãªãã°å¹¸ãã§ãã
Â