development:testing:mocha:start

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
development:testing:mocha:start [2014/09/30 01:25] – [Installation] don.leedevelopment:testing:mocha:start [2019/01/13 12:34] (current) – [Sinon JS] don.lee
Line 5: Line 5:
 ===== Installation ===== ===== Installation =====
  
-  * install the mocha & should plugins via npm+  * install the mocha & eslint-plugin-mocha plugins via npm
 <code> <code>
-$ npm install -mocha +$ npm install --save-dev mocha 
-$ npm install should+$ npm install --save-dev eslint-plugin-mocha
 </code> </code>
   * create test folder and put test case there   * create test folder and put test case there
Line 15: Line 15:
 $ mocha $ mocha
 </code> </code>
 +
 +Edit the .eslintrc.json file to include the eslint-plugin-mocha plugins
 +<code>
 +{
 +  ...
 +  "plugins": [
 +    "mocha"
 +  ],
 +  "rules": {
 +    ...
 +    "mocha/no-exclusive-tests": "error",
 +    "prefer-arrow-callback": 0,
 +    "mocha/prefer-arrow-callback": 2
 +  }
 +}
 +</code>
 +
 +===== Using .only for exclusive test only =====
 +
 +Mocha has a feature that allows you to run tests exclusively by appending .only to a test-suite or a test-case. This feature is really helpful to debug a failing test, so you don’t have to execute all of your tests. After you have fixed your test and before committing the changes you have to remove .only to ensure all tests are executed on your build system.
 +
 +This rule looks for every describe.only, it.only, suite.only, test.only, context.only and specify.onlyoccurrences within the source code.
 +
 +e.g.
 +<code>
 +describe('.saveOne', function () {
 +  it.only('will run the test', async function () {
 +    ...
 +  })
 +  it.only('will not run this test', async function () {
 +    ...
 +  })
 +})
 +</code>
 +
 +===== prefer arrow function callbacks (mocha-aware) =====
 +
 +In ES6, we use arrow function a lot. But in Mocha, both describe and it callback should NOT use arrow function as the this will no longer refer to suite/test.
 +
 +e.g. INCORRECT
 +<code>
 +describe('User', () => {
 +  this.timeout(5000)   // this is not refer to suite
 +  ...
 +})
 +</code>
 +
 +CORRECT
 +<code>
 +describe('User', function () {
 +  this.timeout(5000)   // this is ok
 +  ...
 +})
 +</code>
 +
 +===== Sinon JS =====
 +
 +  * [[development:testing:sinonjs:|Sinon JS]]
  
 ===== Test Case ===== ===== Test Case =====
  
-http://jasmine.github.io/2.0/introduction.html+ 
 +  * Reference 
 +http://www.wekeroad.com/2012/02/25/testing-your-model-with-mocha-mongo-and-nodejs/
  
  
  • development/testing/mocha/start.1412011536.txt.gz
  • Last modified: 2014/09/30 01:25
  • by don.lee