在没有浏览器的情况下使用ES6 +编写和测试Javascript库

时间:2017-06-12 02:13:06

标签: javascript webpack

我是Webpack和ES6 +的新手。我试图编写一个Javascript库,它具有一些需要浏览器的功能(编写DOM元素等),但大多数都是可以在没有浏览器的情况下使用和测试的数学函数。同样,我希望能够在没有带浏览器的浏览器和UI功能的情况下测试非UI相关功能。

我有以下配置。

的package.json

{
  "name": "<NAME>",
  "version": "1.0.0",
  "description": "<DESC>",
  "main": "index.js",
  "scripts": {
    "test": "mocha",
    "start": "webpack --colors --progress --watch"
  },
  "author": "<ME>",
  "license": "ISC",
  "dependencies": {
    "mathjs": "^3.13.3"
  },
  "devDependencies": {
    "mocha": "^3.4.2"
  }
}

webpack.config.js

module.exports = {
    entry: './index.js',
    output: {
        filename: 'bundle.js'
    }
}

index.js

import math from 'mathjs';

// just a dummy example, uses mathjs.org
function concat(A, B) {
    return math.concat(A, B)
}

export default concat

测试/ test.js

import assert from 'assert';
import concat from '../index';

describe('Concat', function() {
  describe('#concat()', function() {
    it('should return a horizontal concatenation of two matrices', function() {
      const A = [[1, 2], [3, 4]]
      const B = [[5, 6], [7, 8]]

      const AB = concat(A, B);

      assert.deepEqual(AB, [[1, 2, 5, 6], [3, 4, 7, 8]]);
    });
  });
});

截至目前,我还没有办法运行test/test.js ..如何将其添加到我的网络包配置中?还有,&#34;观看&#34;测试?如果我想在浏览器中进行一些测试,我该怎么做呢?

0 个答案:

没有答案
相关问题