简体   繁体   English

如何在浏览器中公开外部库,以便Jest单元测试可以看到它?

[英]How to expose external library in browser so Jest unit tests can see it?

I'm writing a math library to be used in a browser, and using Jest to run unit tests on it (which I realize is more geared toward Node). 我正在编写一个要在浏览器中使用的数学库,并使用Jest在其上运行单元测试(我意识到它更适合于Node)。 I've solved most of the issues by extending JS Math, but in order to do averaging (mean) and standard deviation, I'm working with https://mathjs.org 's math library. 我已经通过扩展JS Math解决了大多数问题,但是为了进行平均(均值)和标准差,我正在使用https://mathjs.org的数学库。 This all works fine in the browser, but Jest is unable to see the mathjs library and I'm not sure how to fix it. 所有这些在浏览器中都可以正常运行,但是Jest无法看到mathjs库,而且我不确定如何修复它。

This is the particular section of code that's failing in Jest (CalRunWebMath.js): 这是在Jest(CalRunWebMath.js)中失败的特定代码部分:

//Extend Math to calculate coefficient of variation:
Math.cv = function(numericArray){
    var std = math.std(numericArray);
    var mean = math.mean(numericArray);
    //this is how I originally did it:
    //return math.std(numericArray)/math.mean(numericArray);
    return std/mean;
}
try {
    module.exports.cv = exports = Math.cv;
}
catch (e) {}

//and this is a snippet of the internal tests that works just fine in the browser, but not in Jest
var data1 = [10.4,20.3,30.2,40.1];
console.log(Math.cv(data1)); //0.5061720808904743

This is the HTML that drives it: 这是驱动它的HTML:

<script src='js/math.js'></script>
<script src='js/CalRunWebMath.js'></script>

This is the Jest test file: 这是Jest测试文件:

const crwm = require('./CalRunWebMath.js');
const math = require('./math.js');
const cv = crwm.cv;

test('Calculates coefficient of variation', ()=> {
    var data1 = [10.4,20.3,30.2,40.1];
    expect(cv(data1)).toBe(0.5061720808904743);
});

The error I receive is: ReferenceError: math is not defined (I've omitted the other passing tests from the snippet above): 我收到的错误是: ReferenceError:未定义数学 (我从上面的代码段中省略了其他通过的测试):

 FAIL  ./CalRunWebMath.test.js
  √ Calculates slope of two coordinates (6ms)
  × Calculates coefficient of variation (4ms)
  √ Calculates Y-intercept of two coordinates (1ms)
  √ Calculates the mean of an array of decimals (48ms)

  ● Calculates coefficient of variation

    ReferenceError: math is not defined

      43 | Math.cv = function(numericArray){
      44 |      //console.log(math.std);
    > 45 |      var std = math.std(numericArray);
         |                ^
      46 |      var mean = math.mean(numericArray);
      47 |      //return math.std(numericArray)/math.mean(numericArray);
      48 |      return std/mean;

      at math (js/CalRunWebMath.js:45:12)
      at Object.cv (js/CalRunWebMath.test.js:14:9)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 3 passed, 4 total

How can I expose the math module in the browser so that Jest can see it in the tests? 如何在浏览器中显示数学模块,以便Jest在测试中可以看到它?

The global namespace object in Node is available as global . Node的全局名称空间对象可用作global

You can add math to the global namespace object like this: 您可以像这样将math添加到全局名称空间对象:

global.math = require('./math.js');
const { cv } = require('./CalRunWebMath.js');

test('Calculates coefficient of variation', () => {
  var data1 = [10.4, 20.3, 30.2, 40.1];
  expect(cv(data1)).toBe(0.5061720808904743);  // Success!
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM