ã¯ããã«
以ä¸ã® Header ã³ã³ãã¼ãã³ãã®ãã¹ããæ¸ãã¦ãããå®è¡ããã¨ãããCSS ã® import é¨åã§ã¨ã©ã¼ã«ãªã£ã¦ãã¾ã£ãã®ã§ãã®å¯¾å¿ãã¡ã¢ãã¾ããã
 SyntaxError: Unexpected token '.'
  > 1 | import "./css/component.css";
    | ^
   2 |
   3 | type RibbonType = {
   4 |  caption: string;
Â
ãã¹ãã³ã¼ã
Header.test.tsx
ãã¹ã対象ã³ã¼ã
Header.tsx
Â
ãã¹ã対象ã³ã¼ãã®åã³ã³ãã¼ãã³ãï¼CSSèªã¿è¾¼ã¿é¨åã§ä»åã®ã¨ã©ã¼ãçºçï¼
Ribbon.tsx
Â
åå ã«ã¤ãã¦
Jest ãä¸è¨ç®æã§CSSãèªã¿è¾¼ãéãJavaScript ã¨ãã¦å¦çãããã¨ãã¦ãã¾ãããã«çºçãã¦ããã¨ã©ã¼ã®ããã§ãã
ä¸è¨ã®åèãµã¤ãã§ã¯ CSS ã¢ãã¯ç¨ã® JS ãã¡ã¤ã«ãç¨æããããæ¹ããjest-transform-stub ã使ãããæ¹ãç´¹ä»ããã¦ãã¾ãããç§ã®ã»ãã§ã¯ identity-obj-proxy ã使ã£ã CSS ã®ã¢ãã¯åã®ããæ¹ã試ããã®ã§ãã¡ãã®æ¹æ³ã§å¯¾å¦æ³ãè¨è¼ãã¾ãã
Â
ã¨ã©ã¼å¯¾å¿
identity-obj-proxy ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ãã
npm install --save-dev identity-obj-proxy
Â
ã¢ãã¯è¨å®ã追å ãã
jest.config.js
module.exports = { moduleFileExtensions: ["ts", "tsx", "js", "jsx"], // æ¡å¼µåã追å ã»ã»ï¼ç¥ï¼ moduleNameMapper: { "\\.(css|less|scss|sass)$": "identity-obj-proxy", }, };
- css, less, scss, sass ãããã®æ¡å¼µåã®ãã¡ã¤ã«ã«å¯¾ãã¦ãidentity-obj-proxy ã使ãããã«æå®ãã¦ãã¾ã
- identity-obj-proxy ã«ããã該å½ã®ãã¡ã¤ã«ãã¢ãã¯ãããã¨ãã§ãã¾ã
Â
ãã®ç¶æ ã§å度ãã¹ããå®è¡ãã¦ã¿ãã¨ãä¸è¨ã®ããã«ç¡äºãã¹ããPASSãããã¨ã確èªãã¾ããã
npm run test
> [email protected] test
> jest PASS  src/components/__tests__/Button.test.tsx
 PASS  src/components/__tests__/Header.test.tsxTest Suites: 2 passed, 2 total
Tests: Â Â Â 3 passed, 3 total
Snapshots: Â 0 total
Time: Â Â Â Â 0.723 s, estimated 1 s
Â
çµããã«
CSSèªã¿è¾¼ã¿ã§ã¨ã©ã¼ã«ééããã¨ã¯æã£ã¦ãã¾ããã§ãããããããã£ãå ´é¢ã§ã対å¿ã§ããããã±ã¼ã¸ãæä¾ããã¦ãããã¨ãç¥ãã¾ããã
調ã¹ã¦ããä¸ã§ã»ãã«ã対å¿æ³ããããã¨ããããã¾ããã
Jest ã®ã¦ããããã¹ãå ¥éããã°ããã§ãããã»ãã«ãã¨ã©ã¼ã«ããã£ããã¾ãä½ãè¨äºã«ãããã¨æãã¾ãã
Â