本文介绍了如何在浏览器中公开外部库,以便Jest单元测试可以看到它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个在浏览器中使用的数学库,并使用Jest在其上运行单元测试(我意识到它更适合于Node).我已经通过扩展JS Math解决了大多数问题,但是为了进行平均(均值)和标准差,我正在使用 https://mathjs.org 的数学库.所有这些都可以在浏览器中正常运行,但是Jest无法看到mathjs库,而且我不确定如何修复它.

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). 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. 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(CalRunWebMath.js)中失败的特定代码部分:

This is the particular section of code that's failing in 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

这是驱动它的HTML:

This is the HTML that drives it:

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

这是Jest测试文件:

This is the Jest test file:

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);
});

我收到的错误是: ReferenceError:未定义数学 (我从上面的代码段中省略了其他通过的测试):

The error I receive is: ReferenceError: math is not defined (I've omitted the other passing tests from the snippet above):

 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

如何在浏览器中显示数学模块,以便Jest在测试中可以看到它?

How can I expose the math module in the browser so that Jest can see it in the tests?

推荐答案

Node中的全局名称空间对象可以作为global使用.

The global namespace object in Node is available as global.

您可以像这样将math添加到全局名称空间对象:

You can add math to the global namespace object like this:

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!
});

这篇关于如何在浏览器中公开外部库,以便Jest单元测试可以看到它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:56