> For the complete documentation index, see [llms.txt](https://easonwang.gitbook.io/class/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://easonwang.gitbook.io/class/test.md).

# 單元測試

有關測試部分

1.新增一個資料夾

2.terminal cd 進入該資料夾 ，執行`npm init`

3.安裝`mocha`與`should`兩個模組

> 注意:mocha要安裝在 global

```
npm install mocha -g
npm install mocha --save-dev
npm install should --save-dev
```

should的API可參考 <http://shouldjs.github.io/#assertion-above>

4.於package.json將script改為如下

```
"test": "./node_modules/.bin/mocha"
```

我們新增一個index.js

```
exports.test1 = (num) => {
  return 10 + num;
}
```

5.之後新增test資料夾

裡面放入test.js

```
var should = require('should');
var testFile = require('../index.js');
var assert = require('assert');

console.log(testFile.test1());

describe('10 + number', function(){
  it('should = 20', function(done){
    var total = testFile.test1(10);
    total.should.equal(20);
    done();
})
  it('should >= 20', function(done){
    var total = testFile.test1(20);
    total.should.be.above(20);
    done();
  })
})
```

然後在專案目錄輸入Mocha即可

他會自動去找專案內的test資料夾內的檔案執行


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://easonwang.gitbook.io/class/test.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
