Skip to content

Commit

Permalink
test: 'fix' memory leak in graceful-fs (#211)
Browse files Browse the repository at this point in the history
graceful-fs and jest does not like each other, so this removes the graceful-fs implementation before running tests and adds it back afterwards.

jestjs/jest#6399
jestjs/jest#6814
isaacs/node-graceful-fs#102
  • Loading branch information
B4nan committed Oct 22, 2019
1 parent 38ec973 commit 5e84847
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
"homepage": "https://mikro-orm.io",
"scripts": {
"build": "rimraf dist && tsc",
"test": "jest --runInBand",
"coverage": "rimraf temp tests/generated-entities && jest --runInBand --coverage",
"pretest": "node tests/pre-test",
"test": "node --expose-gc ./node_modules/.bin/jest --runInBand --logHeapUsage",
"posttest": "node tests/post-test",
"coverage": "rimraf temp tests/generated-entities && yarn test --coverage",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"lint": "tslint -p ."
},
Expand Down
13 changes: 13 additions & 0 deletions tests/post-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { writeFileSync, readFileSync, existsSync, unlinkSync } = require('fs');

/**
* This un-patches the graceful-fs library which is causing memory leaks when running via jest
*/
const path = process.cwd() + '/node_modules/graceful-fs/graceful-fs.js';
const backup = process.cwd() + '/node_modules/graceful-fs/graceful-fs.js.backup';

if (existsSync(backup)) {
const original = readFileSync(backup);
writeFileSync(path, original);
unlinkSync(backup);
}
11 changes: 11 additions & 0 deletions tests/pre-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { writeFileSync, readFileSync } = require('fs');

/**
* This patches the graceful-fs library which is causing memory leaks when running via jest
*/
const replacement = `const fs = require('fs'); module.exports = Object.assign(fs, { gracefulify: () => {} });`;
const path = process.cwd() + '/node_modules/graceful-fs/graceful-fs.js';
const backup = process.cwd() + '/node_modules/graceful-fs/graceful-fs.js.backup';
const original = readFileSync(path);
writeFileSync(backup, original);
writeFileSync(path, replacement);

0 comments on commit 5e84847

Please sign in to comment.