Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Examples


In this tutorial we are going to look at live codes where mocha has can be used:

Mocha examples

//when testing with babel
import { equal } from "assert";
import { add } from "../src";

describe('Babel usage suite', () => {
  it('it should add numbers correctly', () => {
    equal(add(2, 3), 5);
  });
});
//you can use mocha with karma
const expect = chai.expect;

describe('Array', function () {
  describe('.push()', function () {
    it('it should append a value', function () {
      var arr = [];
      arr.push('foo');
      arr.push('bar');
      expect(arr[0]).to.equal('foo');
      expect(arr[1]).to.equal('bar');
    })

    it('it should return the length', function () {
      var arr = [];
      var n = arr.push('foo');
      expect(n).to.equal(1);
      n = arr.push('bar');
      expect(n).to.equal(2);
    })

    describe('with many arguments', function () {
      it('it should add the values', function () {
        var arr = [];
        arr.push('foo', 'bar');
        expect(arr[0]).to.equal('foo');
        expect(arr[1]).to.equal('bar');
      })
    })
  })

  describe('.unshift()', function () {
    it('it should prepend a value', function () {
      var arr = [1, 2, 3];
      arr.unshift('foo');
      expect(arr[0]).to.equal('foo');
      expect(arr[1]).to.equal(1);
    })

    it('it should return the length', function () {
      var arr = [];
      var n = arr.unshift('foo');
      expect(n).to.equal(1);
      n = arr.unshift('bar');
      expect(n).to.equal(2);
    })

    describe('with many arguments', function () {
      it('it should add the values', function () {
        var arr = [];
        arr.unshift('foo', 'bar');
        expect(arr[0]).to.equal('foo');
        expect(arr[1]).to.equal('bar');
      })
    })
  })

  describe('.pop()', function () {
    it('it should remove and return the last value', function () {
      var arr = [1, 2, 3];
      expect(arr.pop()).to.equal(3);
      expect(arr.pop()).to.equal(2);
      expect(arr).to.have.length(1);
    })
  })

  describe('.shift()', function () {
    it('it should remove and return the first value', function () {
      var arr = [1, 2, 3];
      expect(arr.shift()).to.equal(1);
      expect(arr.shift()).to.equal(2);
      expect(arr).to.have.length(1);
    })
  })
})

Express examples

//testing a res.status 
var express = require('../')
  , request = require('supertest');

describe('res', function(){
  describe('.status(code)', function(){
    it('it should set the response .statusCode', function(done){
      var app = express();

      app.use(function(req, res){
        res.status(201).end('Created');
      });

      request(app)
      .get('/')
      .expect('Created')
      .expect(201, done);
    })
  })
})

Connect examples

//testing an app.listen
var assert = require('assert')
var connect = require('..');
var request = require('supertest');

describe('app.listen()', function(){
  it('it should wrap in an http.Server', function(done){
    var app = connect();

    app.use(function(req, res){
      res.end();
    });

    var server = app.listen(0, function () {
      assert.ok(server)
      request(server)
        .get('/')
        .expect(200, function (err) {
          server.close(function () {
            done(err)
          })
        })
    });
  });
});

SuperAgent examples

//form testing
'use strict';

const request = require('../support/client');
const setup = require('../support/setup');

const base = setup.uri;
const assert = require('assert');

describe('Merging objects', () => {
  it("Do not mix JSON and Buffer", () => {
    assert.throws(() => {
      request
        .post('/echo')
        .send(Buffer.from('some buffer'))
        .send({ allowed: false });
    });
  });
});

describe('req.send(String)', () => {
  it('it should default to "form"', done => {
    request
      .post(`${base}/echo`)
      .send('user[name]=tj')
      .send('user[email][email protected]')
      .end((err, res) => {
        res.header['content-type'].should.equal(
          'application/x-www-form-urlencoded'
        );
        res.body.should.eql({
          user: { name: 'tj', email: '[email protected]' }
        });
        done();
      });
  });
});

describe('res.body', () => {
  describe('application/x-www-form-urlencoded', () => {
    it('it should parse the body', done => {
      request.get(`${base}/form-data`).end((err, res) => {
        res.text.should.equal('pet[name]=manny');
        res.body.should.eql({ pet: { name: 'manny' } });
        done();
      });
    });
  });
});

WebSocket.io

//test for a websocket

/**
 * Test dependencies.
 */

var ws = require('../lib/websocket.io')
  , http = require('http')

/**
 * Tests.
 */

describe('websocket.io', function () {

  it('must expose version number', function () {
    ws.version.should.match(/[0-9]+\.[0-9]+\.[0-9]+/);
  });

  it('has to expose public constructors', function () {
    ws.Socket.should.be.a('function');
    ws.Server.should.be.a('function');
    ws.protocols.drafts.should.be.a('function');
    ws.protocols['7'].should.be.a('function');
    ws.protocols['8'].should.be.a('function');
    ws.protocols['13'].should.be.a('function');
  });

  it('must connect', function (done) {
    listen(function (addr, server) {
      var cl = client(addr);
      cl.on('open', function () {
        cl.close();
        server.close();
        done();
      });
    });
  });

  it('has to handle upgrade requests of a normal http server', function (done) {
    var httpServer = http.createServer(function (req, res) {
      res.writeHead(200);
      res.end('Hello World');
    })

    var server = ws.attach(httpServer);

    httpServer.listen(function (err) {
      if (err) throw err;

      var addr = httpServer.address();

      http.get({ host: addr.address, port: addr.port }, function (res) {
        res.on('data', function (data) {
          data.toString().should.equal('Hello World');

          var cl = client(addr);
          cl.on('open', function () {
            cl.close();
            httpServer.close();
            done();
          });
        });
      });
    });
  });

  it('has to listen on a port', function (done) {
    var server = ws.listen(null, function () {
      var addr = server.httpServer.address();
	
      http.get({ host: addr.address, port: addr.port }, function (res) {
        res.statusCode.should.equal(501);
        res.on('data', function (data) {
          data.toString().should.equal('Not Implemented');

          var cl = client(addr);
          cl.on('open', function () {
            cl.close();
            server.httpServer.close();
            done();
          });
        });
      });
    });
  });

});