node 클라이언트 JS.js로 테스트
                                            
                                                
                                                
                                                
                                                
                                                
                                                 10728 단어  JavaScript
                    
골대 
클라이언트 측 JavaScript, 특히 DOM과 같은 접촉하지 않는 부분 node.js 테스트가 결승점입니다.스트로크일지 몰라도 과장된 작업은 필요 없기 때문에 가볍게 하면 괜찮을 것 같아요.
전제 조건 
작업 
우선 프로젝트를 하다.$ mkdir hello-node
$ cd hello-node
$ npm init
npm init 이후 대화 형식으로 종목 설정을 묻지만 연타로 끝난다.다음 테스트를 위해 mocha에 설치npm install --save mocha.그래서 패키지.다음은 제이슨.{
  "name": "hello-node",
  "version": "0.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mocha": "^3.2.0"
  }
}
npm test에서 패키지는 모차로test 디렉터리의 테스트를 실행합니다.제이슨을 다시 쓰다.@@ -4,7 +4,7 @@
   "description": "",
   "main": "index.js",
   "scripts": {
-    "test": "echo \"Error: no test specified\" && exit 1"
+    "test": "./node_modules/.bin/mocha test"
   },
   "author": "",
   "license": "ISC",
테스트용 파일입니다.테스트 디렉터리에서 js' 를 만듭니다.var assert = require("assert");
describe("calc", function() {
  it("add", function() { assert.equal(4, 1 + 3);});
  it("sub", function() { assert.equal(3, 4 - 1);});
  it("sub_", function() { assert.equal(2, 4 - 1);});
});
npm test 테스트는 정상적으로 실행되고 다음과 같은 표준에 따라 출력됩니다.> mocha test
  calc
    ✓ add
    ✓ sub
    1) sub_
  2 passing (7ms)
  1 failing
  1) calc sub_:
      AssertionError: 2 == 3
      + expected - actual
      -2
      +3
테스트가 실행될 수 있음을 확인했습니다.여기에 src 디렉터리를 만들고 최종 클라이언트가 사용할 js 파일을 만듭니다. (파일 이름 bird.jsvar Bird = function() {};
Bird.prototype.say = function() {
  return "ka-ka-";
};
try { module.exports = Bird;} catch(e) {};
브라우저에 정의된 편집 module 이 없기 때문에try-catch에서 오류를 침묵시킬 수 있습니다.그럼, 이걸 테스트하는 파일bird-test.js을 만들게요.var assert = require("assert");
var Bird = require("../src/bird");
describe("animal", function() {
  it("say", function() {
    assert.equal("ka-ka-", new Bird().say());
  });
});
npm test 이후 bird-test.js에서 정의한 테스트를 실시하고 다음과 같은 표준 출력을 실시한다.> mocha test
  animal
    ✓ say
  calc
    ✓ add
    ✓ sub
    1) sub_
  3 passing (7ms)
  1 failing
  1) calc sub_:
      AssertionError: 2 == 3
      + expected - actual
마지막hello-node 디렉토리에 다음 HTML 파일을 만들고 브라우저에서 엽니다.<html>
  <head>
  <title>hello</title>
  <script src="./src/bird.js"></script>
  </head>
  <body>
  </body>
</html>
디더링 도구의 콘솔에서 작업하는지 확인합니다(이미지는 Chrome).
 
 
이 때 디렉터리의 구성은 다음과 같다..
├── index.html
├── node_modules
│   └── mocha
├── package.json
├── src
│   └── bird.js
└── test
    ├── bird-test.js
    └── calc-test.js
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(node 클라이언트 JS.js로 테스트), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/nnabeyang/items/3fbd267c6aebdf5510b9
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                 우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
$ mkdir hello-node
$ cd hello-node
$ npm init
{
  "name": "hello-node",
  "version": "0.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mocha": "^3.2.0"
  }
}
@@ -4,7 +4,7 @@
   "description": "",
   "main": "index.js",
   "scripts": {
-    "test": "echo \"Error: no test specified\" && exit 1"
+    "test": "./node_modules/.bin/mocha test"
   },
   "author": "",
   "license": "ISC",
var assert = require("assert");
describe("calc", function() {
  it("add", function() { assert.equal(4, 1 + 3);});
  it("sub", function() { assert.equal(3, 4 - 1);});
  it("sub_", function() { assert.equal(2, 4 - 1);});
});
> mocha test
  calc
    ✓ add
    ✓ sub
    1) sub_
  2 passing (7ms)
  1 failing
  1) calc sub_:
      AssertionError: 2 == 3
      + expected - actual
      -2
      +3
var Bird = function() {};
Bird.prototype.say = function() {
  return "ka-ka-";
};
try { module.exports = Bird;} catch(e) {};
var assert = require("assert");
var Bird = require("../src/bird");
describe("animal", function() {
  it("say", function() {
    assert.equal("ka-ka-", new Bird().say());
  });
});
> mocha test
  animal
    ✓ say
  calc
    ✓ add
    ✓ sub
    1) sub_
  3 passing (7ms)
  1 failing
  1) calc sub_:
      AssertionError: 2 == 3
      + expected - actual
<html>
  <head>
  <title>hello</title>
  <script src="./src/bird.js"></script>
  </head>
  <body>
  </body>
</html>
.
├── index.html
├── node_modules
│   └── mocha
├── package.json
├── src
│   └── bird.js
└── test
    ├── bird-test.js
    └── calc-test.js
Reference
이 문제에 관하여(node 클라이언트 JS.js로 테스트), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/nnabeyang/items/3fbd267c6aebdf5510b9텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)