{"id":211,"date":"2023-02-23T06:19:38","date_gmt":"2023-02-22T22:19:38","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=211"},"modified":"2023-04-30T15:28:09","modified_gmt":"2023-04-30T07:28:09","slug":"nodejs-testing","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/23\/nodejs-testing\/","title":{"rendered":"Node.js \u6d4b\u8bd5"},"content":{"rendered":"<h2>\u5b89\u88c5\u4e0e\u51c6\u5907<\/h2>\n<pre><code>$ npm init -y\n$ npm i mocha -g\n$ mkdir test\n$ mocha<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u5b89\u88c5\uff1a<\/p>\n<pre><code>$ npm install mocha --save-dev<\/code><\/pre>\n<pre><code>&quot;scripts&quot;: {\n  &quot;test&quot;: &quot;.\/node_modules\/mocha\/bin\/mocha&quot;\n}<\/code><\/pre>\n<h2>mocha\uff1a\u6d4b\u8bd5\u6846\u67b6<\/h2>\n<p>\u521b\u5efa\uff1atest.js<\/p>\n<pre><code class=\"language-javascript\">describe(&#039;TestDemo&#039;, function() {\n  describe(&#039;\u65b9\u6cd5 1&#039;, function() {\n    context(&#039;\u60c5\u5883 1&#039;, function() {\n      it(&#039;\u6d4b\u8bd5 1&#039;, function() {\n\n      })\n      it(&#039;\u6d4b\u8bd5 2&#039;, function() {\n\n      })\n    })\n  })\n})<\/code><\/pre>\n<pre><code>$ mocha\n$ mocha run test<\/code><\/pre>\n<h2>mocha\uff1a\u5b89\u6392\u6d4b\u8bd5\u4e4b\u524d\u4e0e\u4e4b\u540e\u8981\u505a\u7684\u4e8b<\/h2>\n<pre><code class=\"language-javascript\">describe(&#039;TestDemo&#039;, function() {\n  describe(&#039;\u65b9\u6cd5 1&#039;, function() {\n\n    before(function() {\n      console.log(&#039;--- \u6d4b\u8bd5\u4e4b\u524d ---&#039;)\n    })\n    before(function() {\n      console.log(&#039;--- \u6d4b\u8bd5\u4e4b\u540e ---&#039;)\n    })\n\n    beforeEach(function() {\n      console.log(&#039;--- \u6bcf\u6761\u6d4b\u8bd5\u4e4b\u524d ---&#039;)\n    })\n    beforeEach(function() {\n      console.log(&#039;--- \u6bcf\u6761\u6d4b\u8bd5\u4e4b\u540e ---&#039;)\n    })\n\n    context(&#039;\u60c5\u5883 1&#039;, function() {\n      it(&#039;\u6d4b\u8bd5 1&#039;, function() {\n\n      })\n      it(&#039;\u6d4b\u8bd5 2&#039;, function() {\n\n      })\n    })\n  })\n})<\/code><\/pre>\n<h2>chai\uff1a\u65ad\u8a00\u5e93<\/h2>\n<p>\u5b89\u88c5<\/p>\n<pre><code>npm i chai --save-dev<\/code><\/pre>\n<p>chai\uff1aassert\u98ce\u683c\u7684\u65ad\u8a00<\/p>\n<pre><code class=\"language-javascript\">const chai = require(&#039;chai&#039;)\nconst assert = chai.assert\n\ndescribe(&#039;TestDemo&#039;, function() {\n  it(&#039;\u4f7f\u7528 assert \u98ce\u683c\u7684\u65ad\u8a00\u6d4b\u8bd5&#039;, function() {\n    \/\/var value = &#039;hello&#039;\n    var value = 123\n    assert.typeOf(value, &#039;string&#039;)\n    assert.equal(value, &#039;hello&#039;)\n    assert.lengthOf(value, 5)\n  })\n})<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b89\u88c5\u4e0e\u51c6\u5907 $ npm init -y $ npm i mocha -g $ mkdir test $ moc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-211","post","type-post","status-publish","format-standard","hentry","category-node"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=211"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}