npm

Need private packages and team management tools?Check out npm Orgs. »

codeceptjs-dbhelper

1.0.0 • Public • Published

codeceptjs-dbhelper

Let your CodeceptJS tests talk to databases

This is a Helper for CodeceptJS that allows you to execute queries or commands to databases using database-js. That is, your tests written for CodeceptJS now will be able to access databases easily. It is especially useful for preparing databases before/after executing test cases.

Install

npm install --save-dev codeceptjs-dbhelper

👉 You will also have to install the drivers of database-js for the desired databases. For instance, whether you want to access a JSON file, just install the JSON driver.

Available drivers

Only install those you need

  • Access databases or SQL Server databases
    npm install --save-dev database-js-adodb
  • CSV files
    npm install --save-dev database-js-csv
  • Excel files
    npm install --save-dev database-js-xlsx
  • INI files
    npm install --save-dev database-js-ini
  • Firebase databases
    npm install --save-dev database-js-firebase
  • JSON files
    npm install --save-dev database-js-json
  • MySQL databases
    npm install --save-dev database-js-mysql
  • PostgreSQL databases
    npm install --save-dev database-js-postgres
  • SQLite databases
    npm install --save-dev database-js-sqlite

How to configure it

In your codecept.json, include DbHelper in the property helpers :

  ...
  "helpers": {
    ...
    "DbHelper": {
      "require": "node_modules/codeceptjs-dbhelper"
    }
  },
  ...

How to use it

The object I of your tests and events will have access to new methods. See the API.

Example 1

BeforeSuite( async( I ) => {
    // The first parameter is the key that will hold a reference to the db
    I.connect( "testdb", "mysql:///root:mypassword@localhost:3306/testdb" );
} );
 
AfterSuite( async( I ) => {
    await I.removeConnection( "testdb" ); // also disconnects
} );
 
 
Before( async( I ) => {
 
    // Delete all the records of the table user
    await I.run( "testdb", "DELETE FROM user" );
 
    // Insert some users
    await I.run( "testdb", "INSERT INTO user ( username, password ) VALUES ( ?, ? )", "admin", "123456" );
    await I.run( "testdb", "INSERT INTO user ( username, password ) VALUES ( ?, ? )", "bob", "654321" );
await I.run( "testdb", "INSERT INTO user ( username, password ) VALUES ( ?, ? )", "alice", "4lic3p4s$" );
 
} );
 
 
// ... your feature ...
 
// ... your scenarios ...

Example 2

Feature( 'Foo' );
 
Scenario( 'Bar', async( I ) => {
 
    // Query a user from the database
    const results = await I.query( "testdb", "SELECT * FROM user WHERE username = ?", "bob" );
    const bob = results[ 0 ];
 
    I.amOnPage( '/login' );
    I.fillField( '#username', bob.username );
    I.fillField( '#password', bob.password );
    I.click( '#ok' );
    I.see( 'Welcome' );
} );

API

/**
 * Connects to a database by the given connection data.
 *
 * @param {string|number} key Key used to identify the database
 * @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
 * @param {object|undefined} driver Driver object, used by `database-js` (optional).
 */
connect( key, conn, driver );
 
/**
 * Disconnects from a given database by its key.
 *
 * @param {string|number} key Key used to identify the database
 */
async disconnect( key );
 
/**
 * Disconnects and removes a database connection by its key.
 *
 * @param {string|number} key Key used to identify the database
 */
async removeConnection( key );
 
/**
 * Queries a database with the given key.
 *
 * @param {string|number} key Key used to identify the database
 * @param {string} command Query
 * @param {any[]} params Parameters of the query
 *
 * @returns {Promise<any[]>} The results of the query.
 */
async query( key, command, ... params );
 
/**
 * Executes a command to the database with the given key.
 *
 * @param {string|number} key Key used to identify the database
 * @param {string} command Command to execute
 * @param {any[]} params Parameters of the command
 *
 * @returns {Promise<any[]>}
 */
async run( key, command, ... params );

License

MIT © Thiago Delgado Pinto

install

npm i codeceptjs-dbhelper

Downloadsweekly downloads

923

version

1.0.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability