jest-mysql

2.0.0 • Public • Published

jest-mysql codecov

Jest preset for easier setup of MySQL storage.

Resume

Install

npm install jest-mysql --save-dev

Or if you use yarn

yarn add jest-mysql --dev

Make sure jest and mysql are installed as well in the project, as they are required as peer dependencies.

1. Configure jest to use preset

In order for jest to know about this preset, you needs to configure it. You could choose one of the following methods, for further reference checkout the jest documentation on configuration and presets

  • Within package.json
{
  "jest": {
        "preset": "jest-mysql",
        //any other jest configurations
    },
    //rest of package.json configuration
}
  • Create jest.config.js into your root directory
module.exports = {
  preset: "jest-mysql"
  //any other configuration
};

If you have a custom jest.config.js make sure you remove testEnvironment property, otherwise it will conflict with the preset.

2. Create jest-mysql-config.js

Within the current working directory, create jest-mysql-config.js. I.E.

module.exports = {
  databaseOptions: {
    host: "localhost",
    port: 3306,
    user: "root",
    password: "",
    database: "test"
  },
  createDatabase: true,
  dbSchema: "DB_creation.sql",
  truncateDatabase: false
};

2.1 Option definitions:

  • databaseOptions - Required {Object} Connection options used to be used by the MySQL client For further info regarding what parameters are supported, check this reference
  • createDatabase - Optional {Boolean} If this is set to true, a database will be created if database with such name does not exist in your MySQL instance
  • dbSchema - Optional {String} Path to the MySQL dump schema for the database (this can be any database dump; regardless if data is exported or only the tables structure).
  • truncateDatabase: Optional {Boolean} If this is set to true, the database will be truncated upon tests finishing, see globalTeardown for further reference

3. Database connection

For utility purposes, the connection to the database has been made available within the global context and it can be accessed as follows:

global.db;

4. Setup Hooks

If you need further customization after the database has been created and schema imported, you could provide a custom hooks file which will be exectuted after the initial setup has been completed ( if createDatabase - the database has been created and the connection has been established to the database).

  • Create within the current working directory setupHooks.js
  • The provided functions must be async or Promise based Example structure:
const { setupDummyUsers } = require("tests/fixtures/dummyUser");

async function postSetup() {
  await setupDummyUsers();
}

module.exports = {
  postSetup
};

5. All done!

You should be able to access the connection to the database and query if needed. Enjoy!

it("should have created a database with User table and 3 dummy user records", done => {
  const users = global.db.query(
    "SELECT * FROM users",
    (error, results, fields) => {
      if (error) {
        throw error;
      }
      expect(results).toHaveLength(3);
      done();
    }
  );
});

You can enable debug logs by setting environment variable DEBUG=jest-mysql:*

License

MIT

Package Sidebar

Install

npm i jest-mysql

Weekly Downloads

86

Version

2.0.0

License

MIT

Unpacked Size

48 kB

Total Files

43

Last publish

Collaborators

  • dippy