Testim Documentation

Welcome to the Testim documentation. You'll find comprehensive guides to help you get started quickly, example code snippets, and tips for being more productive. You will also find details about advanced features that allow you to customize Testim to fit your environment or special testing conditions. Don't worry, we'll support you if you get stuck. Let's jump right in!

Guides    Changelog

MongoDB validation

Learn how to validate MongoDB using the CLI action step.

You can use the CLI action step to perform queries and run validations on MongoDB database.

Example code:

const MongoClient = mongodb.MongoClient;
// Connection URL
const url = "XXXXXX";
query = JSON.parse(query);

const client = new MongoClient(url, { useNewUrlParser: true });

const connect = () => {
  return new Promise((resolve, reject) => {
    client.connect(function(err) {
      if (err) {
        return reject(err);
      }
      console.log("Connected successfully to server");
      const db = client.db(dbName);
      resolve(db);
    });
  });
};

const findOne = (db, collName) => {
  return new Promise((resolve, reject) => {
    const coll = db.collection(collName);
    coll.findOne(query, (err, result) => {
      if (err) {
        return reject(err);
      }
      console.log("Got collection result");
      resolve(result);
    });
  });
};

return connect()
  .then(db => findOne(db, collName, query))
  .then(result => {
    if (!result) {
      return Promise.reject(new Error("Failed to find object"));
    }
  })
  .finally(() => client.close());

Parameters - Packages and JavaScript used in this example:

  1. name: mongodb, type: Package, value: [email protected]
  2. name: Promise, type: Package, value: [email protected]
  3. name: query, type: JavaScript, value: '{"name":"test"}'
  4. name: collName, type: JavaScript, value: 'users'
  5. name: dbName, type: JavaScript, value: 'myproject'

see screenshot:

Updated 6 months ago

MongoDB validation


Learn how to validate MongoDB using the CLI action step.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.