1"use strict";(self.webpackChunkwiki=self.webpackChunkwiki||[]).push([[8563],{3905:function(e,t,n){n.d(t,{Zo:function(){return l},kt:function(){return m}});var r=n(7294);function o(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function s(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?s(Object(n),!0).forEach((function(t){o(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):s(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function i(e,t){if(null==e)return{};var n,r,o=function(e,t){if(null==e)return{};var n,r,o={},s=Object.keys(e);for(r=0;r<s.length;r++)n=s[r],t.indexOf(n)>=0||(o[n]=e[n]);return o}(e,t);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(e);for(r=0;r<s.length;r++)n=s[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(o[n]=e[n])}return o}var p=r.createContext({}),c=function(e){var t=r.useContext(p),n=t;return e&&(n="function"==typeof e?e(t):a(a({},t),e)),n},l=function(e){var t=c(e.components);return r.createElement(p.Provider,{value:t},e.children)},u={inlineCode:"code",wrapper:function(e){var t=e.children;return r.createElement(r.Fragment,{},t)}},d=r.forwardRef((function(e,t){var n=e.components,o=e.mdxType,s=e.originalType,p=e.parentName,l=i(e,["components","mdxType","originalType","parentName"]),d=c(n),m=o,f=d["".concat(p,".").concat(m)]||d[m]||u[m]||s;return n?r.createElement(f,a(a({ref:t},l),{},{components:n})):r.createElement(f,a({ref:t},l))}));function m(e,t){var n=arguments,o=t&&t.mdxType;if("string"==typeof e||o){var s=n.length,a=new Array(s);a[0]=d;var i={};for(var p in t)hasOwnProperty.call(t,p)&&(i[p]=t[p]);i.originalType=e,i.mdxType="string"==typeof e?e:o,a[1]=i;for(var c=2;c<s;c++)a[c]=n[c];return r.createElement.apply(null,a)}return r.createElement.apply(null,n)}d.displayName="MDXCreateElement"},4738:function(e,t,n){n.r(t),n.d(t,{frontMatter:function(){return i},contentTitle:function(){return p},metadata:function(){return c},toc:function(){return l},default:function(){return d}});var r=n(7462),o=n(3366),s=(n(7294),n(3905)),a=["components"],i={sidebar_position:2},p="\u5355\u5143\u6d4b\u8bd5",c={unversionedId:"6NodeJS/\u5355\u5143\u6d4b\u8bd5",id:"6NodeJS/\u5355\u5143\u6d4b\u8bd5",isDocsHomePage:!1,title:"\u5355\u5143\u6d4b\u8bd5",description:"`",source:"@site/docs/6NodeJS/\u5355\u5143\u6d4b\u8bd5.md",sourceDirName:"6NodeJS",slug:"/6NodeJS/\u5355\u5143\u6d4b\u8bd5",permalink:"/docs/6NodeJS/\u5355\u5143\u6d4b\u8bd5",editUrl:"https://github.com/facebook/docusaurus/edit/main/website/docs/6NodeJS/\u5355\u5143\u6d4b\u8bd5.md",tags:[],version:"current",sidebarPosition:2,frontMatter:{sidebar_position:2},sidebar:"tutorialSidebar",previous:{title:"dotenv \u73af\u5883\u53d8\u91cf\u7ba1\u7406",permalink:"/docs/6NodeJS/dotenv\u73af\u5883\u53d8\u91cf\u7ba1\u7406"},next:{title:"\u5f39\u6846",permalink:"/docs/7\u524d\u7aef\u5e38\u89c1\u529f\u80fd/dialog"}},l=[{value:"\u6dfb\u52a0\u6d4b\u8bd5\u9879\uff1a",id:"\u6dfb\u52a0\u6d4b\u8bd5\u9879",children:[]},{value:"\u8dd1\u6d4b\u8bd5",id:"\u8dd1\u6d4b\u8bd5",children:[]}],u={toc:l};function d(e){var t=e.components,n=(0,o.Z)(e,a);return(0,s.kt)("wrapper",(0,r.Z)({},u,n,{components:t,mdxType:"MDXLayout"}),(0,s.kt)("h1",{id:"\u5355\u5143\u6d4b\u8bd5"},"\u5355\u5143\u6d4b\u8bd5"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre"}
1,"yarn add -D jest supertest ts-jest @types/jest @types/supertest\n")),(0,s.kt)("p",null,"\u8bbe\u7f6e jest \u914d\u7f6e: ",(0,s.kt)("inlineCode",{parentName:"p"},"jest.config.js")),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre"},"/* eslint-disable no-undef */\nmodule.exports = {\n moduleFileExtensions: ['js', 'jsx', 'json', 'ts', 'tsx'],\n verbose: true,\n collectCoverage: false,\n roots: ['<rootDir>/__tests__'],\n collectCoverageFrom: ['**/*.{ts,js}', '!**/node_modules/**', '!**/build/**', '!**/coverage/**'],\n transform: {\n '\\\\.ts$': '<rootDir>/node_modules/ts-jest/preprocessor.js',\n },\n coverageThreshold: {\n global: {\n branches: 100,\n functions: 100,\n lines: 100,\n statements: 100,\n },\n },\n coverageReporters: ['text', 'text-summary'],\n // testRegex: '(/__tests__/.*|(\\\\.|/)(test|spec))\\\\.(js|ts)x?$', // __tests__ \u4e0b\u7684\u6240\u6709\u6587\u4ef6\n testRegex: '(\\\\.|/)(test|spec)\\\\.(js|ts)x?$',\n testPathIgnorePatterns: ['/node_modules/', '/build/', '/coverage/'],\n testTimeout: 5000,\n}\n\n")),(0,s.kt)("p",null,(0,s.kt)("inlineCode",{parentName:"p"},"package.json")," \u6dfb\u52a0\u547d\u4ee4\uff1a"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre"},'"test": "export NODE_ENV=uniTest && jest --runInBand --forceExit --colors --detectOpenHandles --silent"\n')),(0,s.kt)("p",null,"unitTest \u73af\u5883\u4e0b\u4e0d\u76f4\u63a5\u8fd0\u884c app ",(0,s.kt)("inlineCode",{parentName:"p"},"server.ts"),":"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre"},"if (process.env.NODE_ENV !== 'uniTest') {\n app.listen(port, () => {\n console.log(`Example app listening at http://localhost:${port}`)\n })\n}\n")),(0,s.kt)("h2",{id:"\u6dfb\u52a0\u6d4b\u8bd5\u9879"},"\u6dfb\u52a0\u6d4b\u8bd5\u9879\uff1a"),(0,s.kt)("p",null,(0,s.kt)("inlineCode",{parentName:"p"},"__tests__/_server.ts"),":"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-ts"},'import supertest from "supertest"\nimport app from "../src/server"\n\nexport default supertest(app)\n')),(0,s.kt)("p",null,(0,s.kt)("inlineCode",{parentName:"p"},"__tests__/routers/test.test.ts")),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre"},"import server from '../_server'\n\ndescribe('routers/test', () => {\n it('should success', async () => {\n const response = await server.get('/test')\n expect(response.status).toEqual(200)\n expect(JSON.parse(response.text).code).toEqual(200)\n })\n})\n")),(0,s.kt)("h2",{id:"\u8dd1\u6d4b\u8bd5"},"\u8dd1\u6d4b\u8bd5"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre"},"yarn run test\n")))}d.isMDXComponent=!0}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.