Registry / testing / jest-mysql

jest-mysql

JSON →
library2.0.2jsnpmunverified

Jest preset that simplifies MySQL database setup for integration testing. Version 2.0.2 requires Node >=16 and peer dependencies jest 29.x.x and mysql 2.x.x. Offers schema import, automatic database creation, optional truncation after tests, and custom setup hooks. Provides a global db connection for querying within tests. Differentiates from alternatives by being a full preset rather than a library, streamlining configuration via jest-mysql-config.js.

npm install jest-mysql
INSTALL
IMPORT
SIG · JEST-MYSQL
J
jest-mysql
testingjavascriptv2.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

global.db
global.db
import db from 'jest-mysql'
The db connection is set on the global object by the preset and accessed without import.
jest-mysql preset
module.exports = { preset: 'jest-mysql' }
preset: 'jest-mysql' in jest.config.ts without module.exports
The preset is configured in jest config, not imported directly in test files.
postSetup hook
module.exports = { postSetup }
export default postSetup
setupHooks.js must use CommonJS module.exports, as it is required by the preset internals.

Configure Jest preset, set MySQL connection options, and use global.db to run queries in tests.

// jest.config.js module.exports = { preset: 'jest-mysql', }; // jest-mysql-config.js (in root) module.exports = { databaseOptions: { host: 'localhost', port: 3306, user: 'root', password: process.env.MYSQL_PASSWORD ?? '', database: 'test' }, createDatabase: true, dbSchema: 'path/to/schema.sql', truncateDatabase: false }; // In your test file: describe('database tests', () => { it('should have a connection', async () => { const result = await new Promise((resolve, reject) => { global.db.query('SELECT 1 + 1 AS solution', (err, rows) => { if (err) reject(err); else resolve(rows); }); }); expect(result[0].solution).toBe(2); }); });
Debug
Known issues
deprecatedThe mysql npm package is in maintenance mode and does not support authentication caching_sha2_password used by MySQL 8.0+. Use mysql2 instead.
fix
Replace mysql with mysql2 as peer dependency. Note: jest-mysql may not be compatible with mysql2 directly; consider using mysql2 and adapt connection options.
affects: >=0.0.0
gotchaThe jest config must not have a testEnvironment property when using this preset, as it will conflict.
fix
Remove testEnvironment from jest config if present.
affects: >=0.0.0
gotchaThe global.db connection is created using mysql package. It may not support promise-based queries; use callback pattern or promisify.
fix
Wrap queries in Promises or use util.promisify(global.db.query).bind(global.db).
affects: >=0.0.0
breakingVersion 2.x requires Node >=16 and jest 29.x.x. Older Node or jest versions are not supported.
fix
Upgrade Node to >=16 and jest to 29.x.x.
affects: >=2.0.0
Errors
Common errors & fixes
Cannot find module 'jest-mysql' from 'jest.config.js'
jest-mysql is not installed or not in node_modules.
fix
npm install jest-mysql --save-dev
The 'preset' property requires an exact module path
Jest preset path is incorrect; jest-mysql must be a package name.
fix
Use preset: 'jest-mysql' (string) in jest config.
TypeError: global.db.query is not a function
The global.db is a mysql connection object; query method is available, but may be undefined if connection failed.
fix
Check jest-mysql-config.js for correct databaseOptions and ensure MySQL server is running.
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost'
Invalid credentials in databaseOptions.
fix
Verify username, password, host, and port in jest-mysql-config.js.
Upgrade
Version history
2.0.2latest on npm
Audit
Dependencies
jestrequiredpeer dependency required for test runner
mysqlrequiredpeer dependency required for MySQL client
Agent activity
10 hits · last 30 days
node
8
Amazon
1
Resources
jest-mysql — npm install jest-mysql · libregistry