{"id":2469,"date":"2022-03-25T12:00:17","date_gmt":"2022-03-25T03:00:17","guid":{"rendered":"https:\/\/weseek.co.jp\/tech\/?p=2469"},"modified":"2023-04-17T11:14:47","modified_gmt":"2023-04-17T02:14:47","slug":"jest%e3%81%a7%e3%83%86%e3%82%b9%e3%83%88%e3%82%92%e6%9b%b8%e3%81%84%e3%81%a6%e3%81%bf%e3%82%88%e3%81%86%ef%bc%88%e5%ae%9f%e8%b7%b5%e7%b7%a8%ef%bc%89-2","status":"publish","type":"post","link":"https:\/\/weseek.co.jp\/tech\/2469\/","title":{"rendered":"Jest\u3067\u30c6\u30b9\u30c8\u3092\u66f8\u3044\u3066\u307f\u3088\u3046\uff08\u5b9f\u8df5\u7de8\uff09"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u30a8\u30f3\u30b8\u30cb\u30a2\u306eYohei\u3067\u3059\u3002<\/p>\n<p>\u65e9\u901f\u3067\u3059\u304c\u3001\u7686\u3055\u3093\u306f\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u305f\u3053\u3068\u306f\u3042\u308a\u307e\u3059\u304b\uff1f<\/p>\n<p>\u30c6\u30b9\u30c8\u3092\u66f8\u304f\u3068\u306a\u308b\u3068\u8170\u304c\u91cd\u304f\u306a\u3063\u3066\u3057\u307e\u3046\u65b9\u3082\u3044\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u5b9f\u306f\u30c6\u30b9\u30c8\u3092\u66f8\u304f\u3068\u826f\u3044\u3053\u3068\u304c\u305f\u304f\u3055\u3093\u3042\u308b\u3093\u3067\u3059\u3002<\/p>\n<p>\u30c6\u30b9\u30c8\u306b\u3064\u3044\u3066\u3088\u304f\u308f\u304b\u3089\u306a\u3044\u65b9\u3001\u3053\u308c\u304b\u3089\u30c6\u30b9\u30c8\u3092\u66f8\u304d\u59cb\u3081\u308b\u3068\u3044\u3046\u65b9\u306f\u305c\u3072\u8aad\u3093\u3067\u307f\u3066\u304f\u3060\u3055\u3044\uff01<\/p>\n<p>\u524d\u534a\u306f\u3001\u30c6\u30b9\u30c8\u306e\u6982\u8981\u306b\u3064\u3044\u3066\u8aac\u660e\u3057\u307e\u3059\u3002<\/p>\n<p>\u5f8c\u534a\u306f\u3001Javascript\u306e \u30c6\u30b9\u30c6\u30a3\u30f3\u30b0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3042\u308b Jest \u3092\u4f7f\u3063\u3066\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u3092\u4f8b\u306b\u8aac\u660e\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u306f\u30c6\u30b9\u30c8\u306e\u6982\u8981\u306b\u3064\u3044\u3066\u7c21\u5358\u306b\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><!--more--><\/p>\n\n<h1>\u30c6\u30b9\u30c8\u3068\u306f<\/h1>\n<p>\u203b\u5b66\u6821\u3067\u3084\u308b\u30c6\u30b9\u30c8\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>\u3053\u3053\u3067\u306e\u30c6\u30b9\u30c8\u3068\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30c6\u30b9\u30c8\u3067\u3059\u3002<\/p>\n<p>\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30c6\u30b9\u30c8\u3068\u306f\u3001\u5b9f\u88c5\u3057\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u610f\u56f3\u3057\u305f\u901a\u308a\u306b\u52d5\u3044\u3066\u3044\u308b\u304b\u3092\u691c\u8a3c\u3059\u308b\u305f\u3081\u306b\u884c\u3046\u3082\u306e\u3067\u3059\u3002<\/p>\n<h1>\u30c6\u30b9\u30c8\u306e\u7a2e\u985e<\/h1>\n<p>\u30c6\u30b9\u30c8\u306b\u306f\u4e3b\u306b\uff13\u3064\u306e\u7a2e\u985e\u304c\u5b58\u5728\u3057\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5206\u985e\u3067\u304d\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u5358\u4f53\u30c6\u30b9\u30c8<\/li>\n<li>\u7d50\u5408\u30c6\u30b9\u30c8<\/li>\n<li>\u30b7\u30b9\u30c6\u30e0\u30c6\u30b9\u30c8<\/li>\n<\/ul>\n<p>\u3053\u308c\u3089\u306f\u3088\u304f\u300c\u30c6\u30b9\u30c8\u30ec\u30d9\u30eb\u300d\u3068\u547c\u3070\u308c\u308b\u305d\u3046\u3067\u3059\u3002<\/p>\n<h2>\u5404\u30c6\u30b9\u30c8\u30ec\u30d9\u30eb\u306e\u5f79\u5272<\/h2>\n<ul>\n<li>\u5358\u4f53\u30c6\u30b9\u30c8\uff1a\u95a2\u6570\u306a\u3069\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u90e8\u54c1\u306e\u6700\u5c0f\u5358\u4f4d\u306e\u30c6\u30b9\u30c8<\/li>\n<li>\u7d50\u5408\u30c6\u30b9\u30c8\uff1a\u95a2\u6570\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u6a5f\u80fd\u306e\u30c6\u30b9\u30c8<\/li>\n<li>\u30b7\u30b9\u30c6\u30e0\u30c6\u30b9\u30c8\uff1a \u6a5f\u80fd\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u3001\u30b7\u30b9\u30c6\u30e0\u306e\u30c6\u30b9\u30c8<\/li>\n<\/ul>\n<p>\u4e0a\u304b\u3089\u4e0b\u306b\u5411\u304b\u3063\u3066\u30c6\u30b9\u30c8\u304c\u5bfe\u8c61\u3068\u3059\u308b\u6a5f\u80fd\u306e\u7bc4\u56f2\u304c\u5927\u304d\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<h1>\u30c6\u30b9\u30c8\u306e\u6280\u6cd5<\/h1>\n<p>\u30c6\u30b9\u30c8\u306e\u6280\u6cd5\u3068\u306f\u30c6\u30b9\u30c8\u306e\u4ed5\u65b9\u3092\u610f\u5473\u3057\u307e\u3059\u3002<\/p>\n<h2>\u30c6\u30b9\u30c8\u306e\u6280\u6cd5\u306e\u7a2e\u985e<\/h2>\n<p>\u7c21\u5358\u306b\u4ee5\u4e0b\uff12\u3064\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30db\u30ef\u30a4\u30c8\u30dc\u30c3\u30af\u30b9\u30c6\u30b9\u30c8<\/li>\n<li>\u30d6\u30e9\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u30c6\u30b9\u30c8<\/li>\n<\/ul>\n<h3>\u30db\u30ef\u30a4\u30c8\u30dc\u30c3\u30af\u30b9\u30c6\u30b9\u30c8<\/h3>\n<p>\u30b7\u30b9\u30c6\u30e0\u5185\u90e8\u306e\u69cb\u9020\u3092\u7406\u89e3\u3057\u305f\u4e0a\u3067\u3001\u305d\u308c\u3089\u304c\u3061\u3083\u3093\u3068\u610f\u56f3\u901a\u308a\u306b\u52d5\u4f5c\u3059\u308b\u304b\u3092\u78ba\u8a8d\u3059\u308b\u30c6\u30b9\u30c8\u306e\u65b9\u6cd5\u3092\u30db\u30ef\u30a4\u30c8\u30dc\u30c3\u30af\u30b9\u30c6\u30b9\u30c8\u3068\u547c\u3073\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3042\u308b\u95a2\u6570\u306e\u4e2d\u3067\u51e6\u7406A\u304c\u5b9f\u884c\u3055\u308c\u305f\u3089\u3001\u6b21\u306b\u51e6\u7406B\u5b9f\u884c\u3055\u308c\u3001\u6700\u5f8c\u306b\u51e6\u7406C\u304c\u5b9f\u884c\u3055\u308c\u308b\u306f\u305a\u3060\u3002\u3068\u3044\u3063\u305f\u3053\u3068\u3092\u30c6\u30b9\u30c8\u3057\u307e\u3059\u3002<\/p>\n<p>\u7279\u5fb4\u306f\u30b7\u30b9\u30c6\u30e0\u306e\u4e2d\u306e\u69cb\u9020\u306b\u7740\u76ee\u3057\u3066\u3044\u308b\u3053\u3068\u3067\u3059\u3002<\/p>\n<h3>\u30d6\u30e9\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u30c6\u30b9\u30c8<\/h3>\n<p>\u30b7\u30b9\u30c6\u30e0\u5185\u90e8\u306e\u69cb\u9020\u3092\u7406\u89e3\u3059\u308b\u5fc5\u8981\u306f\u306a\u304f\u3001\u3053\u308c\u3092\u3057\u305f\u3089\u7d50\u679c\u306f\u3053\u3046\u306a\u308b\u3088\u306d\u3068\u3001\u3068\u3044\u3046\u610f\u56f3\u901a\u308a\u306b\u30b7\u30b9\u30c6\u30e0\u304c\u52d5\u4f5c\u3059\u308b\u304b\u3092\u78ba\u8a8d\u3059\u308b\u30c6\u30b9\u30c8\u306e\u65b9\u6cd5\u3092\u30d6\u30e9\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u30c6\u30b9\u30c8\u3068\u547c\u3073\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u6570\u5b57\u30922\u3064\u6e21\u3057\u305f\u3089\u3001\u8db3\u3057\u7b97\u3092\u3057\u305f\u7d50\u679c\u3092\u8fd4\u3057\u3066\u304f\u308c\u308b\u95a2\u6570\u304c\u3042\u308b\u3068\u3057\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u95a2\u6570\u306b\u6570\u5b571\u30682\u3092\u6e21\u3057\u305f\u30893\u304c\u8fd4\u3055\u308c\u308b\u306f\u305a\u3060\u3001\u3068\u8a00\u3046\u3053\u3068\u3092\u30c6\u30b9\u30c8\u3057\u307e\u3059\u3002<\/p>\n<p>\u7279\u5fb4\u306f\u30b7\u30b9\u30c6\u30e0\u306e\u5916\u304b\u3089\u898b\u305f\u4ed5\u69d8\u306b\u7740\u76ee\u3057\u3066\u3044\u308b\u3053\u3068\u3067\u3059\u3002<\/p>\n<h3>\u305d\u306e\u4ed6<\/h3>\n<p>\u30b0\u30ec\u30fc\u30dc\u30c3\u30af\u30b9\u30c6\u30b9\u30c8\u3084\u30dc\u30c8\u30e0\u30a2\u30c3\u30d7\u30c6\u30b9\u30c8\u3001\u30c8\u30c3\u30d7\u30c0\u30a6\u30f3\u30c6\u30b9\u30c8\u3068\u547c\u3070\u308c\u308b\u6280\u6cd5\u3082\u5b58\u5728\u3057\u307e\u3059\u3002\u6c17\u306b\u306a\u308b\u65b9\u306f\u305c\u3072\u8abf\u3079\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><span id=\"test-code-pro-con\"><\/span><\/p>\n<h1>\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u30e1\u30ea\u30c3\u30c8\u30c7\u30e1\u30ea\u30c3\u30c8<\/h1>\n<h2>\u30e1\u30ea\u30c3\u30c8<\/h2>\n<p>\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u30e1\u30ea\u30c3\u30c8\u306f\u69d8\u3005\u3067\u3059\u304c\u3001 \u6700\u7d42\u7684\u306b\u306f\u30b5\u30fc\u30d3\u30b9\u3092\u5229\u7528\u3057\u3066\u304f\u308c\u308b\u30e6\u30fc\u30b6\u69d8\u306e\u305f\u3081\u306b\u306a\u308b\u3068\u3044\u3046\u3068\u3053\u308d\u306b\u5e30\u7d50\u3059\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>\u3067\u306f\u3069\u306e\u3088\u3046\u306a\u30e1\u30ea\u30c3\u30c8\u304c\u3042\u308b\u306e\u304b\u898b\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b3\u30fc\u30c9\u306e\u54c1\u8cea\u5411\u4e0a<\/li>\n<li>\u30d0\u30b0\u306e\u65e9\u671f\u767a\u898b<\/li>\n<li>\u30d0\u30b0\u306e\u5c11\u306a\u3044\u6a5f\u80fd\u958b\u767a<\/li>\n<li>\u30c7\u30b0\u30ec\u9632\u6b62<\/li>\n<li>\u5fc3\u7406\u7684\u5b89\u5fc3<\/li>\n<\/ul>\n<h2>\u30c7\u30e1\u30ea\u30c3\u30c8<\/h2>\n<ul>\n<li>\u958b\u767a\u306e\u30b9\u30d4\u30fc\u30c9\u304c\u843d\u3061\u308b<\/li>\n<li>\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u5b9f\u88c5\u306b\u6642\u9593\u304c\u304b\u304b\u308b<\/li>\n<li>\u4ed5\u69d8\u5909\u66f4\u306b\u3088\u308b\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9<\/li>\n<\/ul>\n<p>\u203b\u958b\u767a\u306e\u30b9\u30d4\u30fc\u30c9\u306b\u3064\u3044\u3066\u306f\u5fc5\u305a\u3057\u3082\u843d\u3061\u308b\u3068\u306f\u8a00\u3044\u5207\u308c\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u30d0\u30b0\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u306f\u305d\u306e\u4fee\u6b63\u306e\u30b3\u30b9\u30c8\u304c\u767a\u751f\u3059\u308b\u304b\u3089\u3067\u3059\u3002<\/p>\n<hr \/>\n<p>\u30c6\u30b9\u30c8\u306e\u304a\u304a\u3088\u305d\u306e\u6982\u8981\u306f\u3064\u304b\u3081\u305f\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u3067\u306f\u5b9f\u8df5\u306b\u79fb\u308a\u307e\u3057\u3087\u3046\uff01<\/p>\n<h1>\u5b9f\u8df5\u7de8\uff08Jest\uff09<\/h1>\n<p>\u5f53\u8a18\u4e8b\u3067\u306f\u3001 Node.js \u4e0a\u3067\u30c6\u30b9\u30c6\u30a3\u30f3\u30b0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3042\u308b Jest \u3092\u4f7f\u3063\u3066\u30c6\u30b9\u30c8\u3092\u66f8\u3044\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<h2>\u6e96\u5099<\/h2>\n<p>\u307e\u305a\u306f\u3001\u4ee5\u4e0b\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u203b Node.js \u3067\u306e\u958b\u767a\u3092\u524d\u63d0\u3068\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>\u30e9\u30a4\u30d6\u30e9\u30ea<\/h3>\n<ul>\n<li><a href=\"https:\/\/jestjs.io\/docs\/getting-started\">Jest<\/a>\n<ul>\n<li>Javascript \u306e\u30c6\u30b9\u30c6\u30a3\u30f3\u30b0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3059\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u5f8c\u3001package.json \u306b\u4ee5\u4e0b\u3092\u8ffd\u52a0\u3057\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code>{\n  &quot;scripts&quot;: {\n    &quot;test&quot;: &quot;jest&quot;\n  }\n}<\/code><\/pre>\n<h3>\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u52d5\u4f5c\u3092\u30c6\u30b9\u30c8\u3092\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3092\u307e\u3068\u3081\u305f TestService class \u3092\u5b9a\u7fa9\u3057\u305f <code>index.js<\/code> \u30d5\u30a1\u30a4\u30eb\u3068\u3001\u30c6\u30b9\u30c8\u3092\u3059\u308b\u305f\u3081\u306e <code>service.test.js<\/code> \u7528\u610f\u3057\u307e\u3059\u3002<\/p>\n<p>\u30c6\u30b9\u30c8\u3092\u76ee\u7684\u3068\u3057\u3066\u3044\u308b\u306e\u3067\u30b3\u30fc\u30c9\u3092\u6df1\u304f\u7406\u89e3\u3059\u308b\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3002\u30b3\u30d4\u30fc\uff06\u30da\u30fc\u30b9\u30c8\u3067\u9032\u3093\u3067\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><code>index.js<\/code><\/p>\n<pre><code class=\"language-javascript\">class TestService {\n\n  multiplyNum(num, multiplyBy) {\n    return num * multiplyBy;\n  }\n\n  saveNum(num, saveTo) {\n    return new Promise((resolve) =&gt; {\n      setTimeout(() =&gt; {\n        saveTo.push(num);\n        resolve(&#039;Saved&#039;);\n      }, 100);\n    })\n  }\n\n  multiplyAndSave(num, multiplyBy, saveTo) {\n    const res = this.multiplyNum(num, multiplyBy);\n    this.saveNum(res, saveTo);\n    return res;\n  }\n}\n\nmodule.exports = new TestService();<\/code><\/pre>\n<hr \/>\n<p><code>service.test.js<\/code><\/p>\n<pre><code class=\"language-javascript\">\/\/ \u30c6\u30b9\u30c8\u5bfe\u8c61\u306e TestService \u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092 index.js \u304b\u3089\u8aad\u307f\u8fbc\u3080\nconst service = require(&#039;.\/index&#039;);\n\n\/\/ \u3053\u3053\u304b\u3089\u4e0b\u306b\u30c6\u30b9\u30c8\u3092\u8a18\u8ff0<\/code><\/pre>\n<h4>\u89e3\u8aac<\/h4>\n<p>\u5404\u30e1\u30bd\u30c3\u30c9\u3092\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li>multiplyNum\n<ul>\n<li>\u7b2c1\u5f15\u6570\u306e\u6570\u5024\u3092\u3001\u7b2c2\u5f15\u6570\u306e\u6570\u5024\u3067\u639b\u3051\u7b97\u3057\u3001\u7d50\u679c\u306e\u6570\u5024\u3092\u8fd4\u3059\u30e1\u30bd\u30c3\u30c9\u3002<\/li>\n<\/ul>\n<\/li>\n<li>saveNum\n<ul>\n<li>Promise\u3092\u8fd4\u3059\u975e\u540c\u671f\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/li>\n<li>0.1 \u79d2\u5f8c\u306b\u7b2c1\u5f15\u6570\u306e\u6570\u5024\u3092\u3001\u7b2c2\u5f15\u6570\u306e\u914d\u5217\u306b\u683c\u7d0d\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n<\/li>\n<li>multiplyAndSave\n<ul>\n<li>\u540c\u671f\u7684\u306b<code>multiplyNum<\/code>\u3092\u5b9f\u884c\u3057\u3001\u975e\u540c\u671f\u7684\u306b<code>saveNum<\/code>\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/li>\n<li><code>multiplyNum<\/code>\u306e\u8fd4\u308a\u5024\u3092\u8fd4\u3059\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/li>\n<li>\u8a08\u7b97\u306f\u3059\u3050\u3057\u3066\u6b32\u3057\u3044\u3051\u3069\u3001\u30c7\u30fc\u30bf\u306e\u4fdd\u5b58\u306f\u88cf\u3067\u3088\u3057\u306a\u306b\u3084\u3063\u3068\u3044\u3066\u301c\u3001\u306a\u30b7\u30c1\u30e5\u30a8\u30fc\u30b7\u30e7\u30f3\u3092\u60f3\u5b9a\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u5b9f\u8df5 \u57fa\u790e\u7de8<\/h2>\n<p>\u6700\u521d\u306b\u30c6\u30b9\u30c8\u7528\u306e\u4e0b\u5730\u3092\u7528\u610f\u3057\u307e\u3059\u3002<\/p>\n<p><code>service.test.js<\/code> \u306b\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><code>describe(&#039;TestService&#039;, () =&gt; {\n    \/\/ \u3053\u3053\u306b\u30c6\u30b9\u30c8\u3092\u8ffd\u52a0\n});<\/code><\/pre>\n<p><a href=\"https:\/\/jestjs.io\/ja\/docs\/api#describename-fn\">describe<\/a> \u306f\u3044\u304f\u3064\u304b\u306e\u95a2\u9023\u3059\u308b\u30c6\u30b9\u30c8\u3092\u307e\u3068\u3081\u308b\u305f\u3081\u306e\u30d6\u30ed\u30c3\u30af\u3067\u3001\u3053\u306e\u4e2d\u306b\u30c6\u30b9\u30c8\u3092\u66f8\u3044\u3066\u3044\u304d\u307e\u3059\u3002\u4eca\u56de\u306f<code>TestService<\/code>\u3068\u3044\u3046\u30d6\u30ed\u30c3\u30af\u540d\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<h3>test1<\/h3>\n<p>\u4e0b\u5730\u304c\u3067\u304d\u305f\u306e\u3067\u3001\u307e\u305a\u306f<code>multiplyNum<\/code>\u30e1\u30bd\u30c3\u30c9\u304c\u6b63\u3057\u304f\u52d5\u4f5c\u3059\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u5185\u5bb9\u3067\u30c6\u30b9\u30c8\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<ul>\n<li>2 x 2 \u306f\uff14\u306b\u306a\u308b<\/li>\n<\/ul>\n<p><code>service.test.js<\/code>\u306e describe \u5185\u306b\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8a18\u8ff0\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><code class=\"language-javascript\">test(&#039;2 times 2 should be 4&#039;, () =&gt; {\n  const result = service.multiplyNum(2, 2);\n  expect(result).toBe(4);\n});<\/code><\/pre>\n<h4>\u89e3\u8aac<\/h4>\n<p>\u4e0a\u8a18\u3092\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n<p>\u30c6\u30b9\u30c8\u3092\u66f8\u304f\u3068\u304d\u306f <a href=\"https:\/\/jestjs.io\/ja\/docs\/api#testname-fn-timeout\">test<\/a> \u3068\u66f8\u304d\u307e\u3059(<code>it<\/code>\u3067\u3082\u540c\u69d8)\u3002<\/p>\n<blockquote>\n<p>\u7b2c1\u5f15\u6570\u306b\u30c6\u30b9\u30c8\u540d\u3092\u3001\u7b2c2\u5f15\u6570\u306b\u30c6\u30b9\u30c8\u306e\u78ba\u8a8d\u9805\u76ee\u3092\u542b\u3080\u95a2\u6570\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002 3\u756a\u76ee\u306e\u5f15\u6570 (\u4efb\u610f) \u306f \u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u5024 (\u30df\u30ea\u79d2\u5358\u4f4d) \u3067\u3001\u4e2d\u6b62\u3059\u308b\u307e\u3067\u306e\u5f85\u3061\u6642\u9593\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002 \u6ce8\u610f: \u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u306f5\u79d2\u3067\u3059\u3002<\/p>\n<\/blockquote>\n<pre><code class=\"language-javascript\">const result = service.multiplyNum(2, 2);<\/code><\/pre>\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u306f\u898b\u3066\u306e\u901a\u308a<code>multiplyNum<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3066\u8fd4\u308a\u5024\u3092\u53d7\u3051\u53d6\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u30e1\u30bd\u30c3\u30c9\u304c\u6b63\u3057\u304f\u52d5\u4f5c\u3057\u3066\u3044\u308c\u3070 result \u306b\u306f 4 \u304c\u5165\u308b\u306f\u305a\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u3001<code>\u3007\u3007\u306a\u306f\u305a\u3060<\/code>\u3001\u3068\u3044\u3046<code>\u671f\u5f85<\/code>\u3092\u66f8\u304f\u306e\u304c\u30c6\u30b9\u30c8\u306b\u306a\u308a\u307e\u3059\u3002\u305d\u306e\u30b3\u30fc\u30c9\u304c\u4ee5\u4e0b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-javascript\">expect(result).toBe(4);<\/code><\/pre>\n<p>expect(A).toBe(B) =&gt; A \u306f B \u306a\u306f\u305a\u3060\u3001\u3068\u3044\u3046\u671f\u5f85\u3067\u3059\u3002<\/p>\n<p>\u3067\u306f\u5b9f\u969b\u306b\u30c6\u30b9\u30c8\u3092\u5b9f\u884c\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<h4>\u5b9f\u884c<\/h4>\n<p>\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre><code>npm test\nor\nyarn test<\/code><\/pre>\n<p>\u6210\u529f\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30ed\u30b0\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre><code>% npm test\n\n> @ test \/Users\/*****\/jest-test\n> jest\n\nPASS  .\/service.test.js\nTestService\n  \u2713 2 times 2 should be 4\n\nTest Suites: 1 passed, 1 total\nTests:       1 passed, 1 total<\/code><\/pre>\n<h3>test2<\/h3>\n<p>\u3067\u306f\u3001\u6570\u5024\u306b\u30de\u30a4\u30ca\u30b9\uff12\u3092\u304b\u3051\u305f\u5834\u5408\u3082\u540c\u69d8\u306b\u52d5\u304f\u3067\u3057\u3087\u3046\u304b\u3002\u4e0a\u8a18\u306e test \u306b\u7d9a\u3051\u3066\u30c6\u30b9\u30c8\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code class=\"language-javascript\">test(&#039;2 times 2 should be 4&#039;, () =&gt; {\n  ...\n}\ntest(&#039;2 times negative 2 should be negative 4&#039;, () =&gt; {\n  const result = service.multiplyNum(2, -2);\n  expect(result).toBe(-4);\n});<\/code><\/pre>\n<h4>\u5b9f\u884c<\/h4>\n<pre><code>PASS  .\/service.test.js\nTestService\n  \u2713 2 times 2 should be 4 (1 ms)\n  \u2713 2 times negative 2 should be negative 4\n\nTest Suites: 1 passed, 1 total\nTests:       2 passed, 2 total<\/code><\/pre>\n<p>\u7121\u4e8b\u306b\u6210\u529f\u3057\u305f\u3088\u3046\u3067\u3059\u3002<\/p>\n<h3>test3<\/h3>\n<p>\u6b21\u306b\u975e\u540c\u671f\u30e1\u30bd\u30c3\u30c9\u306e<code>saveNum<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u30c6\u30b9\u30c8\u3057\u307e\u3059\u3002<code>saveNum<\/code>\u30e1\u30bd\u30c3\u30c9\u304c\u6b63\u3057\u304f\u52d5\u4f5c\u3059\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3059\u308b\u305f\u3081\u306b\u4ee5\u4e0b\u306e\u5185\u5bb9\u3067\u30c6\u30b9\u30c8\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<ul>\n<li>\u6570\u5024\u306e 100 \u3068 \u7a7a\u306e\u914d\u5217 \u3092\u6e21\u3057\u3066\u51e6\u7406\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u51e6\u7406\u5f8c\u306e\u914d\u5217\u306b\u306f\u6570\u5024\u306e 100 \u304c\u683c\u7d0d\u3055\u308c\u3066\u3044\u308b\u3002<\/li>\n<\/ul>\n<p>\u975e\u540c\u671f\u30e1\u30bd\u30c3\u30c9\u3082\u66f8\u304d\u65b9\u306f\u307b\u307c\u540c\u3058\u3067\u3059\u3002\u3082\u3057 await \u3092\u3059\u308b\u5834\u5408\u306f async \u3092 test \u30e1\u30bd\u30c3\u30c9\u306e\u7b2c2\u5f15\u6570\u306e\u59cb\u3081\u306b\u3064\u3051\u3066\u3042\u3052\u308b\u3060\u3051\u3067\u3059\u3002<\/p>\n<pre><code class=\"language-javascript\">test(&#039;number should be saved&#039;, async () =&gt; {\n  const saveTo = [];\n  await service.saveNum(100, saveTo);\n\n  const expected = [100];\n  expect(expected).toEqual(expect.arrayContaining(saveTo));\n});<\/code><\/pre>\n<h4>\u89e3\u8aac<\/h4>\n<p>\u5206\u89e3\u3057\u3066\u307f\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u4ee5\u4e0b\u306f\u7a7a\u306e\u914d\u5217\u3092\u7528\u610f\u3057\u3001<code>saveNum<\/code>\u30e1\u30bd\u30c3\u30c9\u306e\u5f15\u6570\u306b \u6570\u5b57\u306e100\u3068\u4e00\u7dd2\u306b\u6e21\u3057\u3066\u51e6\u7406\u3092\u5b9f\u884c\u3057\u3066\u3044\u308b\u3060\u3051\u3067\u3059\u3002<\/p>\n<pre><code>const saveTo = [];\nawait service.saveNum(100, saveTo);<\/code><\/pre>\n<p>\u6b21\u306e\u30b3\u30fc\u30c9\u304c\u8aad\u307f\u306b\u304f\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u5b9f\u306f\u7c21\u5358\u3067\u3059\u3002<\/p>\n<pre><code class=\"language-javascript\">const expected = [100];\nexpect(saveTo).toEqual(expect.arrayContaining(expected));<\/code><\/pre>\n<p><code>expect<\/code>\u306b\u6e21\u3057\u305f\u914d\u5217<code>saveTo<\/code>\u304c\u3001<code>arrayContaining<\/code>\u306b\u6e21\u3057\u305f\u914d\u5217<code>expected<\/code>\u304c\u6301\u3064\u8981\u7d20\u3092\u542b\u3093\u3067\u3044\u308b\u3053\u3068\u3092\u671f\u5f85\u3057\u307e\u3059\u3002<\/p>\n<h4>\u5b9f\u884c<\/h4>\n<p>\u3067\u306f\u30c6\u30b9\u30c8\u3092\u5b9f\u884c\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code>PASS  .\/service.test.js\nTestService\n  \u2713 2 times 2 should be 4 (1 ms)\n  \u2713 2 times negative 2 should be negative 4 (1 ms)\n  \u2713 number should be saved (105 ms)\n\nTest Suites: 1 passed, 1 total\nTests:       3 passed, 3 total<\/code><\/pre>\n<p>\u3053\u3061\u3089\u306e\u30c6\u30b9\u30c8\u3082\u7121\u4e8b\u6210\u529f\u3057\u307e\u3057\u305f\u3002<\/p>\n<hr \/>\n<p>\u305d\u306e\u4ed6\u306b\u3082\u591a\u304f\u306e expect \u30e1\u30bd\u30c3\u30c9\u304c\u5b58\u5728\u3059\u308b\u306e\u3067\u6c17\u306b\u306a\u308b\u65b9\u306f\u4ee5\u4e0b\u3092\u3054\u53c2\u8003\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u8a73\u7d30\u306f\u3053\u3061\u3089\uff1a<a href=\"https:\/\/jestjs.io\/ja\/docs\/expect\">https:\/\/jestjs.io\/ja\/docs\/expect<\/a><\/p>\n<h2>\u5b9f\u8df5 \u5fdc\u7528\u7de8<\/h2>\n<p>\u6700\u5f8c\u306b\u5c11\u3057\u30c8\u30ea\u30c3\u30ad\u30fc\u306a<code>service.multiplyAndSave<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u30c6\u30b9\u30c8\u3057\u307e\u3059\u3002<\/p>\n<p>\u203b\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u5185\u90e8\u3067\u975e\u540c\u671f\u30e1\u30bd\u30c3\u30c9\u3067\u3042\u308b<code>saveNum<\/code>\u30e1\u30bd\u30c3\u30c9 \u3092 await \u3092\u305b\u305a\u306b\u5b9f\u884c\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u306f\u3053\u308c\u307e\u3067\u901a\u308a\u66f8\u3044\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>test4<\/h3>\n<pre><code>test(&#039;returns array containing a multiplied number&#039;, async () =&gt; {\n  const number = 10;\n  const multiplyBy = 10;\n  const saveTo = [];\n  const numAfterMultiplied = await service.multiplyAndSave(number, multiplyBy, saveTo);\n\n  const expectedNumAfterMultiplied = 100;\n  const expectedArray = [100];\n\n  expect(numAfterMultiplied).toBe(expectedNumAfterMultiplied);\n  expect(saveTo).toEqual(expect.arrayContaining(expectedArray));\n});<\/code><\/pre>\n<h4>\u5b9f\u884c<\/h4>\n<p>\u30c6\u30b9\u30c8\u3092\u5b9f\u884c\u3059\u308b\u3068\u5931\u6557\u3057\u307e\u3059\u3002<\/p>\n<pre><code>FAIL  .\/service.test.js\nTestService\n  \u2715 returns array containing a multiplied number (2 ms)\n\n\u25cf TestService \u203a returns array containing a multiplied number\n\n  expect(received).toEqual(expected) \/\/ deep equality\n\n  Expected: ArrayContaining [100]\n  Received: []\n\n    37 |\n    38 |     expect(numAfterMultiplied).toBe(expectedNumAfterMultiplied);\n  &gt; 39 |     expect(saveTo).toEqual(expect.arrayContaining(expectedArray));\n       |                    ^\n    40 |   })\n    41 | })\n\n    at Object.&lt;anonymous&gt; (service.test.js:39:20)\n\nTest Suites: 1 failed, 1 total\nTests:       1 failed, 3 passed, 4 total<\/code><\/pre>\n<h4>\u89e3\u8aac<\/h4>\n<p><code>multiplyAndSave<\/code>\u30e1\u30bd\u30c3\u30c9\u306e\u5185\u90e8\u3067\u975e\u540c\u671f\u306b\u5b9f\u884c\u3057\u3066\u3044\u308b <code>saveNum<\/code>\u30e1\u30bd\u30c3\u30c9\u306f Promise \u3092\u8fd4\u3059\u975e\u540c\u671f\u306a\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002\u3064\u307e\u308a\u3001Promise \u304c\u89e3\u6c7a\u3055\u308c\u3066\u3044\u306a\u304f\u3066\u3082\u51e6\u7406\u306f\u6b21\u306b\u9032\u3093\u3067\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u51e6\u7406\u304c expect \u3092\u5b9f\u884c\u3057\u305f\u6bb5\u968e\u3067\u306f\u307e\u3060 Promise \u304c\u89e3\u6c7a\u3055\u308c\u3066\u3044\u306a\u3044\u305f\u3081\u3001<code>saveTo<\/code>\u5909\u6570\u306f\u7a7a\u306e\u914d\u5217\u306e\u307e\u307e<code>expectedArray<\/code>\u306e\u5024\u3068\u6bd4\u8f03\u3055\u308c\u3066\u3057\u307e\u3044\u3001 expect \u306e\u6761\u4ef6\u3092\u6e80\u305f\u3057\u3066\u3044\u306a\u3044\u305f\u3081\u30c6\u30b9\u30c8\u304c\u5931\u6557\u3057\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002<\/p>\n<p>\u3053\u308c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b<code>saveNum<\/code>\u30e1\u30bd\u30c3\u30c9\u306e mock\u5316\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n<h3>mock\u95a2\u6570\u3067\u3067\u304d\u308b\u3053\u3068<\/h3>\n<ul>\n<li>\u95a2\u6570\u304c\u6301\u3064\u5b9f\u969b\u306e\u5b9f\u88c5\u3092\u9664\u53bb<\/li>\n<li>\u95a2\u6570\u306e\u547c\u3073\u51fa\u3057\uff08\u307e\u305f\u3001\u547c\u3073\u51fa\u3057\u6642\u306b\u6e21\u3055\u308c\u305f\u30d1\u30e9\u30e1\u30fc\u30bf\u3082\u542b\u3081\uff09\u3092\u30ad\u30e3\u30d7\u30c1\u30e3<\/li>\n<li>new \u306b\u3088\u308b\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u95a2\u6570\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3092\u30ad\u30e3\u30d7\u30c1\u30e3<\/li>\n<\/ul>\n<p>\u306a\u3069\u306a\u3069\u66f8\u3044\u3066\u3042\u308a\u307e\u3059\u304c\u3001\u3064\u307e\u308a\u306f\u30e1\u30bd\u30c3\u30c9\u306e\u632f\u308b\u821e\u3044\u3092\u5909\u66f4\u3067\u304d\u305f\u308a\u3059\u308b\u308f\u3051\u3067\u3059\u3002<\/p>\n<p>\u8a73\u7d30\u306f\u3053\u3061\u3089\uff1a<a href=\"https:\/\/jestjs.io\/ja\/docs\/mock-functions\">https:\/\/jestjs.io\/ja\/docs\/mock-functions<\/a><\/p>\n<hr \/>\n<p>\u4eca\u56de\u3053\u308c\u3092\u4f7f\u3063\u3066\u3084\u308b\u3053\u3068\u306f\u4ee5\u4e0b\u306e3\u3064\u3067\u3059\u3002<\/p>\n<ol>\n<li>jest.spyOn \u30e1\u30bd\u30c3\u30c9\u3067\u3001<code>saveNum<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4e00\u65e6\u5185\u90e8\u306e\u51e6\u7406\u306f\u4f55\u3082\u3057\u306a\u3044 null \u3092\u8fd4\u3059\u3060\u3051\u306e\u95a2\u6570\u306bmock\u5316<\/li>\n<li>mock \u5316\u3057\u305f\u30e1\u30bd\u30c3\u30c9\u304c<code>multiplyAndSave<\/code>\u30e1\u30bd\u30c3\u30c9\u306e\u5185\u90e8\u3067\u547c\u3073\u51fa\u3055\u308c\u305f\u6642\u306b\u6e21\u3055\u308c\u305f\u5f15\u6570\u3092\u53d6\u5f97<\/li>\n<li><code>saveNum<\/code>\u30e1\u30bd\u30c3\u30c9 \u306e mock \u5316\u3092\u89e3\u6d88\uff08\u5143\u306e\u95a2\u6570\u306e\u72b6\u614b\u306b\u4fee\u6b63\uff09\u3057\u3001\u53d6\u5f97\u3057\u305f\u5f15\u6570\u3068\u5171\u306b<code>saveNum<\/code>\u30e1\u30bd\u30c3\u30c9 \u3092\u500b\u5225\u306b\u547c\u3073\u51fa\u3057<\/li>\n<\/ol>\n<h3>\u4fee\u6b63<\/h3>\n<p>\u30b3\u30fc\u30c9\u3092\u6b21\u306e\u3088\u3046\u306b\u4fee\u6b63\u3057\u307e\u3059\u3002\uff14\u3064\u76ee\u306e\u30c6\u30b9\u30c8\u3092\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u4e0a\u66f8\u304d\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><code>const syncMultiplyAndSave = async (number, multiplyBy, saveTo) =&gt; {\n  const mockSaveNum = jest.spyOn(service, &#039;saveNum&#039;).mockReturnValue(null);\n  const res = await service.multiplyAndSave(number, multiplyBy, saveTo);\n  const argsCalledWithMockSaveNum = mockSaveNum.mock.calls[0];\n  mockSaveNum.mockRestore();\n  await service.saveNum(...argsCalledWithMockSaveNum);\n  return res;\n}\ntest(&#039;returns array containing a multiplied number&#039;, async () =&gt; {\n  const number = 10;\n  const multiplyBy = 10;\n  const saveTo = [];\n  const numAfterMultiplied = await syncMultiplyAndSave(number, multiplyBy, saveTo);\n\n  const expectedNumAfterMultiplied = 100;\n  const expectedArray = [100];\n\n  expect(numAfterMultiplied).toBe(expectedNumAfterMultiplied);\n  expect(saveTo).toEqual(expect.arrayContaining(expectedArray));\n});<\/code><\/pre>\n<h4>\u89e3\u8aac<\/h4>\n<p>\u65b0\u305f\u306b<code>syncMultiplyAndSave<\/code>\u95a2\u6570\u3092\u5b9a\u7fa9\u3057\u3001<code>service.multiplyAndSave<\/code>\u30e1\u30bd\u30c3\u30c9\u306e\u4ee3\u308f\u308a\u306b\u547c\u3073\u51fa\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h5>mock\u5316<\/h5>\n<pre><code>const mockSaveNum = jest.spyOn(service, &#039;saveNum&#039;).mockReturnValue(null);<\/code><\/pre>\n<p>\u3053\u308c\u306f\u3001<code>syncMultiplyAndSave<\/code>\u30e1\u30bd\u30c3\u30c9\u5185\u3067\u3001<code>saveNum<\/code>\u30e1\u30bd\u30c3\u30c9\u3092mock\u5316\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><code>jest.spyOn<\/code>\u306f mock\u95a2\u6570\u3092\u8fd4\u3057\u307e\u3059\u3002\u3055\u3089\u306b\u3001<code>.mockReturnValue(null)<\/code>\u3067 mock\u95a2\u6570\u304c\u547c\u3073\u51fa\u3055\u308c\u305f\u969b\u306e\u8fd4\u308a\u5024\u3092 null \u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h5>\u30e1\u30bd\u30c3\u30c9\u547c\u3073\u51fa\u3057<\/h5>\n<pre><code>const res = await service.multiplyAndSave(number, multiplyBy, saveTo)<\/code><\/pre>\n<p>\u6b21\u306b<code>multiplyAndSave<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u901a\u5e38\u901a\u308a\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u6642\u3001\u5185\u90e8\u3067\u547c\u3070\u308c\u308b<code>saveNum<\/code>\u30e1\u30bd\u30c3\u30c9\u306f mock\u95a2\u6570\u306b\u306a\u3063\u3066\u304a\u308a\u3001null \u3092\u8fd4\u3059\u4ee5\u5916\u306f\u4f55\u3082\u3057\u307e\u305b\u3093\u3002<\/p>\n<h5>\u30e1\u30bd\u30c3\u30c9\u547c\u3073\u51fa\u3057\u6642\u306e\u5f15\u6570\u53d6\u5f97<\/h5>\n<pre><code>const argsCalledWithMockSaveNum = mockSaveNum.mock.calls[0];<\/code><\/pre>\n<p>\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u6642\u306b\u5185\u90e8\u3067\u547c\u3073\u51fa\u3055\u308c\u305f mock\u95a2\u6570\u306f\u3001\u81ea\u8eab\u304c\u547c\u3073\u51fa\u3055\u308c\u305f\u6642\u306b\u6e21\u3055\u308c\u305f\u5f15\u6570\u3092\u8a18\u61b6\u3057\u3066\u3044\u308b\u306e\u3067\u305d\u308c\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002<\/p>\n<p>\u8a73\u7d30\u306f\u3053\u3061\u3089\uff1a<a href=\"https:\/\/jestjs.io\/ja\/docs\/mock-function-api#mockfnmockcalls\">https:\/\/jestjs.io\/ja\/docs\/mock-function-api#mockfnmockcalls<\/a><\/p>\n<h5>mock\u95a2\u6570\u3092\u5143\u306e\u72b6\u614b\u306b\u623b\u3059<\/h5>\n<pre><code>mockSaveNum.mockRestore();<\/code><\/pre>\n<p>\u305d\u306e\u5f8c mock\u95a2\u6570\u306e <code>saveNum<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u5143\u306e\u30e1\u30bd\u30c3\u30c9\u306b\u623b\u3057\u307e\u3059\u3002<\/p>\n<h5>saveNum \u3092 await \u5b9f\u884c<\/h5>\n<p>\u6700\u5f8c\u306b\u3001\u3053\u3053\u307e\u3067\u306b\u53d6\u5f97\u3057\u305f\u5f15\u6570\u3092\u4f7f\u3063\u3066\u3001<code>saveNum<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u500b\u5225\u306b\u547c\u3073\u51fa\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u3053\u3067\u306f\u540c\u671f\u7684\u306b\u5b9f\u884c\u3057\u305f\u3044\u306e\u3067\u3001<code>await<\/code>\u3092\u3064\u3051\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre><code>await service.saveNum(...argsCalledWithMockSaveNum) \/\/ \u914d\u5217\u306a\u306e\u3067 \u30b9\u30d7\u30ec\u30c3\u30c9\u69cb\u6587\u3092\u4f7f\u7528<\/code><\/pre>\n<h4>\u30c6\u30b9\u30c8\u5b9f\u884c<\/h4>\n<p>\u3082\u3046\u4e00\u5ea6\u30c6\u30b9\u30c8\u3092\u5b9f\u884c\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code>PASS  .\/service.test.js\nTestService\n  \u2713 2 times 2 should be 4 (1 ms)\n  \u2713 2 times negative 2 should be negative 4\n  \u2713 number should be saved (101 ms)\n  \u2713 returns array containing a multiplied number (106 ms)\n\nTest Suites: 1 passed, 1 total\nTests:       4 passed, 4 total<\/code><\/pre>\n<p>\u304a\u75b2\u308c\u69d8\u3067\u3059\u3001\u7121\u4e8b\u306b\u6210\u529f\u3057\u307e\u3057\u305f\u3002<\/p>\n<h1>\u307e\u3068\u3081<\/h1>\n<p>\u304a\u75b2\u308c\u69d8\u3067\u3057\u305f\u3002<\/p>\n<p>\u5f53\u8a18\u4e8b\u3067\u306f\u3001\u306a\u308b\u3079\u304f\u7c21\u6613\u306a\u8aac\u660e\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u8a18\u8ff0\u306f\u5c11\u306a\u3081\u306b\u3057\u307e\u3057\u305f\u304c\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u3088\u3063\u3066\u306f\u3001\u3055\u3089\u306b\u30c6\u30b9\u30c8\u7528\u306e\u30c0\u30df\u30fc\u30c7\u30fc\u30bf\u3092\u7528\u3044\u3066\u691c\u8a3c\u3057\u305f\u308a\u3082\u3057\u307e\u3059\uff08\u30e6\u30fc\u30b6\u30fc\u30c7\u30fc\u30bf\u306a\u3069\uff09\u3002<\/p>\n<p>\u4eca\u56de\u79c1\u3082\u5b9f\u969b\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u305f\u3053\u3068\u3067\u3001\u30ea\u30ea\u30fc\u30b9\u524d\u306b\u591a\u304f\u306e\u30d0\u30b0\u306e\u65e9\u671f\u767a\u898b\u30fb\u672a\u7136\u9632\u6b62\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u7279\u306b\u8907\u96d1\u306a\u30b3\u30fc\u30c9\u3092\u7d0d\u671f\u306a\u3069\u304c\u3042\u308b\u4e2d\u3067\u66f8\u3053\u3046\u3068\u3059\u308b\u3068\u3001\u307b\u307c\u5fc5\u305a buggy \u306a\u30b3\u30fc\u30c9\u304c\u7d1b\u308c\u307e\u3059\uff08\u4eba\u9593\u306a\u306e\u3067\u3057\u3087\u3046\u304c\u306a\u3044\u306e\u3067\u3057\u3087\u3046\uff09\u3002<\/p>\n<p>\u6642\u9593\u304c\u306a\u3044\u3068\u304d\u306f\u30d6\u30e9\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u30c6\u30b9\u30c8\u3060\u3051\u3067\u3082\u66f8\u304f\u3068\u3001\u30a2\u30d7\u30ea\u306e\u54c1\u8cea\u3092\u3042\u3052\u308b\u3053\u3068\u304c\u3067\u304d\u30de\u30b9\u306e\u3067\u3001\u307f\u306a\u3055\u3093\u3082\u305c\u3072\u53ef\u80fd\u306a\u7b87\u6240\u304b\u3089\u30c6\u30b9\u30c8\u3092\u66f8\u3044\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u6700\u5f8c\u306b\u6539\u3081\u3066\u30e1\u30ea\u30c3\u30c8\u30fb\u30c7\u30e1\u30ea\u30c3\u30c8\u3092\u63b2\u8f09\u3057\u307e\u3059\u3067\u305c\u3072\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><a href=\"#test-code-pro-con\">\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u30e1\u30ea\u30c3\u30c8\u30c7\u30e1\u30ea\u30c3\u30c8<\/a><\/p>\n<p>\u3053\u3053\u307e\u3067\u3054\u89a7\u3044\u305f\u3060\u304d\u3042\u308a\u304c\u3068\u3046\u3054\u3056\u3044\u307e\u3057\u305f\u3002<\/p>\n<h1>\u53c2\u7167<\/h1>\n<ul>\n<li><a href=\"https:\/\/www.qbook.jp\/academy\/curriculum\/0001\/lessons\/ad00003\/\">https:\/\/www.qbook.jp\/academy\/curriculum\/0001\/lessons\/ad00003\/<\/a><\/li>\n<li><a href=\"https:\/\/jestjs.io\/ja\/\">https:\/\/jestjs.io\/ja\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u30a8\u30f3\u30b8\u30cb\u30a2\u306eYohei\u3067\u3059\u3002 \u65e9\u901f\u3067\u3059\u304c\u3001\u7686\u3055\u3093\u306f\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u305f\u3053\u3068\u306f\u3042\u308a\u307e\u3059\u304b\uff1f \u30c6\u30b9\u30c8\u3092\u66f8\u304f\u3068\u306a\u308b\u3068\u8170\u304c\u91cd\u304f\u306a\u3063\u3066\u3057\u307e\u3046\u65b9\u3082\u3044\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u5b9f\u306f\u30c6\u30b9\u30c8\u3092\u66f8\u304f\u3068\u826f\u3044\u3053\u3068\u304c\u305f\u304f\u3055\u3093\u3042\u308b\u3093\u3067\u3059\u3002 \u30c6<\/p>\n","protected":false},"author":3,"featured_media":2465,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[31,184,52,71,185],"tags":[],"_links":{"self":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/2469"}],"collection":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/comments?post=2469"}],"version-history":[{"count":9,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/2469\/revisions"}],"predecessor-version":[{"id":4024,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/2469\/revisions\/4024"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/media\/2465"}],"wp:attachment":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/media?parent=2469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/categories?post=2469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/tags?post=2469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}