0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-21 12:39:08 +01:00
mongodb/jstests
2024-11-21 01:28:20 +00:00
..
aggregation SERVER-93694 Address SBE incompatibility for distinct-like queries in distinct multiplanning (#28507) 2024-11-20 15:19:16 +00:00
auth SERVER-88158 Remove support for 'alwaysUpsert' in applyOps (#28762) 2024-11-08 21:02:53 +00:00
change_streams SERVER-83104 Remove the multi-version compatibility patch for capped and numInitialChunks in change stream shard events tests (#29397) 2024-11-20 20:52:58 +00:00
client_encrypt SERVER-94873 Change QI to have ownership teams for components (#27219) 2024-09-24 18:23:11 +00:00
concurrency SERVER-93648 Add component teams as owners for QE owned files (#26984) 2024-11-19 22:46:58 +00:00
core SERVER-93694 Address SBE incompatibility for distinct-like queries in distinct multiplanning (#28507) 2024-11-20 15:19:16 +00:00
core_sharding SERVER-94916 Develop core_sharding smoke tests for analyzeShardKey (#29220) 2024-11-19 03:23:51 +00:00
core_standalone
decimal
disk SERVER-95388: Allow validate to choose the configuration string used by WT verify() (#29133) 2024-11-21 01:28:20 +00:00
fail_point SERVER-90983 Separate service arch owned files to new subteams (#26262) 2024-08-28 20:50:51 +00:00
fle2 SERVER-97041 Remove ECC Tokens (#29127) 2024-11-19 19:15:22 +00:00
hooks SERVER-93221 Drop config.placementHistory metadata in magic restore (#27202) 2024-11-20 23:30:34 +00:00
httpinterface SERVER-90983 Separate service arch owned files to new subteams (#26262) 2024-08-28 20:50:51 +00:00
libs SERVER-87429 Create targeted jstest for magic restore and resharding (#28684) 2024-11-20 23:31:00 +00:00
libunwind SERVER-95628 Make rawMongoProgramOutput more robust (#28204) 2024-10-22 22:40:58 +00:00
multiVersion SERVER-95701 Write authorization schema document on FCV downgrade when necessary (#29054) 2024-11-18 21:48:45 +00:00
noPassthrough SERVER-93694 Address SBE incompatibility for distinct-like queries in distinct multiplanning (#28507) 2024-11-20 15:19:16 +00:00
noPassthroughWithMongod SERVER-96583 Give CBR access to histograms generated via analyze() (#28767) 2024-11-12 22:23:44 +00:00
ocsp SERVER-96678 Mark some tests as owned by the Security team (#28837) 2024-11-06 18:55:27 +00:00
parallel SERVER-96798 Mark some tests as owned by the Storage Execution team (#28945) 2024-11-07 19:27:11 +00:00
product_limits SERVER-94511 Test the ability of the product to handle degenerate queries (#28658) 2024-11-07 06:21:06 +00:00
query_golden SERVER-93694 Address SBE incompatibility for distinct-like queries in distinct multiplanning (#28507) 2024-11-20 15:19:16 +00:00
query_golden_sharding SERVER-96234 Implement missing save/restore calls in DistinctScan (#28986) 2024-11-12 22:08:46 +00:00
readonly SERVER-96801 Mark read-only mode tests as owned by RSS Sydney (#28955) 2024-11-07 17:17:37 +00:00
replsets SERVER-94779 Modify ignore_dbcheck_in_magic_restore.js to use refactored magic restore test utilities (#27057) 2024-11-20 23:28:57 +00:00
resmoke_selftest SERVER-96680 Mark some tests as owned by the DevProd Correctness team (#28838) 2024-11-05 23:45:33 +00:00
selinux
serial_run SERVER-92024: Only provide JS_GC_ZEAL setting for mongo server processes. (#24954) 2024-09-06 19:58:23 +00:00
serverless SERVER-82330 Add ability to decode resume token from bson in js tests (#28345) 2024-11-05 16:36:15 +00:00
sharding SERVER-83104 Remove the multi-version compatibility patch for capped and numInitialChunks in change stream shard events tests (#29397) 2024-11-20 20:52:58 +00:00
slow1 SERVER-95628 Make rawMongoProgramOutput more robust (#28204) 2024-10-22 22:40:58 +00:00
ssl SERVER-97089 Remove irrelevant comments related to tenant migration (#29201) 2024-11-18 17:16:21 +00:00
ssl_linear SERVER-96678 Mark some tests as owned by the Security team (#28837) 2024-11-06 18:55:27 +00:00
ssl_x509 SERVER-96678 Mark some tests as owned by the Security team (#28837) 2024-11-06 18:55:27 +00:00
sslSpecial SERVER-96678 Mark some tests as owned by the Security team (#28837) 2024-11-06 18:55:27 +00:00
third_party/fast_check
watchdog SERVER-90983 Separate service arch owned files to new subteams (#26262) 2024-08-28 20:50:51 +00:00
with_mongot SERVER-96064: Optimize away metadata $sort directly after $vectorSearch for single node environments (#28699) 2024-11-07 18:57:33 +00:00
OWNERS.yml SERVER-93071 Revisit code ownership for cluster scalability (#27899) 2024-10-22 02:52:59 +00:00
README.md
SConscript SERVER-94076 Remove unused imports via Ruff rule (#27337) 2024-10-04 01:43:13 +00:00

Javascript Test Guide

At MongoDB we write integration tests in JavaScript. These are tests written to exercise some behavior of a running MongoDB server, replica set, or sharded cluster. This guide aims to provide some general guidelines and best practices on how to write good tests.

Principles

Minimize the test case as much as possible while still exercising and testing the desired behavior.

  • For example, if you are testing that document deletion works correctly, it may be entirely sufficient to insert just a single document and then delete that document. Inserting multiple documents would be unnecessary. A guiding principle on this is to ask yourself how easy it would be for a new person coming to this test to quickly understand it. If there are multiple documents being inserted into a collection, in a test that only tests document deletion, a newcomer might ask the question: “is it important that the test uses multiple documents, or incidental?”. It is best if you can remove these kinds of questions from a persons mind, by keeping only the absolute essential parts of a test.
  • We should always strive for unittesting when possible, so if the functionality you want to test can be covered by a unit test, we should write a unit test instead.

Add a block comment at the top of the JavaScript test file giving a clear and concise overview of what a test is trying to verify.

  • For tests that are more complicated, a brief description of the test steps might be useful as well.

Keep debuggability in mind.

  • Assertion error messages should contain all information relevant to debugging the test. This means the servers response from the failed command should almost always be included in the assertion error message. It can also be helpful to include parameters that vary during the test to avoid requiring the investigator to use the logs/backtrace to determine what the test was attempting to do.
  • Think about how easy it would be to debug your test if something failed and a newcomer only had the logs of the test to look at. This can help guide your decision on what log messages to include and to what level of detail. The jsTestLog function is useful for this, as it is good at visually demarcating different phases of a test. As a tip, run your test a few times and just study the log messages, imagining you are an engineer debugging the test with only these logs to look at. Think about how understandable the logs would be to a newcomer. It is easy to add log messages to a test but then forget to see how they would actually appear.
  • Never insert identical documents unless necessary. It is very useful in debugging to be able to figure out where a given piece of data came from.
  • If a test does the same thing multiple times, consider factoring it out into a library. Shorter running tests are easier to debug and code duplication is always bad.

Do not hardcode collection or database names, especially if they are used multiple times throughout a test.

It is best to use variable names that attempt to describe what a value is used for. For example, naming a variable that stores a collection name collectionToDrop is much better than just naming the variable collName.

Make every effort to make your test as deterministic as possible.

  • Non-deterministic tests add noise to our build system and, in general, make it harder for yourself and other engineers to determine if the system really is working correctly or not. Flaky integration tests should be considered bugs, and we should not allow them to be committed to the server codebase. One way to make jstests more deterministic is to use failpoints to force the events happening in expected order. However, if we have to use failpoints to make this test deterministic, we should consider write a unit test instead.
  • Note that our fuzzer and concurrency test suites are often an exception to this rule. In those cases we sometimes give up some level of determinism in order to trigger a wider class of rare edge cases. For targeted JavaScript integration tests, however, highly deterministic tests should be the goal.

Think hard about all the assumptions that the test relies on.

  • For example, if a certain phase of the test ran much slower or much faster, would it cause your test to fail for the wrong reason?
  • If your test includes hard-coded timeouts, make sure they are set appropriately. If a test is waiting for a certain condition to be true, and the test should not proceed until that condition is met, it is often correct to just wait “indefinitely”, instead of adding some arbitrary timeout value, like 30 seconds. In practice this usually means setting some reasonable upper limit, for example, 10 minutes.
  • Also, for replication tests, make sure data exists on the right nodes at the right time. For example, if you a do a write and dont explicitly wait for it to replicate, it might not reach a secondary node before you try to do the next step of the test.
  • Does your test require data to be stored persistently? Remember that we have test variants that run on in-memory/ephemeral storage engines
  • There are timeouts in the test suites and we aim to make all tests in the same suite finish before timeout. That says we should always make the test run quickly to keep the test short in terms of duration.

Make tests fail as early as possible.

  • If something goes wrong early in the test, its much harder to diagnose when that error becomes visible much later.
  • Wrap every command in assert.commandWorked, or assert.commandFailedWithCode. There is also assert.commandFailed that won't check the return error code, but we should always try to use assert.commandFailedWithCode to make sure the test won't pass on an unexpected error.

Be aware of all the configurations and variants that your test might run under.

  • Make sure that your test still works correctly if is run in a different configuration or on a different platform than the one you might have tested on.
  • Varying storage engines and suites can often affect a tests behavior. For example, maybe your test fails unexpectedly if it runs with authentication turned on with an in-memory storage engine. You dont have to run a new test on every possible platform before committing it, but you should be confident that your test doesnt break in an unexpected configuration.

Avoid assertions that verify properties indirectly.

All assertions in a test should attempt to verify the most specific property possible. For example, if you are trying to test that a certain collection exists, it is better to assert that the collections exact name exists in the list of collections, as opposed to verifying that the collection count is equal to 1. The desired collections existence is sufficient for the collection count to be 1, but not necessary (a different collection could exist in its place). Be wary of adding these kind of indirect assertions in a test.

Modules in Practice

We have fully migrated to the modularized JavaScript world so any new test should use modules and adapt the new style.

Only import/export what you need.

It's always important to keep the test context clean so we should only import/export what we need.

  • The unused import is against no-unused-vars rule in ESLint though we haven't enforced it.
  • We don't have a linter to check export since it's hard to tell the necessity, but we should only export the modules that are imported by other tests or will be needed in the future.

Declare variables in proper scope.

In the past, we have seen tests referring some "undeclared" or "redeclared" variables, which are actually introduced through load(). Now with modules, the scope is more clear. We can use global variables properly to setup the test and don't need to worry about polluting other tests.

Name variables properly when exporting.

To avoid naming conflicts, we should not make the name of exported variables too general which could easily conflict with another variable from the test which import your module. For example, in the following case, the module exported a variable named alphabet and it will lead to a re-declaration error.

import {alphabet} from "/matts/module.js";
const alphabet = "xyz"; // ERROR

Prefer let/const over var

let/const should be preferred over var since these can help detect double declaration at the first place. Like, in the naming conflict example, if the second line is using var, it could easily mess up without throwing an error.

Export in ES6 style

Due to legacy, we have a lot of code that is using the old style to do export, like the following.

const MyModule = (function() {
function myFeature() {}
function myOtherFeature() {}

return {myFeature, myOtherFeature};
})();

Instead, we should use the ES6 way to do export, as follows.

export function myFeature() {}
export function myOtherFeature() {}

// When import from test
import * as MyModule from "/path/to/my_module.js";

This can help the language server to discover the methods and provide code navigation for it.