Skip to content

Instantly share code, notes, and snippets.

@silkentrance
Last active February 6, 2020 00:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silkentrance/f0e1ff1d689891e124e2a8b73bd31299 to your computer and use it in GitHub Desktop.
Save silkentrance/f0e1ff1d689891e124e2a8b73bd31299 to your computer and use it in GitHub Desktop.
A jest test environment for use with node tmp - temporary work around for https://github.com/raszi/node-tmp/issues/229

A real version of this will be released soon that can then be installed using npm/yarn.

module.exports = {
testEnvironment: './test/util/TmpEnvironment' // change this to the location where you put TmpEnvironment.js
}
// you can access tmp directly from global scope
const t = tmp.fileSync();
// NOTE: place this under e.g. test/util/TmpEnvironment.js
const NodeEnvironment = require('jest-environment-node');
const tmp = require('tmp');
// you may want to set
// tmp.setGracefulCleanup()
class TmpEnvironment extends NodeEnvironment {
async setup() {
await super.setup();
this.global.tmp = tmp;
}
}
module.exports = TmpEnvironment;
@armano2
Copy link

armano2 commented Feb 6, 2020

for projects that uses typescript:

declare global {
	var tmp: typeof import('tmp')
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment