I have a small function designed to unzip a file using 'unzipper' and extract to a given location.
when unit testing the function times out, for unit testing I am using jest.
see below code :
exports.unzipFile = async (folderPath) => {
return new Promise((resolve, reject) => {
fs.createReadStream(folderPath)
.pipe(unzipper.Extract({ path: tmpPath+ path.parse(folderPath).name })).on('close', () => resolve()).on('error', (error) => reject(error))
})
The function itself works as expected. I have tried some changes to the function but this seems to break the function. I need this function to execute fully as the unzipped file is then relied on later in the program.
The program is written in node 16. Any help would be appreciated thanks
EDIT: this is my current unit test- I have tried various things :
const { PassThrough } = require('stream')
const os = require('os');
const unzipper = require("unzipper")
const fs = require("fs")
let tmpdir, mockReadStream
beforeEach(() => {
tmpdir = os.tmpdir() + "/uploadFolder/";
if (!fs.existsSync(tmpdir)){
fs.mkdirSync(tmpdir);
}
fs.writeFileSync(tmpdir+"tempfile.zip", "file to be used")
mockReadStream = new PassThrough()
})
afterEach(() => {
// Restore mocks
jest.clearAllMocks()
})
describe('Test helper.js unzip method', () => {
test('should be able to unzip file ', async () => {
jest.isolateModules(() => {
helper = require('helper')
})
const result = await helper.unzipFile(tmpdir+"tempfile.zip")
console.log(result)
})
})