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

@mangroves/jest-docker-mysql

v0.1.3

Published

Jest preset to run MySQL in docker

Downloads

111

Readme

jest-docker-mysql

Jest preset to run MySQL in docker

Features

  • Run a MySQL docker container before test and remove it after test finished
  • Run SQL scripts before each test suite (test file) and run clean-up SQL scripts after each test suite
  • Run SQL scripts before and after each test case according to the file name specified in test case name string

Caveat: Do note that the test cases must not run concurrently as they use the same MySQL database. Thus the --runInBand option must be added to the jest command

jest --runInBand

Install

yarn add @mangroves/jest-docker-mysql --dev

You can use npm or other tools as an alternative.

Usage

There are 3 alternatives to use this package.

Use as a preset

In your jest.config.js (Or jest field in package.json):

module.exports = {
  preset: '@mangroves/jest-docker-mysql',
}

Specify scripts

If you've already set another package as the preset, you can set setupFilesAfterEnv, globalSetup and globalTeardown manually.

// jest.config.js
module.exports = {
  setupFilesAfterEnv: [
    '@mangroves/jest-docker-mysql/lib/setup-after-env.js',
    // ...other files
  ],
  globalSetup: '@mangroves/jest-docker-mysql/lib/global-setup.js',
  globalTeardown: '@mangroves/jest-docker-mysql/lib/global-teardown.js',
}

Import Setup and Teardown Scripts

Maybe even globalSetup and globalTeardown are taken by your own script, but don't worry, you can still use this package.

// jest.config.js
module.exports = {
  setupFilesAfterEnv: [
    '@mangroves/jest-docker-mysql/lib/setup-after-env.js',
    // ...other files
  ],
  globalSetup: 'path/to/your/script-setup.js',
  globalTeardown: 'path/to/your/script-teardown.js',
}

In your setup script:

// path/to/your/script-setup.js
import { globalSetup } from '@mangroves/jest-docker-mysql/lib/global-setup.js'

const yourSetup = async () => {
  await globalSetup()
}

export default yourSetup

The same for the teardown script.

Connect to MySQL Container

After you execute the jest command and the test cases are running, you can connect to the MySQL container and use it.

Example:

// xxx.spec.js
import { config } from '@mangroves/jest-docker-mysql/lib/config'

describe('Your test case', () => {
  let connection
  beforeAll(async () => {
    // Use your mysql library to connect the database
    connection = await createConnection({
      host: config.db.host,
      port: config.db.port,
      user: config.db.user,
      pass: config.db.pass,
    })
  })

  afterAll(async () => {
    // Don't forget to close connection after test run
    connection.close()
  })
})

Environment Variables

Sometimes you don't want to stop and remove the MySQL container, or you don't want to start a container every time you run the test.

You can set an environment variable whose key is JEST_DOCKER_MYSQL_ENV to local.

And you may need this lib: cross-env

It's practical especially in jest watch mode:

cross-env JEST_DOCKER_MYSQL_ENV=local jest --watch --runInBand

Don't forget to remove the container manually if you don't need it any more.

Config File

Create a file named jest-docker-mysql-config.js and put it under the root directory as the config file.

Use defineConfig exported by this package to get better type intellisense.

// jest-docker-mysql-config.js
const { defineConfig } = require('@mangroves/jest-docker-mysql')

module.exports = defineConfig({
  db: {
    port: 6666,
  },
})