Best JavaScript code snippet using root
exec.test.js
Source:exec.test.js
...10 cpp = require('child-process-promise');11 exec = require('./exec');12 });13 it(`exec command with no arguments successfully`, async () => {14 mockCppSuccessful(cpp);15 await exec.execWithRetriesAndLogs('bin');16 expect(cpp.exec).toHaveBeenCalledWith(`bin`, { timeout: 0 });17 });18 it(`exec command with no arguments successfully`, async () => {19 const successfulResult = returnSuccessfulNoValue();20 const resolvedPromise = Promise.resolve(successfulResult);21 cpp.exec.mockReturnValueOnce(resolvedPromise);22 await exec.execWithRetriesAndLogs('bin');23 expect(cpp.exec).toHaveBeenCalledWith(`bin`, { timeout: 0 });24 });25 it(`exec command with arguments successfully`, async () => {26 mockCppSuccessful(cpp);27 const options = {args: `--argument 123`};28 await exec.execWithRetriesAndLogs('bin', options);29 expect(cpp.exec).toHaveBeenCalledWith(`bin --argument 123`, { timeout: 0 });30 });31 it(`exec command with arguments and prefix successfully`, async () => {32 mockCppSuccessful(cpp);33 const options = {34 args: `--argument 123`,35 prefix: `export MY_PREFIX`36 };37 await exec.execWithRetriesAndLogs('bin', options);38 expect(cpp.exec).toHaveBeenCalledWith(`export MY_PREFIX && bin --argument 123`, { timeout: 0 });39 });40 it(`exec command with arguments and status logs successfully`, async () => {41 mockCppSuccessful(cpp);42 const options = {args: `--argument 123`};43 const statusLogs = {44 trying: `trying status log`,45 successful: `successful status log`46 };47 await exec.execWithRetriesAndLogs('bin', options, statusLogs);48 expect(cpp.exec).toHaveBeenCalledWith(`bin --argument 123`, { timeout: 0 });49 });50 it(`exec command should output success and err logs`, async () => {51 mockCppSuccessful(cpp);52 await exec.execWithRetriesAndLogs('bin');53 expect(logger.trace).toHaveBeenCalledWith({ event: 'EXEC_SUCCESS', stdout: true }, '"successful result"');54 expect(logger.trace).toHaveBeenCalledWith({ event: 'EXEC_SUCCESS', stderr: true }, 'err');55 });56 it(`exec command should output a plain success if no output was made`, async () => {57 const cppResult = {58 childProcess: {59 exitCode: 060 }61 };62 cpp.exec.mockReturnValueOnce(Promise.resolve(cppResult));63 await exec.execWithRetriesAndLogs('bin');64 expect(logger.trace).toHaveBeenCalledWith({ event: 'EXEC_SUCCESS' }, '');65 expect(logger.trace).toHaveBeenCalledTimes(1);66 });67 it(`exec command should output success with high severity if verbosity set to high`, async () => {68 mockCppSuccessful(cpp);69 await exec.execWithRetriesAndLogs('bin', { verbosity: 'high' });70 expect(logger.debug).toHaveBeenCalledWith({ event: 'EXEC_SUCCESS', stdout: true }, '"successful result"');71 expect(logger.debug).toHaveBeenCalledWith({ event: 'EXEC_SUCCESS', stderr: true }, 'err');72 expect(logger.trace).not.toHaveBeenCalledWith(expect.objectContaining({event: 'EXEC_SUCCESS'}), expect.anything());73 });74 it(`exec command with undefined return should throw`, async () => {75 cpp.exec.mockReturnValueOnce(undefined);76 try {77 await exec.execWithRetriesAndLogs('bin');78 fail('should throw');79 } catch (ex) {80 expect(ex).toBeDefined();81 }82 });83 it(`exec command and fail with error code`, async () => {84 const errorResult = returnErrorWithValue('error result');85 const rejectedPromise = Promise.reject(errorResult);86 cpp.exec.mockReturnValueOnce(rejectedPromise);87 try {88 await exec.execWithRetriesAndLogs('bin', null, '', 1, 1);89 fail('expected execWithRetriesAndLogs() to throw');90 } catch (object) {91 expect(cpp.exec).toHaveBeenCalledWith(`bin`, { timeout: 0 });92 expect(logger.error.mock.calls).toHaveLength(3);93 }94 });95 it(`exec command and fail with error code, report only to debug log if verbosity is low`, async () => {96 const errorResult = returnErrorWithValue('error result');97 const rejectedPromise = Promise.reject(errorResult);98 cpp.exec.mockReturnValueOnce(rejectedPromise);99 try {100 await exec.execWithRetriesAndLogs('bin', { verbosity: 'low' }, '', 1, 1);101 fail('expected execWithRetriesAndLogs() to throw');102 } catch (object) {103 expect(cpp.exec).toHaveBeenCalledWith(`bin`, { timeout: 0 });104 expect(logger.error).not.toHaveBeenCalled();105 expect(logger.debug.mock.calls).toHaveLength(4);106 }107 });108 it(`exec command and fail with timeout`, async () => {109 const errorResult = returnErrorWithValue('error result');110 const rejectedPromise = Promise.reject(errorResult);111 cpp.exec.mockReturnValueOnce(rejectedPromise);112 try {113 await exec.execWithRetriesAndLogs('bin', { timeout: 1 }, '', 1, 1);114 fail('expected execWithRetriesAndLogs() to throw');115 } catch (object) {116 expect(cpp.exec).toHaveBeenCalledWith(`bin`, { timeout: 1 });117 expect(logger.error.mock.calls).toHaveLength(3);118 }119 });120 it(`exec command with multiple failures`, async () => {121 const errorResult = returnErrorWithValue('error result');122 const rejectedPromise = Promise.reject(errorResult);123 cpp.exec.mockReturnValueOnce(rejectedPromise)124 .mockReturnValueOnce(rejectedPromise)125 .mockReturnValueOnce(rejectedPromise)126 .mockReturnValueOnce(rejectedPromise)127 .mockReturnValueOnce(rejectedPromise)128 .mockReturnValueOnce(rejectedPromise);129 try {130 await exec.execWithRetriesAndLogs('bin', null, '', 6, 1);131 fail('expected execWithRetriesAndLogs() to throw');132 } catch (object) {133 expect(cpp.exec).toHaveBeenCalledWith(`bin`, { timeout: 0 });134 expect(cpp.exec).toHaveBeenCalledTimes(6);135 expect(object).toBeDefined();136 }137 });138 it(`exec command with multiple failures and then a success`, async () => {139 const errorResult = returnErrorWithValue('error result');140 const rejectedPromise = Promise.reject(errorResult);141 const successfulResult = returnSuccessfulWithValue('successful result');142 const resolvedPromise = Promise.resolve(successfulResult);143 cpp.exec.mockReturnValueOnce(rejectedPromise)144 .mockReturnValueOnce(rejectedPromise)145 .mockReturnValueOnce(rejectedPromise)146 .mockReturnValueOnce(rejectedPromise)147 .mockReturnValueOnce(rejectedPromise)148 .mockReturnValueOnce(resolvedPromise);149 await exec.execWithRetriesAndLogs('bin', null, '', 6, 1);150 expect(cpp.exec).toHaveBeenCalledWith(`bin`, { timeout: 0 });151 expect(cpp.exec).toHaveBeenCalledTimes(6);152 });153});154describe('spawn', () => {155 let exec;156 let cpp;157 let log;158 beforeEach(() => {159 jest.mock('./logger');160 jest.mock('child-process-promise');161 cpp = require('child-process-promise');162 exec = require('./exec');163 log = require('./logger');164 const childProcess = {165 pid: 2018,166 stdout: toStream('hello'),167 stderr: toStream('world'),168 };169 const cpPromise = Promise.resolve({ code: 0, childProcess });170 cpp.spawn.mockReturnValue(Object.assign(cpPromise, {171 childProcess172 }));173 });174 it('spawns an attached command with ignored input and piped output', () => {175 const command = 'command';176 const flags = ['--some', '--value', Math.random()];177 exec.spawnAndLog(command, flags);178 expect(cpp.spawn).toBeCalledWith(command, flags, {179 stdio: ['ignore', 'pipe', 'pipe']180 });181 });182 it('should collect output and log it', async () => {183 await exec.spawnAndLog('command', []);184 await nextCycle();185 expect(log.debug).toBeCalledWith(expect.objectContaining({ event: 'SPAWN_CMD' }), '[pid=2018] command');186 expect(log.trace).toBeCalledWith(expect.objectContaining({ event: 'SPAWN_END' }), 'command finished with code = 0');187 expect(log.trace).toBeCalledWith(expect.objectContaining({ event: 'SPAWN_STDOUT', stdout: true }), 'hello');188 expect(log.error).toBeCalledWith(expect.objectContaining({ event: 'SPAWN_STDERR', stderr: true }), 'world');189 });190 it('should not log output if silent: true', async () => {191 await exec.spawnAndLog('command', [], { silent: true });192 await nextCycle();193 expect(log.debug).toBeCalledWith(expect.objectContaining({ event: 'SPAWN_CMD' }), '[pid=2018] command');194 expect(log.trace).toBeCalledWith(expect.objectContaining({ event: 'SPAWN_END' }), 'command finished with code = 0');195 expect(log.trace).not.toBeCalledWith(expect.objectContaining({ event: 'SPAWN_STDOUT', stdout: true }), expect.any(String));196 expect(log.error).not.toBeCalledWith(expect.objectContaining({ event: 'SPAWN_STDERR', stderr: true }), expect.any(String));197 });198 it('should log erroneously finished spawns', async () => {199 const childProcess = {200 pid: 8102,201 stdout: toStream(''),202 stderr: toStream('Some error.'),203 };204 cpp.spawn.mockReturnValue(Object.assign(Promise.reject({ code: -2, childProcess }), {205 childProcess206 }));207 await exec.spawnAndLog('command', []).catch(() => {});208 await nextCycle();209 expect(log.debug).toBeCalledWith(expect.objectContaining({ event: 'SPAWN_CMD' }), '[pid=8102] command');210 expect(log.trace).toBeCalledWith(expect.objectContaining({ event: 'SPAWN_END' }), 'command finished with code = -2');211 expect(log.error).toBeCalledWith(expect.objectContaining({ event: 'SPAWN_STDERR', stderr: true }), 'Some error.');212 });213 it('should log immediate spawn errors', async () => {214 const childProcess = {215 pid: null,216 exitCode: -2,217 stdout: toStream(''),218 stderr: toStream('Command `command` not found.'),219 };220 cpp.spawn.mockReturnValue(Object.assign(Promise.resolve({ childProcess }), {221 childProcess222 }));223 await exec.spawnAndLog('command', []);224 await nextCycle();225 expect(log.debug).toBeCalledWith(expect.objectContaining({ event: 'SPAWN_CMD' }), '[pid=null] command');226 expect(log.error).toBeCalledWith(expect.objectContaining({ event: 'SPAWN_ERROR' }), 'command failed with code = -2');227 expect(log.error).toBeCalledWith(expect.objectContaining({ event: 'SPAWN_STDERR', stderr: true }), 'Command `command` not found.');228 });229});230function nextCycle() {231 return new Promise(resolve => setTimeout(resolve));232}233function toStream(string) {234 const {Readable} = require('stream');235 const stream = new Readable();236 stream._read = () => {};237 stream.push(string);238 stream.push(null);239 return stream;240}241function returnSuccessfulWithValue(value) {242 const result = {243 stdout: JSON.stringify(value),244 stderr: "err",245 childProcess: {246 exitCode: 0247 }248 };249 return result;250}251function returnErrorWithValue(value) {252 const result = {253 stdout: "out",254 stderr: value,255 childProcess: {256 exitCode: 1257 }258 };259 return result;260}261function returnSuccessfulNoValue() {262 const result = {263 childProcess: {264 exitCode: 0265 }266 };267 return result;268}269function mockCppSuccessful(cpp) {270 const successfulResult = returnSuccessfulWithValue('successful result');271 const resolvedPromise = Promise.resolve(successfulResult);272 cpp.exec.mockReturnValueOnce(resolvedPromise);273 return successfulResult;...
Using AI Code Generation
1var root = require('./root.js');2root.mockCppSuccessful();3var cpp = require('./cpp.js');4exports.mockCppSuccessful = function() {5 cpp.mockCppSuccessful();6}7exports.mockCppSuccessful = function() {8}
Using AI Code Generation
1var cpp = require('bindings')('mockCppSuccessful');2cpp.mockCppSuccessful();3var cpp = require('bindings')('mockCppFail');4cpp.mockCppFail();5var cpp = require('bindings')('mockCppFail');6var sub = cpp.sub;7sub.mockCppFail();8var cpp = require('bindings')('mockCppSuccessful');9var sub = cpp.sub;10sub.mockCppSuccessful();11var cpp = require('bindings')('mockCppSuccessful');12var sub = cpp.sub;13sub.mockCppSuccessful();14var cpp = require('bindings')('mockCppFail');15var sub = cpp.sub;16sub.mockCppFail();17var cpp = require('bindings')('mockCppFail');18var sub = cpp.sub;19sub.mockCppFail();20var cpp = require('bindings')('mockCppSuccessful');21var sub = cpp.sub;22sub.mockCppSuccessful();23var cpp = require('bindings')('mockCppFail');24var sub = cpp.sub;25sub.mockCppFail();26var cpp = require('bindings')('mockCppSuccessful');27var sub = cpp.sub;28sub.mockCppSuccessful();29var cpp = require('bindings')('mockCppFail');30var sub = cpp.sub;31sub.mockCppFail();32var cpp = require('bindings')('mockCppSuccessful');33var sub = cpp.sub;34sub.mockCppSuccessful();35var cpp = require('bindings')('mockCppFail');
Using AI Code Generation
1var mockCppSuccessful = require('../build/Release/mockCpp.node').mockCppSuccessful;2var mockCppFailure = require('../build/Release/mockCpp.node').mockCppFailure;3var assert = require('assert');4describe('mockCppSuccessful', function() {5 it('should return 0', function() {6 assert.equal(0, mockCppSuccessful());7 });8});9describe('mockCppFailure', function() {10 it('should return 1', function() {11 assert.equal(1, mockCppFailure());12 });13});14var mockCppSuccessful = require('../build/Release/mockCpp.node').mockCppSuccessful;15var mockCppFailure = require('../build/Release/mockCpp.node').mockCppFailure;16var assert = require('assert');17describe('mockCppSuccessful', function() {18 it('should return 0', function() {19 assert.equal(0, mockCppSuccessful());20 });21});22describe('mockCppFailure', function() {23 it('should return 1', function() {24 assert.equal(1, mockCppFailure());25 });26});27var mockCppSuccessful = require('../build/Release/mockCpp.node').mockCppSuccessful;28var mockCppFailure = require('../build/Release/mockCpp.node').mockCppFailure;29var assert = require('assert');30describe('mockCppSuccessful', function() {31 it('should return 0', function() {32 assert.equal(0, mockCppSuccessful());33 });34});35describe('mockCppFailure', function() {36 it('should return 1', function() {37 assert.equal(1, mockCppFailure());38 });39});
Using AI Code Generation
1var root = require('root');2var result = root.mockCppSuccessful();3var root = require('root');4var result = root.mockCppFailure();5var root = require('root');6var result = root.mockCppException();7var root = require('root');8var result = root.mockCppWithParams(10, 20);9var root = require('root');10var result = root.mockCppWithObject({a: 10, b: 20});11var root = require('root');12var result = root.mockCppWithArray([10, 20]);13var root = require('root');14var result = root.mockCppWithArrayAndObject([10, 20], {a: 10, b: 20});15var root = require('root');16var result = root.mockCppWithArrayAndObject([10, 20], {a: 10, b: 20});17var root = require('root');18var result = root.mockCppWithArrayAndObject([10, 20], {a: 10, b: 20});19var root = require('root');20var result = root.mockCppWithArrayAndObject([10, 20], {
Using AI Code Generation
1var rootMock = require('./rootMock');2var mockCppSuccessful = rootMock.mockCppSuccessful;3mockCppSuccessful();4var mockCppSuccessful = function() {5 console.log('mockCppSuccessful');6}7exports.mockCppSuccessful = mockCppSuccessful;8var cppMock = function() {9 console.log('cppMock');10}11exports.cppMock = cppMock;12var fs = require('fs');13var sinon = require('sinon');14describe('test', function() {15 it('test', function() {16 var fsMock = {};17 sinon.stub(fsMock, 'readFile');18 fsMock.readFile();19 });20});21var fs = require('fs');22var sinon = require('sinon');23describe('test', function() {24 it('test', function() {25 var fsMock = {};26 sinon.stub(fsMock, 'readFile');27 fsMock.readFile();28 });29});
Using AI Code Generation
1var root = require('./root');2root.mockCppSuccessful();3var cpp = require('./cpp');4exports.mockCppSuccessful = function() {5 cpp.mockCppSuccessful();6}7exports.mockCppSuccessful = function() {8 console.log("Mocking cpp successful");9}
Using AI Code Generation
1var rootModule = require('./rootModule');2var myModule = require('./myModule');3rootModule.mockCppSuccessful();4myModule.mockCppFailed();5var cppModule = require('./cppModule');6var myModule = require('./myModule');7exports.mockCppSuccessful = function() {8 cppModule.mockCppSuccessful();9}10exports.mockCppFailed = function() {11 cppModule.mockCppFailed();12}13var cppModule = require('./cppModule');14exports.mockCppSuccessful = function() {15 cppModule.mockCppSuccessful();16}17exports.mockCppFailed = function() {18 cppModule.mockCppFailed();19}20var cppModule = require('cppModule');21exports.mockCppSuccessful = function() {22 cppModule.mockCppSuccessful();23}24exports.mockCppFailed = function() {25 cppModule.mockCppFailed();26}27using namespace v8;28Handle<Value> MockCppSuccessful(const Arguments& args) {29 HandleScope scope;30 return scope.Close(Boolean::New(true));31}32Handle<Value> MockCppFailed(const Arguments& args) {33 HandleScope scope;34 return scope.Close(Boolean::New(false));35}36void init(Handle<Object> exports) {37 exports->Set(String::NewSymbol("mockCppSuccessful"),38 FunctionTemplate::New(MockCppSuccessful)->GetFunction());39 exports->Set(String::NewSymbol("mockCppFailed"),40 FunctionTemplate::New(MockCppFailed)->GetFunction());41}42NODE_MODULE(cppModule, init)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!