jest-mysql-diff can find out changes of database after executing some code. It can be used in test framework like jest.
npm install jest-mysql-diff
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();
}
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();
})
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.
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);
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');
After testing, we could call function use
with parameter name
to reset database for next test.
await differ.use('main');
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()
After all tests, you should call end
to close database connection.
await differ.end();