{"id":191000,"date":"2023-06-14T05:16:37","date_gmt":"2023-06-13T21:16:37","guid":{"rendered":"https:\/\/www.idc.net\/help\/191000\/"},"modified":"2023-06-14T05:16:37","modified_gmt":"2023-06-13T21:16:37","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91react%e6%95%99%e7%a8%8b%ef%bc%9a%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/191000\/","title":{"rendered":"\u540e\u6d6a\u4e91React\u6559\u7a0b\uff1a\u6d4b\u8bd5"},"content":{"rendered":"<h2>\u8fd0\u884c\u6d4b\u8bd5\u548c\u8d21\u732e<\/h2>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">React Native \u56de\u8d2d\u6709\u51e0\u4e2a\u4f60\u53ef\u4ee5\u8fd0\u884c\u7684\u6d4b\u8bd5\uff0c\u6765\u9a8c\u8bc1\u4f60\u6ca1\u6709\u7528PR\u5f15\u8d77\u62df\u5408\u3002\u8fd9\u4e9b\u6d4b\u8bd5\u662f\u7528<span>&nbsp;<\/span>Travis<span>&nbsp;<\/span>\u6301\u7eed\u96c6\u6210\u7cfb\u7edf\u8fd0\u884c\u7684\uff0c\u5e76\u81ea\u52a8\u7684\u5411\u4f60\u7684 PR \u53d1\u5e03\u7ed3\u679c\u3002\u4f60\u4e5f\u53ef\u4ee5\u5728 IntegrationTest \u548c\u5728 Xcode \u4e2d\u7684 UIExplorer \u5e94\u7528\u4e2d\uff0c\u4f7f\u7528 cmd+U \u672c\u5730\u8fd0\u884c\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u7684<span>&nbsp;<\/span><code>npm test<\/code><span>&nbsp;<\/span>\u8fd0\u884c jest \u6d4b\u8bd5\u3002\u4f46\u662f\u6211\u4eec\u76ee\u524d\u8fd8\u6ca1\u6709\u5f88\u5927\u7684\u6d4b\u8bd5\u8986\u76d6\u7387\uff0c\u6240\u4ee5\u5927\u591a\u6570\u7684\u53d8\u5316\u4ecd\u5c06\u9700\u8981\u5927\u91cf\u624b\u5de5\u9a8c\u8bc1\uff0c\u4f46\u5982\u679c\u4f60\u60f3\u5e2e\u52a9\u6211\u4eec\u63d0\u9ad8\u6211\u4eec\u7684\u6d4b\u8bd5\u8986\u76d6\u7387\uff0c\u6211\u4eec\u662f\u975e\u5e38\u6b22\u8fce\u7684!<\/p>\n<h2>Jest \u6d4b\u8bd5<\/h2>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">Jest<span>&nbsp;<\/span>\u6d4b\u8bd5\u662f JS-only \u6d4b\u8bd5\uff0c\u8fd0\u884c\u5728\u8282\u70b9\u547d\u4ee4\u884c\u4e0a\u3002\u6d4b\u8bd5\u4f4d\u4e8e\u5b83\u4eec\u6d4b\u8bd5\u7684\u6587\u4ef6<span>&nbsp;<\/span><code>__tests__<\/code><span>&nbsp;<\/span>\u76ee\u5f55\u4e2d\uff0c\u8fd8\u6709\u4e00\u4e2a\u5bf9\u4e0d\u662f\u4f4d\u4e8e\u6545\u969c\u9694\u79bb\u548c\u6700\u5927\u901f\u5ea6\u6d4b\u8bd5\u4e0b\u7684\u79ef\u6781\u6a21\u62df\u529f\u80fd\u7684\u5f3a\u8c03\u3002\u4f60\u53ef\u4ee5\u7528\u6765\u81ea react-native \u6839\u7684<span>&nbsp;<\/span><code>npm test<\/code><span>&nbsp;<\/span>\u8fd0\u884c\u73b0\u6709\u7684 React Native jest \u6d4b\u8bd5\uff0c\u5e76\u4e14\u6211\u4eec\u9f13\u52b1\u4f60\u4e3a\u4f60\u60f3\u505a\u51fa\u8d21\u732e\u7684\u4efb\u4f55\u7ec4\u4ef6\u6dfb\u52a0\u4f60\u81ea\u5df1\u7684\u6d4b\u8bd5\u3002\u57fa\u672c\u793a\u4f8b\u8bf7\u770b<span>&nbsp;<\/span><code>getImageSource-test.js<\/code>\u3002<\/p>\n<h2>\u96c6\u6210\u6d4b\u8bd5<\/h2>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">React Native \u63d0\u4f9b\u8bbe\u65bd\uff0c\u4f7f\u6d4b\u8bd5\u9700\u8981 native \u548c JS \u7ec4\u4ef6\u8fdb\u884c\u8de8\u6865\u4ea4\u4e92\u7684\u96c6\u6210\u7ec4\u4ef6\u66f4\u5bb9\u6613\u3002\u4e24\u4e2a\u4e3b\u8981\u7ec4\u4ef6\u662f<code>RCTTestRunner<\/code><span>&nbsp;<\/span>\u548c<span>&nbsp;<\/span><code>RCTTestModule<\/code>\u3002<code>RCTTestRunner<\/code><span>&nbsp;<\/span>\u8bbe\u7f6e\u4e86 React Native \u73af\u5883\u5e76\u63d0\u4f9b\u8bbe\u5907\u8fd0\u884c\u6d4b\u8bd5\uff0c\u6b63\u5982\u5728 Xcode \u4e2d\u7684<span>&nbsp;<\/span><code>XCTestCase<\/code>(<code>runTest:module<\/code><span>&nbsp;<\/span>\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5)\u3002<code>RCTTestModule<\/code><span>&nbsp;<\/span>\u548c<span>&nbsp;<\/span><code>TestModule<\/code><span>&nbsp;<\/span>\u4e00\u6837\uff0c\u901a\u8fc7<span>&nbsp;<\/span><code>NativeModules<\/code><span>&nbsp;<\/span>\u88ab\u5bfc\u51fa\u5230 JS \u4e2d\u3002\u6d4b\u8bd5\u5199\u5728 JS \u4e2d\uff0c\u5f53\u5b83\u4eec\u5b8c\u6210\u65f6\uff0c\u5fc5\u987b\u8c03\u7528<span>&nbsp;<\/span><code>TestModule.markTestCompleted()<\/code>\uff0c\u5426\u5219\u6d4b\u8bd5\u5c06\u8d85\u65f6\u5931\u8d25\u3002\u6d4b\u8bd5\u5931\u8d25\u4e3b\u8981\u662f\u901a\u8fc7\u629b\u51fa\u5f02\u5e38\u8868\u793a\u3002\u5b83\u8fd8\u53ef\u4ee5\u7528<span>&nbsp;<\/span><code>runTest:module:initialProps:expectErrorRegex:<\/code><span>&nbsp;<\/span>\u6216<code>runTest:module:initialProps:expectErrorBlock:<\/code><span>&nbsp;<\/span>\u6d4b\u8bd5\u9519\u8bef\u6761\u4ef6\uff0c\u5b83\u9884\u8ba1\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u5e76\u9a8c\u8bc1\u9519\u8bef\u4e0e\u63d0\u4f9b\u7684\u6807\u51c6\u76f8\u5339\u914d\u3002\u5bf9\u4e8e\u4f8b\u5b50\u7684\u4f7f\u7528\uff0c\u8bf7\u770b<span>&nbsp;<\/span><code>IntegrationTestHarnessTest.js<\/code><span>&nbsp;<\/span>\u548c<span>&nbsp;<\/span><code>IntegrationTestsTests.m<\/code>\u3002<\/p>\n<h2>\u5feb\u7167\u6d4b\u8bd5<\/h2>\n<p style=\"margin: 0px 0px 10px;line-height: 26px;font-size: 14px;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;text-align: start;text-indent: 0px;text-transform: none\">\u5e38\u89c1\u7684\u4e00\u79cd\u96c6\u6210\u6d4b\u8bd5\u662f\u5feb\u7167\u6d4b\u8bd5\u3002\u8fd9\u4e9b\u6d4b\u8bd5\u6e32\u67d3\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u5e76\u4f7f\u7528<span>&nbsp;<\/span><code>TestModule.verifySnapshot()<\/code><span>&nbsp;<\/span>\u9a8c\u8bc1\u53c2\u8003\u56fe\u50cf\u7684\u5c4f\u5e55\u5feb\u7167\uff0c\u5728\u5e55\u540e\u4f7f\u7528<span>&nbsp;<\/span><code>FBSnapshotTestCase<\/code><span>&nbsp;<\/span>\u5e93\u3002\u53c2\u8003\u56fe\u50cf\u901a\u8fc7\u5728<span>&nbsp;<\/span><code>RCTTestRunner<\/code><span>&nbsp;<\/span>\u4e2d\u8bbe\u7f6e<span>&nbsp;<\/span><code>recordMode = YES<\/code><span>&nbsp;<\/span>\u88ab\u8bb0\u5f55\u4e0b\u6765\uff0c\u7136\u540e\u8fd0\u884c\u6d4b\u8bd5\u3002\u5feb\u7167\u5728 32 \u4f4d\u548c 64 \u4f4d\u7cfb\u7edf\u4e2d\u7565\u6709\u4e0d\u540c\uff0c\u4e14\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u7248\u672c\u4e2d\u4e5f\u6709\u6240\u4e0d\u540c\uff0c\u6240\u4ee5\u5efa\u8bae\u4f60\u4f7f\u7528\u6b63\u786e\u7684\u914d\u7f6e\u8fd0\u884c\u6d4b\u8bd5\u3002\u540c\u65f6\u5f3a\u70c8\u5efa\u8bae\u6240\u6709\u7f51\u7edc\u6570\u636e\u88ab\u6a21\u62df\uff0c\u4ee5\u53ca\u5176\u4ed6\u6f5c\u5728\u7684\u9ebb\u70e6\u7684\u4f9d\u8d56\u6027\u3002\u57fa\u672c\u793a\u4f8b\u8bf7\u770b<code>SimpleSnapshotTest<\/code>\u3002<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd0\u884c\u6d4b\u8bd5\u548c\u8d21\u732e React Native \u56de\u8d2d\u6709\u51e0\u4e2a\u4f60\u53ef\u4ee5\u8fd0\u884c\u7684\u6d4b\u8bd5\uff0c\u6765\u9a8c\u8bc1\u4f60\u6ca1\u6709\u7528PR\u5f15\u8d77\u62df\u5408\u3002\u8fd9\u4e9b\u6d4b\u8bd5\u662f\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182398],"tags":[],"class_list":["post-191000","post","type-post","status-publish","format-standard","hentry","category-mini-program"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/191000","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=191000"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/191000\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=191000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=191000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=191000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}