简体   繁体   English

NodeJS 在测试环境中更改 MongoDB 数据库名称

[英]NodeJS change MongoDB database name on testing environment

I have this piece of code to connect to MongoDB:我有这段代码可以连接到 MongoDB:

MongoClient.connect(
      process.env.MONGO_HOST,
      { useUnifiedTopology: true },
      function (err, client) {
        _db = client.db(process.env.DB_NAME);
        _client = client;
        return callback(err);
      }
    );

I am using mocha and chai to test my application.我正在使用 mocha 和 chai 来测试我的应用程序。 The problem is since the db name comes from process.env.DB_NAME, the same database is modified while running npm test .问题是因为数据库名称来自 process.env.DB_NAME,所以在运行npm test时修改了同一个数据库。 Is there a way to change the db name based on the command given to start the server so that the tests can modify a seperate db of it's own?有没有办法根据启动服务器的命令更改数据库名称,以便测试可以修改它自己的单独数据库?

What I usually do if I want to my test configuration to be checked into repo (because it isn't dependent on how your dev environment is configured), I add a folder and a test file that I know will be loaded first:如果我想将我的测试配置签入回购协议(因为它不依赖于您的开发环境的配置方式),我通常会做什么,我添加一个文件夹和一个我知道将首先加载的测试文件:

tests
 \ 00-init
   \ 00-init.js

Which just contains其中只包含

process.env.MONGO_HOST = 'localhost';
process.env.DB_NAME = 'test';

Of course, this doesn't really work if different devs are using different hosts and database names for testing, then they should supply their own ENV variables when running the tests.当然,如果不同的开发人员使用不同的主机和数据库名称进行测试,这并不能真正起作用,那么他们应该在运行测试时提供自己的 ENV 变量。

If you use a test runner in your IDE, you can normally specify which ENV variables that you want to set, otherwise you can specify them for your terminal, or directly when you are invoking your command line (for unix shell type terminals):如果您在 IDE 中使用测试运行程序,您通常可以指定要设置的 ENV 变量,否则您可以为终端指定它们,或者直接在调用命令行时指定它们(对于 unix shell 类型终端):

MONGO_HOST=localhost DB_NAME=test npm test

In your case, where you are using dotenv, you can still override any ENV variables like this because dotenv won't overwrite an ENV variable that already exists.在您使用 dotenv 的情况下,您仍然可以像这样覆盖任何 ENV 变量,因为 dotenv 不会覆盖已经存在的 ENV 变量。

If I understand you correctly, you want to have two separate environments on your local machine: one for development (with its own database) and one for testing (with its own database).如果我理解正确的话,您希望在您的本地机器上有两个独立的环境:一个用于开发(有自己的数据库)和一个用于测试(有自己的数据库)。

This is how I would do things.这就是我做事的方式。

  • I would keep the environment variables as non-checked in files (since they oftentimes contain private keys and such).我会将环境变量保留为未检查的文件(因为它们通常包含私钥等)。 (So, they are added to.gitignore file, especially if it is in a publicly accessible repo). (因此,它们被添加到 .gitignore 文件中,尤其是当它位于可公开访问的存储库中时)。 This has also the benefit of having fixed.env variables for your machine.这也有为您的机器提供 fixed.env 变量的好处。 (Otherwise, if you check in the files, other developers who have different values, they will have to modify these variables at each pull from master. (否则,如果您签入文件,其他具有不同值的开发人员,他们将不得不在每次从 master 拉取时修改这些变量。

  • You are using the dotenv package as far as I understand.据我了解,您正在使用dotenv package。 You can create.env files to be parsed by this package. For example, .env.dev and.env.test可以创建.env文件供这个package解析。比如.env.dev和.env.test

  • In your package.json, you modify your dev and test scripts to set node to a specific environment.在您的 package.json 中,您修改开发和测试脚本以将节点设置为特定环境。 For example,例如,

    "scripts": { “脚本”:{
    "dev": "NODE_ENV=dev node./server.js", "dev": "NODE_ENV=dev node./server.js",
    "test": "NODE_ENV=test mocha./test/test.js", "test": "NODE_ENV=test mocha./test/test.js",
    }, },

  • As final step, in the relevant part of the application as early as possible (for example, during the server bootup in server.js), you do作为最后一步,在应用程序的相关部分尽早(例如,在 server.js 中的服务器启动期间),你做

    require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }). require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }).

  • Now, process.env is loaded with your environment variables from the correct.env file.现在,process.env 加载了来自 correct.env 文件的环境变量。

Doing something like process.env.MONGO_HOST='foo';做一些像process.env.MONGO_HOST='foo'; inside a module is a code smell—don't do it.模块内部有代码味道——不要这样做。

In the most basic of Node project setups, you can modify your "test" script so that tets/local environment variables are prepended to your test runner & start scripts.在最基本的 Node 项目设置中,您可以修改“测试”脚本,以便将 tets/本地环境变量添加到您的测试运行器和启动脚本中。 Example:例子:

package.json package.json

{
  "name": "@jameswomack/npm-config-example",
  "scripts": {
    "test": "$npm_package_config_dblocal mocha ./src/*.test.js",
    "start-server": "node ./src/index.js",
    "start:local": "$npm_package_config_dblocal npm run start-server",
    "start:prod": "$npm_package_config_dbprod npm run start-server"
  },
  "config": {
    "dbprod": "env MONGO_HOST=https://fu.manchu DB_NAME=1337;",
    "dblocal": "env MONGO_HOST=localhost DB_NAME=local;"
  },
  "devDependencies": {
    "mocha": "8.1.3"
  }
}

See example within example repo here此处查看示例回购中的示例

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

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