tailwindcss/jest/runInTempDirectory.js

27 lines
569 B
JavaScript
Raw Permalink Normal View History

import fs from 'fs/promises'
2019-03-20 08:17:56 -05:00
import path from 'path'
import { rimraf } from 'rimraf'
2019-03-20 08:17:56 -05:00
let id = 0
/**
* @template T
* @param {() => Promise<T>} callback
* @returns {Promise<T>}
*/
export default async function (callback) {
const workerId = `${process.env.JEST_WORKER_ID}-${id++}`
const tmpPath = path.resolve(__dirname, `../__tmp_${workerId}`)
const currentPath = process.cwd()
2019-03-20 08:17:56 -05:00
await rimraf(tmpPath)
await fs.mkdir(tmpPath)
2019-03-20 08:17:56 -05:00
process.chdir(tmpPath)
let result = await callback()
process.chdir(currentPath)
await rimraf(tmpPath)
return result
2019-03-20 08:17:56 -05:00
}