I'm asking this after a lot of research
I have these files
// connection.js
const mysql = require('mysql');
module.exports = mysql.getConnection();
// a.js
const connection = require('./connection');
function doQuery() {
const q = 'select * from table';
connection.query(q, (err, res) => {
... do some stuff
})
}
module.exports = doQuery;
When I what to do a test with jest (deleting unnecessary things to better read)
// test.js
const jest = require('jest');
const a = require('./a.js');
const connection = {
query: (query, cb) => cb('error', null),
};
jest.mock('./connection.js', () => connection);
test('testing something', () => {
expect(a.doQuery).to.be.true //this is just an example
});
I'm getting the next error
The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
Invalid variable access: connection
I tried moving the files in the same folder, with relative paths, absolute paths, moving the order of imports, and I really can't get it.
I really don't know how to fix this, also I'm migrating from proxyquire to jest that is the reason that I'm doing this and I can't use proxyquire anymore.