I'm new to JEST and testing in general and having trouble figuring out the following.
I have the following script that is part of a CLI tool.
I would like to stop the spinner outputs when testing.
I have tried spyOn/mock, but to no avail.
const ora = require('ora');
const spinner = new ora();
const chalk = require('chalk');
const fs = require('fs');
module.exports = path =>
new Promise((resolve, reject) => {
spinner.text = chalk.blue('Creating directory...');
spinner.start();
fs.mkdir(path, err => {
if (!err) {
spinner.succeed(chalk.bgGreen('Directory created\n'));
resolve(true);
} else {
spinner.fail(chalk.bgRed(`Directory already exists: ${path}`));
reject(err);
}
});
});
this is my test:
const createDir = require('./utils/createDir');
const fs = require('fs');
describe('createDir function', () => {
const folders = {
base: './.test',
fail: './.test/fail',
success: './.test/success'
};
beforeAll(() => {
fs.mkdirSync(folders.fail, { recursive: true });
});
afterAll(() => {
fs.rmdirSync(folders.base, { recursive: true });
});
it('creates the directory', async () => {
await expect(createDir(folders.success)).resolves.toBe(true);
});
it('fails if directory exists', async () => {
await expect(createDir(folders.fail)).rejects.toThrow();
});
});
You should be able to just add
jest.mock('ora')
in the beginning of your test. It will auto-mock the entire library replacing each of the methods with jest.fn() (without any implementation) so the calls from the implementation will have no effect on the output.
EDIT by Ben:
The functional mock turned out to be this:
jest.mock('ora', () => {
return jest.fn().mockImplementation(() => {
return {
start: () => {},
fail: () => {},
succeed: () => {}
};
});
});
Related
I have following script employeeJobTransferServiceLive.js:
const createJobTrasferSetPayloadOutput = async (jobTransferSetPayload) => {
const file = './output/jobTransferSetPayload.json';
try {
await fs.writeFile(file, JSON.stringify(jobTransferSetPayload, null, 4), { overwrite: false });
return JSON.stringify(jobTransferSetPayload);
} catch (err) {
return err;
}
};
so I created following unit test employeeJobTransferServiceLive.test.js:
const empJobTransferSrvc = require('../../../libs/employeeJobTransferSet/employeeJobTransferServiceLive');
jest.mock('fs', () => ({
promises: {
writeFile: jest.fn(),
},
}));
describe('sendWFDRequest is called', () => {
afterEach(() => {
jest.resetModules();
jest.clearAllMocks();
});
it('Create and Write to the file', async () => {
const result = await empJobTransferSrvc.createJobTrasferSetPayloadOutput(jobTransferSetPayload);
expect(JSON.parse(result)[0]).toEqual(jobTransferSetPayload[0]);
expect(result).not.toBeNull();
});
});
The problem the compiler complain about test coverage for line return err; after catch.
I don't know how to create unit test to cover it.
Maybe it is not best answer, but at least it works fine for me
const fs = require('fs').promises;
const empJobTransferSrvc = require('../../../libs/employeeJobTransferSet/employeeJobTransferServiceLive');
jest.mock('fs', () => ({
promises: {
writeFile: jest.fn(),
},
}));
describe('sendWFDRequest is called', () => {
afterEach(() => {
jest.resetModules();
jest.clearAllMocks();
});
it('Create and Write to the file', async () => {
const result = await empJobTransferSrvc.createJobTrasferSetPayloadOutput(jobTransferSetPayload);
expect(JSON.parse(result)[0]).toEqual(jobTransferSetPayload[0]);
expect(result).not.toBeNull();
});
it('should handle a writeFile error', async () => {
jest.spyOn(fs, 'writeFile').mockRejectedValue(new Error('something went wrong'));
try {
await empJobTransferSrvc.createJobTrasferSetPayloadOutput(jobTransferSetPayload, logger);
} catch (error) {
expect(error.message).toEqual('Error: something went wrong');
}
});
});
Here is my mock file __mocks__/#optimizely/optimizely-sdk.js
const optimizelySDK = jest.requireActual('#optimizely/optimizely-sdk')
optimizelySDK.createInstance().onReady = () => ({ success: false }))
module.exports = optimizelySDK
Here is my test file Optimizely.test.js
import optimizelySDK from '#optimizely/optimizely-sdk'
test('onReady', () => {
const response = optimizelySDK.createInstance().onReady()
expect(response).toBe({ success: false })
})
I think I might be going about this all wrong. This worked perfectly when I try this with lodash. I believe this is because optimizelySDK is a class. I think I should be mocking that instead. How do I successfully mock and test optimizelySDK?
For anyone who came across this on Google, I had the same problem and got it working with jest:
jest.mock('#optimizely/optimizely-sdk', () => ({
...jest.requireActual('#optimizely/optimizely-sdk'),
createInstance: () => ({
getEnabledFeatures: jest.fn().mockReturnValueOnce(['featureA', 'featureB']),
onReady: jest.fn().mockResolvedValueOnce({ status: 200 })
})
}))
describe('my-test', () => {
it('should pass', async () => {
const result = await getFeatures()
console.log(result) // ['featureA', 'featureB']
// assert on result here
});
});
where my code looked something like:
const getFeatures = async (event) => {
try {
const optimizelyInstance = optimizelySDK.createInstance({
sdkKey: process.env.OPTIMIZLEY_SDK_KEY,
});
const optimizelyParameters = {}
return optimizelyInstance.onReady().then(() => {
const result = optimizelyInstance.getEnabledFeatures('id', optimizelyParameters);
return result;
});
} catch (err) {
console.error('Could not get features', err);
}
};
I want to unit test the exported method in the code below. Trying to write unit tests for a function that is downloading a zip file from a localhost server.I will write my function bellow so you understand better:
export const downloadCdn = async (cdnUrl, out) => {
const download = (resolve, reject) => {
const req = request({
method: 'GET',
uri: cdnUrl
});
req.on('response', (data) => {
// do something
});
req.on('error', (data) => {
// do something
});
req.on('data', (chunk) => {
// do something
});
req.on('end', () => {
console.log('download done');
});
req.pipe(out);
out.on('close', () => {
resolve([null, 'done']);
});
};
const downloadSummary = new Promise(download);
return downloadSummary
.then(() => [null, 'Done'])
.catch(err => [err, null]);
};
Here are my test file, what I'm trying to achieve is to have unit test that validates the download of the zip file:
import request from 'request';
import * as Module from './downloadCdn';
jest.mock('request', () => {
const mockRequest = {
pipe: jest.fn(),
on: jest.fn(),
};
return function () {
return mockRequest;
};
});
describe('Downloading a file', () => {
it('Should find the module', () => {
expect(typeof Module.downloadCdn === 'function').toBeTruthy();
});
it('Should download the zip', async () => {
const [error, response] = await Module.downloadCdn(cdnUrl, out);
expect(response === 'Done').toBeTruthy();
expect(error === null).toBeTruthy();
});
});
The response from the Promise, I receive inside the test is null, no error catching. Here is the error received from jest:
expect(received).toBeTruthy()
Expected value to be truthy, instead received false
While mocking the request, you should resolve the promise. I think that promise is not resolving that's why it's not working. I hope that the below code will be fixed your problem.
jest.mock('request', () => {
const mockRequest = {
pipe: jest.fn(),
on: (parameter, callback) => {
callback();
},
};
return function () {
return mockRequest;
};
});
I am trying to unit test a JavaScript promise, that contains a firebase query, using mocha, chai, sinon. I am trying to mock the database using sinon rather than actually making a request to the database. However, I am unable to implement it correctly.
Here is my promise in the file '/services/pr_services:
exports.getUserInfo = (userId) => {
return new Promise((resolve, reject) => {
const userProfile = {};
const userProfileRef = database.ref('profiles').child(userId);
userProfileRef.once('value', (snap) => {
if (snap.exists()) {
const userProfileData = snap.val();
resolve(userProfile);
} else {
reject();
}
});
});
};
The variable database contains the database configuration like credentials, database url, etc
Here is my code for the test case:
const chai = require('chai');
const sinon = require('sinon');
const admin = require('firebase-admin');
const database = require('../database');
const services = require('../services/pr_services');
const should = chai.should();
describe('Database functions', () => {
let adminInitStub;
before(() => {
adminInitStub = sinon.stub(admin, 'initializeApp');
});
describe('get profile info', () => {
it('should return a non empty object', (done) => {
beforeEach(() => {
services.getUserInfo = sinon.stub();
});
afterEach(() => {
services.getUserInfo.reset();
});
const userId = 'jim123';
const snap = {
name: 'Jim Dani',
address: 'Porto'
};
const userProfileRef = database.ref('profiles').child(userId);
userProfileRef.once('value').returns(Promise.resolve(snap));
services.getUserInfo
.then(info => {
info.should.be.a('object');
info.should.equal(snap);
done();
})
.catch(err => {
should.not.exist(err);
done();
});
});
});
after(() => {
adminInitStub.restore();
test.cleanup();
});
});
Can anyone point out where I am going wrong and kindly point me in right direction.
Thanks.
My function is
exports.downloadFromBucket = function(fileKey) {
const localPath = `${process.cwd()}/data/${fileKey}`
return new Promise((resolve, reject) => {
const localFile = fs.createWriteStream(localPath)
const awsStream = s3.getObject({
Bucket: process.env.UPLOAD_BUCKET,
Key: fileKey
})
.createReadStream()
.on('error', (err) => {
logger.info('Error downloading file', err)
return reject(err)
})
.on('finish', () => {
logger.info('Completed downloading')
return resolve(localPath)
})
.pipe(localFile)
})
}
How would I go about writing a unit test for this using mocha and sinon?
This may not be the prettiest solution but assuming you want to mock s3 and fs and test the on('error') and on('finish') behavior:
You could use a custom s3 mocking class, stub the original s3 and fs with sinon and trigger the events you want to test.
// Custom S3 Mocking Library
class S3MockLibrary {
constructor() {
this.events = {};
}
getObject(options) {
return this;
}
createReadStream() {
return this;
}
on(event, func) {
this.events[event] = func;
return this;
}
pipe(file) {
return this;
}
emit(event, err) {
this.events[event](err);
}
}
Test on('finish')
it('should verify', async () => {
const s3Mock = new S3MockLibrary();
const fsStub = sinon.stub(fs, 'createWriteStream').returns('success');
const s3Stub = sinon.stub(s3, 'getObject').returns(s3Mock);
// Emit the finish event async
setTimeout(() => {
s3Mock.emit('finish');
}, 0);
const result = await downloadFromBucket('test');
fsStub.restore();
s3Stub.restore();
sinon.assert.calledOnce(fsStub);
sinon.assert.calledOnce(s3Stub);
assert.equal(result, `${process.cwd()}/data/test`);
});
Test on('error)
it('should fail', async () => {
const s3Mock = new S3MockLibrary();
const fsStub = sinon.stub(fs, 'createWriteStream').returns('success');
const s3Stub = sinon.stub(s3, 'getObject').returns(s3Mock);
setTimeout(() => {
s3Mock.emit('error', 'testError');
}, 0);
let error;
await downloadFromBucket('test').catch((err) => {
error = err;
});
fsStub.restore();
s3Stub.restore();
sinon.assert.calledOnce(fsStub);
sinon.assert.calledOnce(s3Stub);
assert.equal(error, 'testError');
});