简体   繁体   English

Mocha.js - 如何保存全局变量?

[英]Mocha.js - How to save a global variable?

I'm working with Mocha.js for testing in a Node.js - Express.js - Firebase我正在使用 Mocha.js 在 Node.js - Express.js - Firebase 中进行测试

I need a token from Firebase to access the API endpoints, I have a before hook in all my files, but after about 250 tests, probably calling the authentication endpoint multiple times, I'm getting rate limited by firebase.我需要来自 Firebase 的令牌来访问 API 端点,我的所有文件中都有一个 before 挂钩,但经过大约 250 次测试,可能多次调用身份验证端点,我的速率受到 firebase 的限制。

I want to get the token once and use it in all my tests.我想获得一次令牌并在我的所有测试中使用它。

The tests are spread in different files, I have an index.js that requires them all.测试分布在不同的文件中,我有一个需要所有测试的 index.js。 I'm aware of Root Level Hooks, but how can I save the token and use it in all my separate files?我知道 Root Level Hooks,但如何保存令牌并在我的所有单独文件中使用它?

Thanks!谢谢!

you can create a function that gets the token.您可以创建一个获取令牌的 function。 then call it.然后调用它。 then create your test suite only after that然后仅在此之后创建您的测试套件

function getToken(callback) {
  //
}

// define tests
function allTests(token) {
    describe(xxxxxx, function () {
        it(xxxxxxxxx, function() {
            //
        })
    });
}

// start all
getToken(function(token) {
    allTests(token);
});

I managed to solve it myself, if anyone needs an answer on how to approach it, take a look at this.我设法自己解决了它,如果有人需要有关如何解决它的答案,请看一下。

I have multiple files where we write our unit testing, we unite them in an index.spec.js that we execute for testing ($ mocha index.spec.js)我有多个文件,我们在其中编写单元测试,我们将它们合并到我们为测试执行的 index.spec.js 中($ mocha index.spec.js)

I created a utility file that looks like this:我创建了一个如下所示的实用程序文件:

let token;
(() => { token = getYourToken() })()
module.exports = {
getToken: () => {
    return new Promise((resolve) => {
        const interval = setInterval(() => {
            if (token) {
                clearInterval(interval);
                resolve(token);
            }
        }, 100);
    });
}
};

Basically, it's a singleton, in the index.spec.js I require this file, executing the 'getYourToken()' once (add your logic to get token here).基本上,它是 singleton,在 index.spec.js 中我需要这个文件,执行一次“getYourToken()”(在此处添加您的逻辑以获取令牌)。 Then I store it in a variable that then I export.然后我将它存储在一个变量中,然后我导出。 In the export, I use an interval because my current code is not using promises, use your best practice method, interval + Promise worked for me.在导出中,我使用了一个区间,因为我当前的代码没有使用承诺,使用你的最佳实践方法,区间 + Promise 为我工作。

This way I require this file in my tests and get the token I got at the beginning once, avoiding rate-limiting and any issue with firebase.这样我在我的测试中需要这个文件并获得我在开始时获得的令牌一次,避免了速率限制和 firebase 的任何问题。

  • Create a JSON file in your test root directory.在测试根目录中创建一个 JSON 文件。
  • Import the file.导入文件。
  • Append a token property with the token value. Append 具有令牌值的令牌属性。
  • Then import it anywhere to access the token property.然后将其导入任何地方以访问令牌属性。

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

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