npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-mysql

v2.0.1

Published

User friendly preset configuration for Jest & MySQL setup

Downloads

424

Readme

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