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-diff

v0.0.2

Published

a mysql differ to help testing

Downloads

2

Readme

Introduction

jest-mysql-diff can find out changes of database after executing some code. It can be used in test framework like jest.

Install

npm install jest-mysql-diff

How to use

const MysqlDiffer = require('jest-mysql-diff');
const differ = new MysqlDiffer({
  mysql: {
    host: 'your-host',
    user: 'your-user',
    password: 'your-password',
    database: 'your-database'
  }
});

async beforeAllTest() {
  await differ.register('main', 'your-sql-absolute-path');
}

async someTest() {
  await differ.use('main');
  await runSomeCode();
  const changes = await differ.diff();
}

async afterAllTest() {
  await differ.end();
}

Used in Jest

You can create MysqlDiffer object and register some sql files in globalSetup. And then call use in beforeEach to reset database. Use diff to get changes and compare it with snapshot to make sure database changes are expected. Finally, we can call end in globalTeardown to close connecton.

test('some test', async () => {
  await differ.use('main');
  await runSomeCode();
  const changes = await differ.diff();
  expect(changes).toMatchSnapshot();
})

Make it fast

MysqlDiffer will read whole database and make tests become very slow. Running Mysql in memory can speed it up.

In unix system, there is a directory /dev/shm, it works just like normal file system except it keeps data in momery instead of disk. So just set Mysql data directory to it.

API

MysqlDiffer

this library exports a class, constructor accept one parameter options.

const options = {
  mysql: {
    host: 'your-host',
    user: 'your-user',
    password: 'your-password',
    database: 'your-database'
  }
}

const differ = new MysqlDiffer(options);

MysqlDiffer.register

register will execute a sql file and remember how database looks like. First parameter will name the corresponding sql file.

await differ.register('main', 'your-sql-absolute-path');

MysqlDiffer.use

After testing, we could call function use with parameter name to reset database for next test.

await differ.use('main');

MysqlDiffer.diff

diff will compare present database state with the state before testing. diff will return a readable result that can tell us how database is changed.

const changes = await differ.diff()

MysqlDiffer.end

After all tests, you should call end to close database connection.

await differ.end();