Best JavaScript code snippet using stryker-parent
spawn-child-process-stream.ts
Source:spawn-child-process-stream.ts
1import { EventEmitter } from 'events'2import { mockRequire } from '@mock/require'3import { createSpy, getSpyCalls } from 'spyfn'4import test from 'tape'5import { unchunkString } from 'unchunk'6import { mockChildProcess } from './helpers'7test('spown: spawnChildProcessStream + ok + stdout + stderr', async (t) => {8 const childProcess = mockChildProcess({9 stdout: 'output',10 stderr: 'oops',11 exitCode: null,12 })13 childProcess.kill = () => {}14 const unmockRequire = mockRequire('../src', {15 'cross-spawn': {16 default: () => childProcess,17 },18 'signal-exit': {19 default: () => {},20 },21 })22 const { spawnChildProcessStream } = await import('../src')23 const result = spawnChildProcessStream('foo')24 const [stdout, stderr] = await Promise.all([25 result.stdout !== null ? unchunkString(result.stdout) : null,26 result.stderr !== null ? unchunkString(result.stderr) : null,27 ])28 t.equal(29 stdout,30 'output',31 'should provide stdout'32 )33 t.equal(34 stderr,35 'oops',36 'should provide stderr'37 )38 unmockRequire()39})40test('spown: spawnChildProcessStream + command + default options', async (t) => {41 const emitter = new EventEmitter()42 const spy = createSpy(() => {43 return emitter44 })45 const unmockRequire = mockRequire('../src', {46 'cross-spawn': {47 default: spy,48 },49 'signal-exit': {50 default: () => {},51 },52 })53 const { spawnChildProcessStream } = await import('../src')54 spawnChildProcessStream('foo -a b -b "c d" -c d\\ e -d "e f g"')55 t.deepEqual(56 getSpyCalls(spy),57 [58 [59 'foo',60 ['-a', 'b', '-b', 'c d', '-c', 'd\\ e', '-d', 'e f g'],61 {62 argv0: undefined,63 cwd: undefined,64 env: { ...process.env },65 gid: undefined,66 uid: undefined,67 serialization: undefined,68 stdio: ['pipe', 'pipe', 'pipe', 'ignore'],69 },70 ],71 ],72 'should work'73 )74 unmockRequire()75})76test('spown: spawnChildProcessStream + custom options', async (t) => {77 const emitter = new EventEmitter()78 const spy = createSpy(() => {79 return emitter80 })81 const unmockRequire = mockRequire('../src', {82 'cross-spawn': {83 default: spy,84 },85 'signal-exit': {86 default: () => {},87 },88 })89 const { spawnChildProcessStream } = await import('../src')90 spawnChildProcessStream('foo -a b -b "c d" -c d\\ e -d "e f g"', {91 argv0: 'argv0',92 cwd: 'cwd',93 env: {94 foo: 'bar',95 },96 gid: 123,97 uid: 456,98 serialization: 'advanced',99 stdin: null,100 stdout: null,101 stderr: null,102 shouldCreateIpcChannel: true,103 })104 t.deepEqual(105 getSpyCalls(spy),106 [107 [108 'foo',109 ['-a', 'b', '-b', 'c d', '-c', 'd\\ e', '-d', 'e f g'],110 {111 argv0: 'argv0',112 cwd: 'cwd',113 env: {114 ...process.env,115 foo: 'bar',116 },117 gid: 123,118 uid: 456,119 serialization: 'advanced',120 stdio: ['ignore', 'ignore', 'ignore', 'ipc'],121 },122 ],123 ],124 'should work'125 )126 unmockRequire()127})128test('spown: spawnChildProcessStream + onExitHook + null signal + kill', async (t) => {129 const childProcess = mockChildProcess({130 stdout: null,131 stderr: null,132 exitCode: null,133 })134 let isClosed = false135 let onExitCallback: (signal: number | null) => void136 const killSpy = createSpy(() => {})137 childProcess.kill = killSpy138 childProcess.once = (e: string, callback: (data?: any) => void) => {139 if (!isClosed && e === 'close') {140 isClosed = true141 onExitCallback(null)142 callback(0)143 }144 return childProcess145 }146 const unmockRequire = mockRequire('../src', {147 'cross-spawn': {148 default: () => childProcess,149 },150 'signal-exit': {151 default: (callback: (signal: number | null) => void) => {152 onExitCallback = callback153 },154 },155 })156 const { spawnChildProcessStream } = await import('../src')157 spawnChildProcessStream('foo')158 t.deepEqual(159 getSpyCalls(killSpy),160 [[]],161 'should work'162 )163 unmockRequire()164})165test('spown: spawnChildProcessStream + onExitHook + signal + kill', async (t) => {166 const childProcess = mockChildProcess({167 stdout: null,168 stderr: null,169 exitCode: null,170 })171 let isClosed = false172 let onExitCallback: (signal: number | null) => void173 const killSpy = createSpy(() => {})174 childProcess.kill = killSpy175 childProcess.once = (e: string, callback: (data?: any) => void) => {176 if (!isClosed && e === 'close') {177 isClosed = true178 onExitCallback(42)179 callback(0)180 }181 return childProcess182 }183 const unmockRequire = mockRequire('../src', {184 'cross-spawn': {185 default: () => childProcess,186 },187 'signal-exit': {188 default: (callback: (signal: number | null) => void) => {189 onExitCallback = callback190 },191 },192 })193 const { spawnChildProcessStream } = await import('../src')194 spawnChildProcessStream('foo')195 t.deepEqual(196 getSpyCalls(killSpy),197 [[42]],198 'should work'199 )200 unmockRequire()201})202test('spown: spawnChildProcessTest + onExitHook + multiple processes + kill', async (t) => {203 let onExitCallback: (signal: number | null) => void204 const killSpies = new Set<any>()205 const ChildProcess = () => {206 const childProcess = mockChildProcess({207 stdout: null,208 stderr: null,209 exitCode: null,210 })211 let isClosed = false212 const killSpy = createSpy(() => {})213 killSpies.add(killSpy)214 childProcess.kill = killSpy215 childProcess.once = (e: string, callback: (data?: any) => void) => {216 if (!isClosed && e === 'close') {217 isClosed = true218 onExitCallback(42)219 callback(0)220 }221 return childProcess222 }223 return childProcess224 }225 const unmockRequire = mockRequire('../src', {226 'cross-spawn': {227 default: ChildProcess,228 },229 'signal-exit': {230 default: (callback: (signal: number | null) => void) => {231 onExitCallback = callback232 },233 },234 })235 const { spawnChildProcessStream } = await import('../src')236 spawnChildProcessStream('foo')237 spawnChildProcessStream('bar')238 const result = Array.from(killSpies).map((spy) => getSpyCalls(spy))239 t.deepEqual(240 result,241 [242 [[42]],243 [[42]],244 ],245 'should work'246 )247 unmockRequire()248})249test('spown: spawnChildProcessStream + onExitHook + multiple processes + nothing to kill', async (t) => {250 let onExitCallback: (signal: number | null) => void251 const killSpies = new Set<any>()252 const ChildProcess = () => {253 const childProcess = mockChildProcess({254 stdout: null,255 stderr: null,256 exitCode: null,257 })258 let isClosed = false259 const killSpy = createSpy(() => {})260 killSpies.add(killSpy)261 childProcess.kill = killSpy262 childProcess.once = (e: string, callback: (data?: any) => void) => {263 if (!isClosed && e === 'close') {264 isClosed = true265 callback(0)266 onExitCallback(42)267 }268 return childProcess269 }270 return childProcess271 }272 const unmockRequire = mockRequire('../src', {273 'cross-spawn': {274 default: ChildProcess,275 },276 'signal-exit': {277 default: (callback: (signal: number | null) => void) => {278 onExitCallback = callback279 },280 },281 })282 const { spawnChildProcessStream } = await import('../src')283 spawnChildProcessStream('foo')284 spawnChildProcessStream('bar')285 const result = Array.from(killSpies).map((spy) => getSpyCalls(spy))286 t.deepEqual(287 result,288 [289 [],290 [],291 ],292 'should work'293 )294 unmockRequire()...
scheduler.js
Source:scheduler.js
1/*2 * test/scheduler.js3 *4 * Copyright (C) 2009-13 by RStudio, Inc.5 *6 * This program is licensed to you under the terms of version 3 of the7 * GNU Affero General Public License. This program is distributed WITHOUT8 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,9 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the10 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.11 *12 */13var AppSpec = require('../lib/worker/app-spec.js');14var sinon = require('sinon');15var Q = require('q');16var _ = require('underscore');17var SimpleEventBus = require('../lib/events/simple-event-bus');18var rewire = require("rewire");19var sinon = require('sinon');20// need the static functions21var should = require('should');22var Scheduler = rewire('../lib/scheduler/scheduler.js');23var exitPromise = Q.defer();24var killSpy = sinon.spy();25Scheduler.__set__("app_worker", {launchWorker_p: function() {26 return Q({27 kill: killSpy,28 getExit_p: function(){ return exitPromise.promise; },29 isRunning: function(){ return true }30 });31}});32var appSpec = new AppSpec("/var/shiny-www/01_hello/", "jeff", "", "/tmp", {})33var scheduler;34var clock = sinon.useFakeTimers();35describe('Scheduler', function(){36 beforeEach(function(){37 scheduler = new Scheduler(new SimpleEventBus(), appSpec.getKey());38 scheduler.setTransport({alloc_p: function(){39 return Q({40 getLogFileSuffix: function(){ return "" },41 ToString: function(){ return "" },42 toString: function(){ return "" },43 connect_p: function(){ return Q(true) }44 })45 }});46 killSpy.resetHistory();47 exitPromise = Q.defer();48 });49 describe('#spawnWorker', function(){50 it('properly stores provided data.', function(done){51 //check that we're starting off with no workers.52 Object.keys(scheduler.$workers).should.be.empty;53 //request a worker54 scheduler.spawnWorker(appSpec, {a:5, b:"test"}).getAppWorkerHandle_p()55 .then(function(wh){56 //check that exactly one worker has been created57 Object.keys(scheduler.$workers).should.have.length(1);58 //check that the worker has the necessary fields created.59 var worker = scheduler.$workers[Object.keys(scheduler.$workers)[0]];60 worker.should.have.keys('data', 'promise');61 worker.data.should.have.keys('a', 'b', 'sockConn', 'httpConn');62 }) 63 .then(done, done).done();64 }),65 it('properly handles acquire and release.', function() {66 //request a worker67 let workerEntry = scheduler.spawnWorker(appSpec, {});68 var worker = scheduler.$workers[Object.keys(scheduler.$workers)[0]];69 worker.data.httpConn.should.equal(0);70 worker.data.sockConn.should.equal(0);71 workerEntry.acquire('http');72 73 worker.data.httpConn.should.equal(1);74 worker.data.sockConn.should.equal(0);75 workerEntry.acquire('sock');76 77 worker.data.httpConn.should.equal(1);78 worker.data.sockConn.should.equal(1);79 workerEntry.release('http');80 81 worker.data.httpConn.should.equal(0);82 worker.data.sockConn.should.equal(1);83 workerEntry.release('sock');84 85 worker.data.httpConn.should.equal(0);86 worker.data.sockConn.should.equal(0); 87 }),88 it('sets timer after last connection.', function(done) {89 //request a worker90 let workerEntry = scheduler.spawnWorker(appSpec, {});91 workerEntry.getAppWorkerHandle_p()92 .then(function(wh){93 // TODO: clean up 94 // The old tests all have pending kill timers which the spy will95 // capture if we just run this test now. Advance the clock to get96 // those kills out of the way then reset the spy to get an accurate97 // count from JUST this test.98 clock.tick(5500);99 killSpy.resetHistory();100 //check that the worker has the necessary fields created.101 var worker = scheduler.$workers[Object.keys(scheduler.$workers)[0]];102 should.exist(worker);103 104 // make a connection so there should be no timer.105 workerEntry.acquire('sock');106 // release the only connection which should trigger the timer107 workerEntry.release('sock');108 // Advance time far enough that the process109 // should have been killed110 clock.tick(5500);111 killSpy.callCount.should.equal(1);112 // Mark the process as killed113 exitPromise.resolve(true);114 return exitPromise.promise.then(function(){115 Object.keys(scheduler.$workers).should.have.length(0);116 })117 }) 118 .then(done, done).done();119 })120 })121 after(function(){122 clock.restore();123 });...
cluster_manager_spec.js
Source:cluster_manager_spec.js
1'use strict';2var os = require('os');3var chai = require('chai');4var expect = chai.expect;5var sinon = require('sinon');6var sinonChai = require('sinon-chai');7chai.use(sinonChai);8var rewire = require('rewire');9var ClusterManager = rewire('../../lib/cluster_manager');10describe('ClusterManager', function () {11 beforeEach(function () {12 var onMessageSpy = this.onMessageSpy = sinon.spy();13 var killSpy = this.killSpy = sinon.spy();14 var forkSpy = this.forkSpy = sinon.spy(function () {15 return {16 on: onMessageSpy,17 kill: killSpy18 };19 });20 // jscs:disable21 ClusterManager.__set__('cluster', {22 fork: forkSpy23 });24 // jscs:enable25 this.subject = new ClusterManager();26 });27 describe('#initialization', function () {28 it('should have an empty array of workers', function () {29 expect(this.subject.workers).to.be.instanceOf(Array);30 expect(this.subject.workers.length).to.equal(0);31 });32 describe('workersCount', function () {33 it('should equal the number of cores by default', function () {34 expect(this.subject.workersCount).to.equal(os.cpus().length);35 });36 it('should accept it as constructor arg', function () {37 expect(new ClusterManager(111).workersCount).to.equal(111);38 });39 });40 });41 describe('#fork', function () {42 beforeEach(function () {43 this.subject.fork();44 });45 it('should call cluster.fork workersCount times', function () {46 expect(this.forkSpy).to.have.callCount(this.subject.workersCount);47 });48 it('should add an on message listener', function () {49 expect(this.onMessageSpy).to.have.callCount(this.subject.workersCount);50 });51 it('should add the workers to its workers array', function () {52 expect(this.subject.workers.length).to.equal(this.subject.workersCount);53 });54 });55 describe('#onMessage', function () {56 it('calls restart if the message is a restart command', function () {57 var restartSpy = sinon.spy(this.subject, 'restart');58 this.subject.onMessage({command: 'restart'});59 expect(restartSpy).to.have.callCount(1);60 });61 });62 describe('#removeWorker', function () {63 beforeEach(function () {64 this.worker = {};65 this.subject.workers.push(this.worker);66 });67 it('removes a worker from its list', function () {68 this.subject.removeWorker(this.worker);69 expect(this.subject.workers.length).to.equal(0);70 });71 });72 describe('#restart', function () {73 it('calls kill and fork', function () {74 var killSpy = sinon.spy(this.subject, 'kill');75 var restartSpy = sinon.spy(this.subject, 'fork');76 this.subject.restart();77 expect(killSpy).to.have.callCount(1);78 expect(restartSpy).to.have.callCount(1);79 });80 });81 describe('#kill', function () {82 beforeEach(function () {83 this.subject.fork();84 });85 it('should kill and remove all workers', function () {86 this.subject.kill();87 expect(this.killSpy).to.have.callCount(this.subject.workersCount);88 expect(this.subject.workers.length).to.equal(0);89 });90 });...
Using AI Code Generation
1const killSpy = require('stryker-parent').killSpy;2const killSpy = require('stryker-parent').killSpy;3const killSpy = require('stryker-parent').killSpy;4const killSpy = require('stryker-parent').killSpy;5const killSpy = require('stryker-parent').killSpy;6const killSpy = require('stryker-parent').killSpy;7const killSpy = require('stryker-parent').killSpy;8const killSpy = require('stryker-parent').killSpy;9const killSpy = require('stryker-parent').killSpy;10const killSpy = require('stryker-parent').killSpy;11const killSpy = require('stryker-parent').killSpy;12const killSpy = require('stryker-parent').killSpy;13const killSpy = require('stryker-parent').killSpy;14const killSpy = require('stryker-parent').killSpy;15const killSpy = require('stryker-parent').killSpy;16const killSpy = require('stryker-parent').killSpy;17const killSpy = require('stryker-parent').killSpy;18const killSpy = require('stryker-parent').killSpy;
Using AI Code Generation
1module.exports = function (config) {2 config.set({3 mochaOptions: {4 }5 });6};7{8 "scripts": {9 },10 "devDependencies": {11 },12 "dependencies": {13 }14}
Using AI Code Generation
1conki killSpy = window.__stllSpy2__();py;2it('should kill the mutant', () => {3 killSpy();4});5module.exorts = function(config) {6 config.set({7 karma: {8 config: {9 { pattern: 'src/**/*.js', mutated: true, included: false },10 { pattern: 'src/**/*.spec.js', included: false },11 }12 }13 });14};15module.exports = function(config) {16 config.set({17 { pattern: 'src/**/*.js', mutated: true, included: false },18 { pattern: 'src/**/*.spec.js', included: false },19 });20};21module.exports = function(config) {22 config.set({23 { pattern: 'src/**/*.js', mutated: true, included: false },24 { pattern: 'src/**/*.spec.js', included: false },25 });26};27module.exports = function(config) {28 config.set({29 { pattern: 'src/**/*.js', mutated: true, included: false },30 { pattern: 'src/**/*.spec.js', included: false },31 });32};33module.exports = functionconfig) {34 config.set({35 { pattern: 'src/**/*.js', mutated: true, included: false },36 { pattern: 'src/**/*.spec.js', included: false },37 };38}
Using AI Code Generation
1const killSpy = window.__stryker2__.killSpy;2it('should kill the mutant', () => {3 killSpy();4});5module.exports = function(config) {6 config.set({7 karma: {8 config: {9 { pattern: 'src/**/*.js', mutated: true, included: false },10 { pattern: 'src/**/*.spec.js', included: false },11 }12 }13 });14};15module.exports = function(config) {16 config.set({17 { pattern: 'src/**/*.js', mutated: true, included: false },18 { pattern: 'src/**/*.spec.js', included: false },19 });20};21module.exports = function(config) {22 config.set({23 { pattern: 'src/**/*.js', mutated: true, included: false },24 { pattern: 'src/**/*.spec.js', included: false },25 });26};27module.exports = function(config) {28 config.set({29 { pattern: 'src/**/*.js', mutated: true, included: false },30 { pattern: 'src/**/*.spec.js', included: false },31 });32};33module.exports = function(config) {34 config.set({35 { pattern: 'src/**/*.js', mutated: true, included: false },36 { pattern: 'src/**/*.spec.js', included: false },37 });38};
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!!