Best JavaScript code snippet using root
ADB.test.js
Source:ADB.test.js
...212 const testRunner = 'aTestRunner';213 it('should spawn instrumentation', async () => {214 const userArgs = [];215 const expectedArgs = ['-s', deviceId, 'shell', 'am', 'instrument', '-w', '-r', testRunner];216 await adb.spawnInstrumentation(deviceId, userArgs, testRunner);217 expect(spawnAndLog).toHaveBeenCalledWith(adbBinPath, expectedArgs, expect.any(Object));218 });219 it('should pass through additional args', async () => {220 const userArgs = ['-mock', '-args'];221 await adb.spawnInstrumentation(deviceId, userArgs, testRunner);222 expect(spawnAndLog).toHaveBeenCalledWith(adbBinPath, expect.arrayContaining([...userArgs, testRunner]), expect.any(Object));223 });224 it('should set detaching=false as spawn-options', async () => {225 const expectedOptions = {226 detached: false,227 };228 const userArgs = [];229 await adb.spawnInstrumentation(deviceId, userArgs, testRunner);230 expect(spawnAndLog).toHaveBeenCalledWith(adbBinPath, expect.any(Array), expectedOptions);231 });232 it('should chain-return the promise from spawn util', async () => {233 const userArgs = [];234 const mockPromise = Promise.resolve('mock');235 spawnAndLog.mockReturnValue(mockPromise);236 const childProcessPromise = adb.spawnInstrumentation(deviceId, userArgs, testRunner);237 expect(childProcessPromise).toEqual(mockPromise);238 });239 });240 it(`listInstrumentation passes the right deviceId`, async () => {241 const deviceId = 'aDeviceId';242 jest.spyOn(adb, 'shell');243 await adb.listInstrumentation(deviceId);244 expect(adb.shell).toBeCalledWith(deviceId, 'pm list instrumentation');245 });246 it(`getInstrumentationRunner parses the correct runner for the package`, async () => {247 const expectedRunner = 'com.example.android.apis/.app.LocalSampleInstrumentation';248 const expectedPackage = 'com.example.android.apis';249 const instrumentationRunnersShellOutput =250 'instrumentation:com.android.emulator.smoketests/android.support.test.runner.AndroidJUnitRunner (target=com.android.emulator.smoketests)\n' +...
Instrumentation.test.js
Source:Instrumentation.test.js
1// @ts-nocheck2describe('Instrumentation', () => {3 const deviceId = 'mock-device-id';4 const bundleId = 'mock-bundle-id';5 let childProcessUtils;6 let adb;7 let instrumentationArgs;8 let logger;9 beforeEach(() => {10 const ADB = jest.genMockFromModule('../exec/ADB');11 adb = new ADB();12 jest.mock('../../../../../utils/childProcess');13 childProcessUtils = require('../../../../../utils/childProcess');14 jest.mock('./instrumentationArgs');15 instrumentationArgs = require('./instrumentationArgs');16 instrumentationArgs.prepareInstrumentationArgs.mockReturnValue({ args: [], usedReservedArgs: [] });17 logger = {18 warn: jest.fn(),19 };20 });21 let childProcess;22 let instrumentationProcess;23 let logListenCallback;24 let userTerminationCallback;25 let uut;26 beforeEach(() => {27 childProcess = {28 on: jest.fn(),29 stdout: {30 setEncoding: jest.fn(),31 on: jest.fn(),32 },33 };34 instrumentationProcess = {35 childProcess,36 };37 adb.spawnInstrumentation.mockReturnValue(instrumentationProcess);38 logListenCallback = jest.fn();39 userTerminationCallback = jest.fn();40 const Instrumentation = require('./Instrumentation');41 uut = new Instrumentation(adb, logger, userTerminationCallback, logListenCallback);42 });43 it('should spawn Android instrumentation with device ID', async () => {44 await uut.launch(deviceId, bundleId, []);45 expect(adb.spawnInstrumentation).toHaveBeenCalledWith(deviceId, expect.anything(), undefined);46 });47 it('should spawn instrumentation with test runner', async () => {48 const testRunner = 'mock-android-test-runner-name';49 adb.getInstrumentationRunner.mockResolvedValue(testRunner);50 await uut.launch(deviceId, bundleId, []);51 expect(adb.spawnInstrumentation).toHaveBeenCalledWith(expect.anything(), expect.anything(), testRunner);52 expect(adb.getInstrumentationRunner).toHaveBeenCalledWith(deviceId, bundleId);53 });54 describe('spawning launch-arguments processing', () => {55 it('should prepare user launch-arguments', async () => {56 instrumentationArgs.prepareInstrumentationArgs.mockReturnValue({ args: [], usedReservedArgs: [] });57 const userArgs = {58 arg1: 'value1',59 };60 await uut.launch(deviceId, bundleId, userArgs);61 expect(instrumentationArgs.prepareInstrumentationArgs).toHaveBeenCalledWith(userArgs);62 });63 it('should prepare forced debug=false launch argument', async () => {64 instrumentationArgs.prepareInstrumentationArgs.mockReturnValue({ args: [], usedReservedArgs: [] });65 await uut.launch(deviceId, bundleId, { });66 expect(instrumentationArgs.prepareInstrumentationArgs).toHaveBeenCalledWith({ debug: false });67 });68 it('should spawn instrumentation with processed user launch-arguments', async () => {69 const mockedPreparedUserArgs = ['mocked', 'prepared-args'];70 const mockedPreparedDebugArg = ['debug', 'mocked'];71 instrumentationArgs.prepareInstrumentationArgs72 .mockReturnValueOnce({ args: mockedPreparedUserArgs, usedReservedArgs: [] })73 .mockReturnValueOnce({ args: mockedPreparedDebugArg, usedReservedArgs: [] });74 await uut.launch(deviceId, bundleId, {});75 expect(adb.spawnInstrumentation).toHaveBeenCalledWith(expect.anything(), [...mockedPreparedUserArgs, ...mockedPreparedDebugArg], undefined);76 });77 it('should log reserved instrumentation args usage if used in user args', async () => {78 const mockedPreparedUserArgs = ['mocked', 'prepared-args'];79 const mockedPreparedDebugArg = ['debug', 'mocked'];80 const usedReservedArgs = ['aaa', 'zzz'];81 instrumentationArgs.prepareInstrumentationArgs82 .mockReturnValueOnce({ args: mockedPreparedUserArgs, usedReservedArgs })83 .mockReturnValueOnce({ args: mockedPreparedDebugArg, usedReservedArgs: ['shouldnt', 'care'] });84 await uut.launch(deviceId, bundleId, {});85 expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Arguments [aaa,zzz] were passed in as launchArgs to device.launchApp()'));86 expect(logger.warn).toHaveBeenCalledTimes(1);87 });88 it('should NOT log reserved instrumentation args usage if none used by user', async () => {89 const mockedPreparedUserArgs = ['mocked', 'prepared-args'];90 const mockedPreparedDebugArg = ['debug', 'mocked'];91 instrumentationArgs.prepareInstrumentationArgs92 .mockReturnValueOnce({ args: mockedPreparedUserArgs, usedReservedArgs: [] })93 .mockReturnValueOnce({ args: mockedPreparedDebugArg, usedReservedArgs: ['shouldnt', 'care'] });94 await uut.launch(deviceId, bundleId, {});95 expect(logger.warn).not.toHaveBeenCalled();96 });97 });98 describe('instrumentation child-process unplanned termination', () => {99 it('should terminate instrumentation', async () => {100 await uut.launch(deviceId, bundleId, []);101 expect(childProcess.on).toHaveBeenCalledWith('close', expect.any(Function));102 await invokeTerminationCallback();103 expect(childProcessUtils.interruptProcess).toHaveBeenCalledWith(instrumentationProcess);104 });105 it('should fail if termination callback breaks', async () => {106 childProcessUtils.interruptProcess.mockRejectedValue(new Error());107 await uut.launch(deviceId, bundleId, []);108 try {109 await invokeTerminationCallback();110 fail();111 } catch(error) {}112 });113 it('should not terminate if dispatched twice', async () => {114 await uut.launch(deviceId, bundleId, []);115 await invokeTerminationCallback();116 await invokeTerminationCallback();117 expect(childProcessUtils.interruptProcess).toHaveBeenCalledTimes(1);118 });119 it('should exec user\'s top-level custom termination callback', async () => {120 await uut.launch(deviceId, bundleId, []);121 await invokeTerminationCallback();122 expect(userTerminationCallback).toHaveBeenCalled();123 });124 });125 describe('user-initiated termination', () => {126 it('should terminate upon a termination API call', async () => {127 await uut.launch(deviceId, bundleId, []);128 await uut.terminate();129 expect(childProcessUtils.interruptProcess).toHaveBeenCalledWith(instrumentationProcess);130 });131 it('should break if process interruption fails', async () => {132 childProcessUtils.interruptProcess.mockRejectedValue(new Error());133 await uut.launch(deviceId, bundleId, []);134 try {135 await uut.terminate();136 fail();137 } catch(error) {}138 });139 it('should not terminate if not running', async () => {140 await uut.terminate();141 expect(childProcessUtils.interruptProcess).not.toHaveBeenCalled();142 });143 it('should not terminate if already terminated', async () => {144 await uut.launch(deviceId, bundleId, []);145 await uut.terminate();146 await uut.terminate();147 expect(childProcessUtils.interruptProcess).toHaveBeenCalledTimes(1);148 });149 it('should NOT exec user\'s top-level custom termination callback', async () => {150 await uut.launch(deviceId, bundleId, []);151 await uut.terminate();152 expect(userTerminationCallback).not.toHaveBeenCalled();153 });154 });155 describe('instrumentation run-status querying', () => {156 it('should be true when running', async () => {157 await uut.launch(deviceId, bundleId, []);158 expect(uut.isRunning()).toEqual(true);159 });160 it('should false if terminated', async () => {161 await uut.launch(deviceId, bundleId, []);162 await uut.terminate();163 expect(uut.isRunning()).toEqual(false);164 });165 });166 describe('instrumentation output-log tapping', () => {167 it('should be made possible to tap into, with utf-8 encoding', async () => {168 await uut.launch(deviceId, bundleId, []);169 expect(childProcess.stdout.on).toHaveBeenCalledWith('data', expect.any(Function));170 expect(childProcess.stdout.setEncoding).toHaveBeenCalledWith('utf8');171 await invokeDataCallbackWith('mock data');172 expect(logListenCallback).toHaveBeenCalledWith('mock data');173 });174 });175 it('should work with no user callbacks registration', async () => {176 const Instrumentation = require('./Instrumentation');177 uut = new Instrumentation(adb, logger, undefined, undefined);178 await uut.launch(deviceId, bundleId, []);179 await uut.terminate();180 });181 const extractDataCallback = () => childProcess.stdout.on.mock.calls[0][1];182 const invokeDataCallbackWith = async (data) => {183 const fn = extractDataCallback();184 await fn(data);185 };186 const extractTerminationCallback = () => childProcess.on.mock.calls[0][1];187 const invokeTerminationCallback = async () => {188 const terminationFn = extractTerminationCallback();189 await terminationFn();190 };...
Instrumentation.js
Source:Instrumentation.js
...13 }14 async launch(deviceId, bundleId, userLaunchArgs) {15 const spawnArgs = this._getSpawnArgs(userLaunchArgs);16 const testRunner = await this.adb.getInstrumentationRunner(deviceId, bundleId);17 this.instrumentationProcess = this.adb.spawnInstrumentation(deviceId, spawnArgs, testRunner);18 this.instrumentationProcess.childProcess.stdout.setEncoding('utf8');19 this.instrumentationProcess.childProcess.stdout.on('data', this._onLogData);20 this.instrumentationProcess.childProcess.on('close', this._onTerminated);21 }22 async terminate() {23 if (this.instrumentationProcess) {24 await this._killProcess();25 }26 }27 isRunning() {28 return !!this.instrumentationProcess;29 }30 _getSpawnArgs(userLaunchArgs) {31 const launchArgs = prepareInstrumentationArgs(userLaunchArgs);...
Using AI Code Generation
1var root = require('./root.js');2root.spawnInstrumentation();3var spawn = require('child_process').spawn;4var child = spawn('node', ['test.js']);5child.stdout.on('data', function(data) {6 console.log(data.toString());7});8child.stderr.on('data', function(data) {9 console.log(data.toString());10});11child.on('close', function(code) {12 console.log('child process exited with code ' + code);13});14exports.spawnInstrumentation = function() {15 var spawn = require('child_process').spawn;16 var child = spawn('node', ['instrument.js']);17 child.stdout.on('data', function(data) {18 console.log(data.toString());19 });20 child.stderr.on('data', function(data) {21 console.log(data.toString());22 });23 child.on('close', function(code) {24 console.log('child process exited with code ' + code);25 });26};27var instrument = require('instrument');28var child = instrument();29child.stdout.on('data', function(data) {30 console.log(data.toString());31});32child.stderr.on('data', function(data) {33 console.log(data.toString());34});35child.on('close', function(code) {36 console.log('child process exited with code ' + code);37});38child.on('message', function(msg) {39 console.log(msg);40});41var instrument = require('instrument');42var child = instrument();43child.stdout.on('data', function(data) {44 console.log(data.toString());45});46child.stderr.on('data', function(data) {47 console.log(data.toString());48});49child.on('close', function(code) {50 console.log('child process exited with code ' + code);51});52child.on('message', function(msg) {53 console.log(msg);54});55var instrument = require('instrument');56var child = instrument();57child.stdout.on('data', function(data) {58 console.log(data.toString());59});60child.stderr.on('data', function(data) {61 console.log(data.toString());62});63child.on('close', function(code) {64 console.log('child process exited with code ' + code);65});66child.on('message', function(msg)
Using AI Code Generation
1var child = root.spawnInstrumentation('test.js');2var child = root.spawnInstrumentation('test.js');3var child = root.spawnInstrumentation('test.js');4var child = root.spawnInstrumentation('test.js');5var child = root.spawnInstrumentation('test.js');6var child = root.spawnInstrumentation('test.js');7var child = root.spawnInstrumentation('test.js');8var child = root.spawnInstrumentation('test.js');9var child = root.spawnInstrumentation('test.js');10var child = root.spawnInstrumentation('test.js');11var child = root.spawnInstrumentation('test.js');
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!!