Best JavaScript code snippet using stryker-parent
packageModules.test.js
Source:packageModules.test.js
1'use strict';2const BbPromise = require('bluebird');3const _ = require('lodash');4const chai = require('chai');5const path = require('path');6const sinon = require('sinon');7const mockery = require('mockery');8const Serverless = require('serverless');9const Configuration = require('../lib/Configuration');10// Mocks11const fsMockFactory = require('./mocks/fs.mock');12const globMockFactory = require('./mocks/glob.mock');13const bestzipMockFactory = require('./mocks/bestzip.mock');14chai.use(require('chai-as-promised'));15chai.use(require('sinon-chai'));16const expect = chai.expect;17describe('packageModules', () => {18 let sandbox;19 let baseModule;20 let serverless;21 let module;22 // Mocks23 let fsMock;24 let globMock;25 let bestzipMock;26 // Serverless stubs27 let writeFileDirStub;28 let getAllFunctionsStub;29 let getFunctionStub;30 let getServiceObjectStub;31 let getVersionStub;32 before(() => {33 sandbox = sinon.createSandbox();34 sandbox.usingPromise(BbPromise);35 fsMock = fsMockFactory.create(sandbox);36 bestzipMock = bestzipMockFactory.create(sandbox);37 globMock = globMockFactory.create(sandbox);38 mockery.enable({ warnOnUnregistered: false });39 mockery.registerMock('bestzip', bestzipMock);40 mockery.registerMock('fs', fsMock);41 mockery.registerMock('glob', globMock);42 baseModule = require('../lib/packageModules');43 Object.freeze(baseModule);44 });45 after(() => {46 mockery.disable();47 mockery.deregisterAll();48 });49 beforeEach(() => {50 serverless = new Serverless();51 serverless.cli = {52 log: sandbox.stub(),53 consoleLog: sandbox.stub()54 };55 writeFileDirStub = sandbox.stub(serverless.utils, 'writeFileDir');56 getAllFunctionsStub = sandbox.stub(serverless.service, 'getAllFunctions');57 getFunctionStub = sandbox.stub(serverless.service, 'getFunction');58 getServiceObjectStub = sandbox.stub(serverless.service, 'getServiceObject');59 getVersionStub = sandbox.stub(serverless, 'getVersion');60 module = _.assign(61 {62 serverless,63 options: {64 verbose: true65 },66 webpackOutputPath: '.webpack',67 configuration: new Configuration()68 },69 baseModule70 );71 });72 afterEach(() => {73 // Reset all counters and restore all stubbed functions74 sandbox.reset();75 sandbox.restore();76 });77 describe('packageModules()', () => {78 it('should do nothing if no compile stats are available', () => {79 module.compileStats = { stats: [] };80 return expect(module.packageModules()).to.be.fulfilled.then(() =>81 BbPromise.all([82 expect(bestzipMock.nativeZip).to.not.have.been.called,83 expect(writeFileDirStub).to.not.have.been.called,84 expect(fsMock.createWriteStream).to.not.have.been.called,85 expect(globMock.sync).to.not.have.been.called86 ])87 );88 });89 it('should do nothing if skipCompile is true', () => {90 module.skipCompile = true;91 return expect(module.packageModules()).to.be.fulfilled.then(() =>92 BbPromise.all([93 expect(bestzipMock.nativeZip).to.not.have.been.called,94 expect(writeFileDirStub).to.not.have.been.called,95 expect(fsMock.createWriteStream).to.not.have.been.called,96 expect(globMock.sync).to.not.have.been.called97 ])98 );99 });100 describe('with service packaging', () => {101 beforeEach(() => {102 // Setup behavior for service packaging103 _.unset(module, 'entryFunctions');104 _.set(serverless.service, 'package.individually', false);105 });106 it('should package', () => {107 // Test data108 const stats = {109 stats: [110 {111 outputPath: '/my/Service/Path/.webpack/service'112 }113 ]114 };115 const files = [ 'README.md', 'src/handler1.js', 'src/handler1.js.map', 'src/handler2.js', 'src/handler2.js.map' ];116 const allFunctions = [ 'func1', 'func2' ];117 const func1 = {118 handler: 'src/handler1',119 events: []120 };121 const func2 = {122 handler: 'src/handler2',123 events: []124 };125 // Serverless behavior126 getVersionStub.returns('1.18.0');127 getServiceObjectStub.returns({128 name: 'test-service'129 });130 getAllFunctionsStub.returns(allFunctions);131 getFunctionStub.withArgs('func1').returns(func1);132 getFunctionStub.withArgs('func2').returns(func2);133 // Mock behavior134 globMock.sync.returns(files);135 fsMock._streamMock.on.withArgs('open').yields();136 fsMock._streamMock.on.withArgs('close').yields();137 fsMock._statMock.isDirectory.returns(false);138 module.compileStats = stats;139 return expect(module.packageModules()).to.be.fulfilled.then(() => BbPromise.all([]));140 });141 describe('with the Google provider', () => {142 let oldProviderName;143 beforeEach(() => {144 oldProviderName = serverless.service.provider.name;145 // Imitate Google provider146 serverless.service.provider.name = 'google';147 });148 afterEach(() => {149 if (oldProviderName) {150 serverless.service.provider.name = oldProviderName;151 } else {152 _.unset(serverless.service.provider, 'name');153 }154 });155 it('should set the service artifact path', () => {156 // Test data157 const stats = {158 stats: [159 {160 outputPath: '/my/Service/Path/.webpack/service'161 }162 ]163 };164 const files = [ 'README.md', 'index.js' ];165 const allFunctions = [ 'func1', 'func2' ];166 const func1 = {167 handler: 'handler1',168 events: []169 };170 const func2 = {171 handler: 'handler2',172 events: []173 };174 getVersionStub.returns('1.18.0');175 getServiceObjectStub.returns({176 name: 'test-service'177 });178 getAllFunctionsStub.returns(allFunctions);179 getFunctionStub.withArgs('func1').returns(func1);180 getFunctionStub.withArgs('func2').returns(func2);181 // Mock behavior182 globMock.sync.returns(files);183 fsMock._streamMock.on.withArgs('open').yields();184 fsMock._streamMock.on.withArgs('close').yields();185 fsMock._statMock.isDirectory.returns(false);186 module.compileStats = stats;187 return expect(module.packageModules()).to.be.fulfilled;188 });189 });190 it('should set the function artifact depending on the serverless version', () => {191 // Test data192 const stats = {193 stats: [194 {195 outputPath: '/my/Service/Path/.webpack/service'196 }197 ]198 };199 const files = [ 'README.md', 'src/handler1.js', 'src/handler1.js.map', 'src/handler2.js', 'src/handler2.js.map' ];200 const allFunctions = [ 'func1', 'func2' ];201 const func1 = {202 handler: 'src/handler1',203 events: []204 };205 const func2 = {206 handler: 'src/handler2',207 events: []208 };209 // Serverless behavior210 getServiceObjectStub.returns({211 name: 'test-service'212 });213 getAllFunctionsStub.returns(allFunctions);214 getFunctionStub.withArgs('func1').returns(func1);215 getFunctionStub.withArgs('func2').returns(func2);216 // Mock behavior217 globMock.sync.returns(files);218 fsMock._streamMock.on.withArgs('open').yields();219 fsMock._streamMock.on.withArgs('close').yields();220 fsMock._statMock.isDirectory.returns(false);221 module.compileStats = stats;222 return BbPromise.each([ '1.18.1', '2.17.0', '10.15.3' ], version => {223 getVersionStub.returns(version);224 return expect(module.packageModules()).to.be.fulfilled.then(() => BbPromise.all([]));225 }).then(() =>226 BbPromise.each([ '1.17.0', '1.16.0-alpha', '1.15.3' ], version => {227 getVersionStub.returns(version);228 return expect(module.packageModules()).to.be.fulfilled.then(() => BbPromise.all([]));229 })230 );231 });232 it('should reject if no files are found', () => {233 // Test data234 const stats = {235 stats: [236 {237 outputPath: '/my/Service/Path/.webpack/service'238 }239 ]240 };241 const files = [];242 const allFunctions = [ 'func1', 'func2' ];243 const func1 = {244 handler: 'src/handler1',245 events: []246 };247 const func2 = {248 handler: 'src/handler2',249 events: []250 };251 // Serverless behavior252 getVersionStub.returns('1.18.0');253 getServiceObjectStub.returns({254 name: 'test-service'255 });256 getAllFunctionsStub.returns(allFunctions);257 getFunctionStub.withArgs('func1').returns(func1);258 getFunctionStub.withArgs('func2').returns(func2);259 // Mock behavior260 globMock.sync.returns(files);261 fsMock._streamMock.on.withArgs('open').yields();262 fsMock._streamMock.on.withArgs('close').yields();263 fsMock._statMock.isDirectory.returns(false);264 module.compileStats = stats;265 return expect(module.packageModules()).to.be.rejectedWith('Packaging: No files found');266 });267 it('should reject if no files are found because all files are excluded using regex', () => {268 module.configuration = new Configuration({269 webpack: {270 excludeRegex: '.*'271 }272 });273 // Test data274 const stats = {275 stats: [276 {277 outputPath: '/my/Service/Path/.webpack/service'278 }279 ]280 };281 const files = [ 'README.md', 'src/handler1.js', 'src/handler1.js.map', 'src/handler2.js', 'src/handler2.js.map' ];282 const allFunctions = [ 'func1', 'func2' ];283 const func1 = {284 handler: 'src/handler1',285 events: []286 };287 const func2 = {288 handler: 'src/handler2',289 events: []290 };291 // Serverless behavior292 getVersionStub.returns('1.18.0');293 getServiceObjectStub.returns({294 name: 'test-service'295 });296 getAllFunctionsStub.returns(allFunctions);297 getFunctionStub.withArgs('func1').returns(func1);298 getFunctionStub.withArgs('func2').returns(func2);299 // Mock behavior300 globMock.sync.returns(files);301 fsMock._streamMock.on.withArgs('open').yields();302 fsMock._streamMock.on.withArgs('close').yields();303 fsMock._statMock.isDirectory.returns(false);304 module.compileStats = stats;305 return expect(module.packageModules()).to.be.rejectedWith('Packaging: No files found');306 });307 it('should reject only .md files without verbose log', () => {308 module.options.verbose = false;309 module.configuration = new Configuration({310 webpack: {311 excludeRegex: '.md$'312 }313 });314 // Test data315 const stats = {316 stats: [317 {318 outputPath: '/my/Service/Path/.webpack/service'319 }320 ]321 };322 const files = [ 'README.md', 'src/handler1.js', 'src/handler1.js.map', 'src/handler2.js', 'src/handler2.js.map' ];323 const allFunctions = [ 'func1', 'func2' ];324 const func1 = {325 handler: 'src/handler1',326 events: []327 };328 const func2 = {329 handler: 'src/handler2',330 events: []331 };332 // Serverless behavior333 getVersionStub.returns('1.18.0');334 getServiceObjectStub.returns({335 name: 'test-service'336 });337 getAllFunctionsStub.returns(allFunctions);338 getFunctionStub.withArgs('func1').returns(func1);339 getFunctionStub.withArgs('func2').returns(func2);340 // Mock behavior341 globMock.sync.returns(files);342 fsMock._streamMock.on.withArgs('open').yields();343 fsMock._streamMock.on.withArgs('close').yields();344 fsMock._statMock.isDirectory.returns(false);345 module.compileStats = stats;346 return expect(module.packageModules()).to.be.fulfilled;347 });348 });349 describe('with individual packaging', () => {350 // Test data351 const stats = {352 stats: [353 {354 outputPath: '/my/Service/Path/.webpack/func1'355 },356 {357 outputPath: '/my/Service/Path/.webpack/func2'358 }359 ]360 };361 const files = [ 'README.md', 'src/handler1.js', 'src/handler1.js.map', 'src/handler2.js', 'src/handler2.js.map' ];362 const allFunctions = [ 'func1', 'func2' ];363 const func1 = {364 handler: 'src/handler1',365 events: []366 };367 const func2 = {368 handler: 'src/handler2',369 events: []370 };371 const entryFunctions = [372 {373 handlerFile: 'src/handler1.js',374 funcName: 'func1',375 func: func1376 },377 {378 handlerFile: 'src/handler2.js',379 funcName: 'func2',380 func: func2381 }382 ];383 beforeEach(() => {384 // Setup sandbox and behavior for individual packaging385 _.set(module, 'entryFunctions', entryFunctions);386 _.set(serverless.service.package, 'individually', true);387 });388 it('should package', () => {389 // Serverless behavior390 getVersionStub.returns('1.18.0');391 getServiceObjectStub.returns({392 name: 'test-service'393 });394 getAllFunctionsStub.returns(allFunctions);395 getFunctionStub.withArgs('func1').returns(func1);396 getFunctionStub.withArgs('func2').returns(func2);397 // Mock behavior398 globMock.sync.returns(files);399 fsMock._streamMock.on.withArgs('open').yields();400 fsMock._streamMock.on.withArgs('close').yields();401 fsMock._statMock.isDirectory.returns(false);402 module.compileStats = stats;403 return expect(module.packageModules()).to.be.fulfilled;404 });405 });406 });407 describe('copyExistingArtifacts()', () => {408 const allFunctions = [ 'func1', 'func2', 'funcPython' ];409 const func1 = {410 handler: 'src/handler1',411 events: []412 };413 const func2 = {414 handler: 'src/handler2',415 events: [],416 runtime: 'node14'417 };418 const funcPython = {419 handler: 'src/handlerPython',420 events: [],421 runtime: 'python3.7'422 };423 const entryFunctions = [424 {425 handlerFile: 'src/handler1.js',426 funcName: 'func1',427 func: func1428 },429 {430 handlerFile: 'src/handler2.js',431 funcName: 'func2',432 func: func2433 },434 {435 handlerFile: 'src/handlerPython.js',436 funcName: 'funcPython',437 func: funcPython438 }439 ];440 describe('with service packaging', () => {441 afterEach(() => {442 fsMock.copyFileSync.resetHistory();443 });444 beforeEach(() => {445 _.set(module, 'entryFunctions', entryFunctions);446 _.set(serverless.service.package, 'individually', false);447 getVersionStub.returns('1.18.0');448 getServiceObjectStub.returns({449 name: 'test-service'450 });451 getAllFunctionsStub.returns(allFunctions);452 getFunctionStub.withArgs('func1').returns(func1);453 getFunctionStub.withArgs('func2').returns(func2);454 getFunctionStub.withArgs('funcPython').returns(funcPython);455 });456 it('copies the artifact', () => {457 const expectedArtifactSource = path.join('.webpack', 'test-service.zip');458 const expectedArtifactDestination = path.join('.serverless', 'test-service.zip');459 return expect(module.copyExistingArtifacts()).to.be.fulfilled.then(() =>460 BbPromise.all([461 // Should copy the artifact into .serverless462 expect(fsMock.copyFileSync).callCount(1),463 expect(fsMock.copyFileSync).to.be.calledWith(expectedArtifactSource, expectedArtifactDestination),464 // Should set package artifact for each function to the single artifact465 expect(func1).to.have.a.nested.property('package.artifact').that.equals(expectedArtifactDestination),466 expect(func2).to.have.a.nested.property('package.artifact').that.equals(expectedArtifactDestination)467 ])468 );469 });470 it('should set the function artifact depending on the serverless version', () => {471 // Test data472 const stats = {473 stats: [474 {475 outputPath: '/my/Service/Path/.webpack/service'476 }477 ]478 };479 const files = [ 'README.md', 'src/handler1.js', 'src/handler1.js.map', 'src/handler2.js', 'src/handler2.js.map' ];480 const allFunctions = [ 'func1', 'func2' ];481 const func1 = {482 handler: 'src/handler1',483 events: []484 };485 const func2 = {486 handler: 'src/handler2',487 events: []488 };489 // Serverless behavior490 getServiceObjectStub.returns({491 name: 'test-service'492 });493 getAllFunctionsStub.returns(allFunctions);494 getFunctionStub.withArgs('func1').returns(func1);495 getFunctionStub.withArgs('func2').returns(func2);496 // Mock behavior497 globMock.sync.returns(files);498 fsMock._streamMock.on.withArgs('open').yields();499 fsMock._streamMock.on.withArgs('close').yields();500 fsMock._statMock.isDirectory.returns(false);501 const expectedArtifactPath = path.join('.serverless', 'test-service.zip');502 module.compileStats = stats;503 return BbPromise.each([ '1.18.1', '2.17.0', '10.15.3' ], version => {504 getVersionStub.returns(version);505 return expect(module.copyExistingArtifacts()).to.be.fulfilled.then(() =>506 BbPromise.all([507 expect(func1).to.have.a.nested.property('package.artifact').that.equals(expectedArtifactPath),508 expect(func2).to.have.a.nested.property('package.artifact').that.equals(expectedArtifactPath)509 ])510 );511 }).then(() =>512 BbPromise.each([ '1.17.0', '1.16.0-alpha', '1.15.3' ], version => {513 getVersionStub.returns(version);514 return expect(module.copyExistingArtifacts()).to.be.fulfilled.then(() =>515 BbPromise.all([516 expect(func1).to.have.a.nested.property('artifact').that.equals(expectedArtifactPath),517 expect(func2).to.have.a.nested.property('artifact').that.equals(expectedArtifactPath),518 expect(func1).to.have.a.nested.property('package.disable').that.is.true,519 expect(func2).to.have.a.nested.property('package.disable').that.is.true520 ])521 );522 })523 );524 });525 describe('with the Google provider', () => {526 let oldProviderName;527 beforeEach(() => {528 oldProviderName = serverless.service.provider.name;529 // Imitate Google provider530 serverless.service.provider.name = 'google';531 });532 afterEach(() => {533 if (oldProviderName) {534 serverless.service.provider.name = oldProviderName;535 } else {536 _.unset(serverless.service.provider, 'name');537 }538 });539 it('should set the service artifact path', () => {540 // Test data541 const allFunctions = [ 'func1', 'func2' ];542 const func1 = {543 handler: 'handler1',544 events: []545 };546 const func2 = {547 handler: 'handler2',548 events: []549 };550 getVersionStub.returns('1.18.0');551 getServiceObjectStub.returns({552 name: 'test-service'553 });554 getAllFunctionsStub.returns(allFunctions);555 getFunctionStub.withArgs('func1').returns(func1);556 getFunctionStub.withArgs('func2').returns(func2);557 // Mock behavior558 // fsMock._streamMock.on.withArgs('open').yields();559 // fsMock._streamMock.on.withArgs('close').yields();560 // fsMock._statMock.isDirectory.returns(false);561 const expectedArtifactPath = path.join('.serverless', 'test-service.zip');562 return expect(module.copyExistingArtifacts()).to.be.fulfilled.then(() =>563 expect(serverless.service).to.have.a.nested.property('package.artifact').that.equals(expectedArtifactPath)564 );565 });566 });567 });568 describe('with individual packaging', () => {569 afterEach(() => {570 fsMock.copyFileSync.resetHistory();571 });572 beforeEach(() => {573 _.set(module, 'entryFunctions', entryFunctions);574 _.set(serverless.service.package, 'individually', true);575 getVersionStub.returns('1.18.0');576 getServiceObjectStub.returns({577 name: 'test-service'578 });579 getAllFunctionsStub.returns(allFunctions);580 getFunctionStub.withArgs('func1').returns(func1);581 getFunctionStub.withArgs('func2').returns(func2);582 getFunctionStub.withArgs('funcPython').returns(funcPython);583 });584 it('copies each node artifact', () => {585 const expectedFunc1Destination = path.join('.serverless', 'func1.zip');586 const expectedFunc2Destination = path.join('.serverless', 'func2.zip');587 return expect(module.copyExistingArtifacts()).to.be.fulfilled.then(() =>588 BbPromise.all([589 // Should copy an artifact per function into .serverless590 expect(fsMock.copyFileSync).callCount(2),591 expect(fsMock.copyFileSync).to.be.calledWith(path.join('.webpack', 'func1.zip'), expectedFunc1Destination),592 expect(fsMock.copyFileSync).to.be.calledWith(path.join('.webpack', 'func2.zip'), expectedFunc2Destination),593 // Should set package artifact locations594 expect(func1).to.have.a.nested.property('package.artifact').that.equals(expectedFunc1Destination),595 expect(func2).to.have.a.nested.property('package.artifact').that.equals(expectedFunc2Destination)596 ])597 );598 });599 it('copies only the artifact for function specified in options', () => {600 _.set(module, 'options.function', 'func1');601 const expectedFunc1Destination = path.join('.serverless', 'func1.zip');602 return expect(module.copyExistingArtifacts()).to.be.fulfilled.then(() =>603 BbPromise.all([604 // Should copy an artifact per function into .serverless605 expect(fsMock.copyFileSync).callCount(1),606 expect(fsMock.copyFileSync).to.be.calledWith(path.join('.webpack', 'func1.zip'), expectedFunc1Destination),607 // Should set package artifact locations608 expect(func1).to.have.a.nested.property('package.artifact').that.equals(expectedFunc1Destination)609 ])610 );611 });612 });613 });...
environment.js
Source:environment.js
1'use strict';2const os = require('os');3const path = require('path');4const sinon = require('sinon');5const Environment = require('yeoman-environment');6const assert = require('assert');7const helpers = require('yeoman-test');8const {TestAdapter} = require('yeoman-test/lib/adapter');9const Base = require('..');10const tmpdir = path.join(os.tmpdir(), 'yeoman-generator-environment');11/* eslint-disable max-nested-callbacks */12describe('Generator with environment version', () => {13 before(helpers.setUpTestDirectory(tmpdir));14 describe('mocked 3.0.0', () => {15 before(function () {16 this.timeout(100000);17 this.env = Environment.createEnv(18 [],19 {'skip-install': true},20 new TestAdapter()21 );22 this.env.getVersion = this.env.getVersion || (() => {});23 this.getVersionStub = sinon.stub(this.env, 'getVersion');24 this.Dummy = class extends Base {};25 this.dummy = new this.Dummy(['bar', 'baz', 'bom'], {26 foo: false,27 something: 'else',28 namespace: 'dummy',29 env: this.env,30 'skip-install': true,31 skipCheckEnv: true32 });33 });34 after(function () {35 this.getVersionStub.restore();36 });37 describe('#checkEnvironmentVersion', () => {38 describe('without args', () => {39 it('returns true', function () {40 this.getVersionStub.returns('3.0.0');41 assert.equal(this.dummy.checkEnvironmentVersion(), true);42 });43 });44 describe('with required environment', () => {45 before(function () {46 this.getVersionStub.returns('3.0.1');47 });48 it('returns true', function () {49 assert.equal(this.dummy.checkEnvironmentVersion('3.0.1'), true);50 });51 describe('with ignoreVersionCheck', () => {52 before(function () {53 this.dummy.options.ignoreVersionCheck = true;54 });55 after(function () {56 this.dummy.options.ignoreVersionCheck = false;57 });58 it('returns true', function () {59 this.getVersionStub.returns('3.0.1');60 assert.equal(this.dummy.checkEnvironmentVersion('3.0.1'), true);61 });62 });63 });64 describe('with greater than required environment', () => {65 it('returns true', function () {66 this.getVersionStub.returns('3.0.2');67 assert.equal(this.dummy.checkEnvironmentVersion('3.0.1'), true);68 });69 });70 describe('with less than required environment', () => {71 before(function () {72 this.getVersionStub.returns('3.0.0');73 });74 it('should throw', function () {75 assert.throws(76 () => this.dummy.checkEnvironmentVersion('3.0.1'),77 /requires yeoman-environment at least 3.0.1, current version is 3.0.0/78 );79 });80 describe('with warning', () => {81 it('should return false', function () {82 assert.equal(83 this.dummy.checkEnvironmentVersion('3.0.1', true),84 false85 );86 });87 });88 describe('with ignoreVersionCheck', () => {89 before(function () {90 this.dummy.options.ignoreVersionCheck = true;91 });92 after(function () {93 this.dummy.options.ignoreVersionCheck = false;94 });95 it('returns false', function () {96 assert.equal(this.dummy.checkEnvironmentVersion('3.0.1'), false);97 });98 });99 });100 describe('with required inquirer', () => {101 it('returns true', function () {102 this.getVersionStub.withArgs('inquirer').returns('7.1.0');103 assert.equal(104 this.dummy.checkEnvironmentVersion('inquirer', '7.1.0'),105 true106 );107 });108 });109 describe('with greater than required inquirer', () => {110 it('returns true', function () {111 this.getVersionStub.withArgs('inquirer').returns('7.1.1');112 assert.equal(113 this.dummy.checkEnvironmentVersion('inquirer', '7.1.0'),114 true115 );116 });117 });118 describe('with less than required inquirer', () => {119 before(function () {120 this.getVersionStub.withArgs('inquirer').returns('7.1.0');121 });122 it('throws exception', function () {123 assert.throws(124 () => this.dummy.checkEnvironmentVersion('inquirer', '7.1.1'),125 /requires inquirer at least 7.1.1, current version is 7.1.0/126 );127 });128 describe('with warning', () => {129 it('returns false', function () {130 assert.equal(131 this.dummy.checkEnvironmentVersion('inquirer', '7.1.1', true),132 false133 );134 });135 });136 describe('with ignoreVersionCheck', () => {137 before(function () {138 this.dummy.options.ignoreVersionCheck = true;139 });140 after(function () {141 this.dummy.options.ignoreVersionCheck = false;142 });143 it('returns false', function () {144 assert.equal(145 this.dummy.checkEnvironmentVersion('inquirer', '7.1.1'),146 false147 );148 });149 });150 });151 });152 describe('#prompt with storage', () => {153 it('with incompatible inquirer', function () {154 this.getVersionStub.withArgs().returns('3.0.0');155 this.getVersionStub.withArgs('inquirer').returns('7.0.0');156 assert.throws(157 () => this.dummy.prompt([], this.dummy.config),158 /requires inquirer at least 7.1.0, current version is 7.0.0/159 );160 });161 it('with compatible environment', function () {162 const self = this;163 this.getVersionStub.withArgs().returns('3.0.0');164 this.getVersionStub.withArgs('inquirer').returns('7.1.0');165 return self.dummy.prompt([], self.dummy.config);166 });167 });168 });169 describe('mocked 2.8.1', () => {170 before(function () {171 this.timeout(100000);172 this.env = Environment.createEnv(173 [],174 {'skip-install': true},175 new TestAdapter()176 );177 this.getVersion = Environment.prototype.getVersion;178 delete Environment.prototype.getVersion;179 this.Dummy = class extends Base {};180 this.dummy = new this.Dummy(['bar', 'baz', 'bom'], {181 foo: false,182 something: 'else',183 namespace: 'dummy',184 env: this.env,185 skipCheckEnv: true,186 'skip-install': true187 });188 });189 after(function () {190 Environment.prototype.getVersion = this.getVersion;191 });192 describe('#checkEnvironmentVersion', () => {193 describe('without args', () => {194 it('throws exception', function () {195 assert.throws(196 () => this.dummy.checkEnvironmentVersion(),197 /requires yeoman-environment at least 2.9.0, current version is less than 2.9.0/198 );199 });200 });201 describe('with ignoreVersionCheck', () => {202 before(function () {203 this.dummy.options.ignoreVersionCheck = true;204 });205 after(function () {206 this.dummy.options.ignoreVersionCheck = false;207 });208 describe('without args', () => {209 it('returns false', function () {210 assert.equal(this.dummy.checkEnvironmentVersion(), false);211 });212 });213 describe('without less then 3.0.0', () => {214 it('returns undefined', function () {215 assert.equal(this.dummy.checkEnvironmentVersion('2.9.0'), false);216 });217 });218 });219 });220 });...
index.unit.ts
Source:index.unit.ts
1import { expect } from 'chai';2import sinon from 'sinon';3import StateManager, { utils as defaultUtils } from '@/lib/services/state';4const VERSION_ID = 'version_id';5const version = {6 prototype: {7 data: {8 locales: ['en-US'],9 },10 model: {11 slots: [{ name: 'slot1' }],12 },13 context: {14 variables: { variable1: 1, variable2: 2 },15 },16 },17 variables: ['variable1'],18 rootDiagramID: 'a',19};20const state = {21 stack: [22 {23 programID: version.rootDiagramID,24 storage: {},25 variables: {},26 },27 ],28 variables: { slot1: 0, variable1: 1, variable2: 2 },29 storage: {},30};31describe('state manager unit tests', () => {32 afterEach(() => {33 sinon.restore();34 });35 describe('generate', () => {36 it('works', async () => {37 const getVersionStub = sinon.stub().resolves(version);38 const services = {39 dataAPI: {40 get: sinon.stub().returns({ getVersion: getVersionStub }),41 },42 };43 const stateManager = new StateManager({ ...services, utils: { ...defaultUtils } } as any, {} as any);44 expect(await stateManager.generate(version as any)).to.eql({ ...state, variables: { variable1: 1, variable2: 2 } });45 expect(services.dataAPI.get.callCount).to.eql(0);46 expect(getVersionStub.callCount).to.eql(0);47 });48 });49 describe('initializeVariables', () => {50 it('works', async () => {51 const services = {52 dataAPI: {53 getVersion: sinon.stub(),54 },55 };56 const stateManager = new StateManager({ ...services, utils: { ...defaultUtils } } as any, {} as any);57 expect(await stateManager.initializeVariables(version as any, {} as any)).to.eql({ variables: { variable1: 0, slot1: 0 } });58 expect(services.dataAPI.getVersion.callCount).to.eql(0);59 });60 });61 describe('handle', () => {62 it('works', async () => {63 const getVersionStub = sinon.stub().resolves(version);64 const services = {65 dataAPI: {66 get: sinon.stub().returns({ getVersion: getVersionStub }),67 },68 analyticsClient: { identify: sinon.stub().resolves() },69 };70 const stateManager = new StateManager({ ...services, utils: { ...defaultUtils } } as any, {} as any);71 const context = {72 versionID: VERSION_ID,73 data: { foo: 'bar' },74 } as any;75 const newContext = await stateManager.handle(context);76 expect(newContext).to.eql({77 request: null,78 versionID: VERSION_ID,79 state,80 trace: [],81 data: {82 ...context.data,83 locale: version.prototype.data.locales[0],84 api: newContext.data.api,85 },86 });87 expect(await newContext.data.api.getVersion(VERSION_ID)).to.eql(version);88 expect(getVersionStub.args).to.eql([[VERSION_ID]]);89 });90 it('throw errors on no versionID', async () => {91 const getVersionStub = sinon.stub().resolves(version);92 const services = {93 dataAPI: {94 get: sinon.stub().returns({ getVersion: getVersionStub }),95 },96 analyticsClient: { identify: sinon.stub().resolves() },97 };98 const stateManager = new StateManager({ ...services, utils: { ...defaultUtils } } as any, {} as any);99 const context = {100 data: { foo: 'bar' },101 } as any;102 await expect(stateManager.handle(context)).to.be.rejectedWith(Error);103 });104 it('works if stack does not exist', async () => {105 const getVersionStub = sinon.stub().resolves(version);106 const services = {107 dataAPI: {108 get: sinon.stub().returns({ getVersion: getVersionStub }),109 },110 analyticsClient: { identify: sinon.stub().resolves() },111 };112 const stateManager = new StateManager({ ...services, utils: { ...defaultUtils } } as any, {} as any);113 const context = {114 state: {},115 versionID: VERSION_ID,116 data: { foo: 'bar' },117 } as any;118 const newContext = await stateManager.handle(context);119 expect(newContext).to.eql({120 request: null,121 versionID: VERSION_ID,122 state,123 trace: [],124 data: {125 ...context.data,126 locale: version.prototype.data.locales[0],127 api: newContext.data.api,128 },129 });130 expect(await newContext.data.api.getVersion(VERSION_ID)).to.eql(version);131 expect(getVersionStub.args).to.eql([[VERSION_ID]]);132 });133 it('works if stack is empty', async () => {134 const getVersionStub = sinon.stub().resolves(version);135 const services = {136 dataAPI: {137 get: sinon.stub().returns({ getVersion: getVersionStub }),138 },139 analyticsClient: { identify: sinon.stub().resolves() },140 };141 const stateManager = new StateManager({ ...services, utils: { ...defaultUtils } } as any, {} as any);142 const context = {143 state: { stack: [] },144 versionID: VERSION_ID,145 data: { foo: 'bar' },146 } as any;147 const newContext = await stateManager.handle(context);148 expect(newContext).to.eql({149 request: null,150 versionID: VERSION_ID,151 state,152 trace: [],153 data: {154 ...context.data,155 locale: version.prototype.data.locales[0],156 api: newContext.data.api,157 },158 });159 expect(await newContext.data.api.getVersion(VERSION_ID)).to.eql(version);160 expect(getVersionStub.args).to.eql([[VERSION_ID]]);161 });162 it('works if analytics client throws and stack exists', async () => {163 const getVersionStub = sinon.stub().resolves(version);164 const services = {165 dataAPI: {166 get: sinon.stub().returns({ getVersion: getVersionStub }),167 },168 analyticsClient: { identify: sinon.stub().throws() },169 };170 const stateManager = new StateManager({ ...services, utils: { ...defaultUtils } } as any, {} as any);171 const context = {172 state: { stack: ['some-stack-value'] },173 versionID: VERSION_ID,174 data: { foo: 'bar' },175 } as any;176 const newContext = await stateManager.handle(context);177 expect(newContext).to.eql({178 request: null,179 versionID: VERSION_ID,180 state: { stack: ['some-stack-value'], variables: { slot1: 0, variable1: 0 } },181 trace: [],182 data: {183 ...context.data,184 locale: version.prototype.data.locales[0],185 api: newContext.data.api,186 },187 });188 expect(await newContext.data.api.getVersion(VERSION_ID)).to.eql(version);189 expect(getVersionStub.args).to.eql([[VERSION_ID]]);190 });191 });...
Using AI Code Generation
1const parent = require('stryker-parent');2const version = parent.getVersionStub();3console.log(version);4const parent = require('stryker-parent');5const version = parent.getVersionStub();6console.log(version);7const parent = require('stryker-parent');8const version = parent.getVersionStub();9console.log(version);10const parent = require('stryker-parent');11const version = parent.getVersionStub();12console.log(version);13const parent = require('stryker-parent');14const version = parent.getVersionStub();15console.log(version);16const parent = require('stryker-parent');17const version = parent.getVersionStub();18console.log(version);19const parent = require('stryker-parent');20const version = parent.getVersionStub();21console.log(version);22const parent = require('stryker-parent');23const version = parent.getVersionStub();24console.log(version);25const parent = require('stryker-parent');26const version = parent.getVersionStub();27console.log(version);28const parent = require('stryker-parent');29const version = parent.getVersionStub();30console.log(version);31const parent = require('stryker-parent');32const version = parent.getVersionStub();33console.log(version);34const parent = require('stryker-parent');35const version = parent.getVersionStub();36console.log(version);37const parent = require('stryker-parent');38const version = parent.getVersionStub();39console.log(version);
Using AI Code Generation
1var getVersionStub = require('stryker-parent').getVersionStub;2console.log(getVersionStub());3var getVersionStub = require('stryker-parent').getVersionStub;4console.log(getVersionStub());5var getVersionStub = require('stryker-parent').getVersionStub;6console.log(getVersionStub());7var getVersionStub = require('stryker-parent').getVersionStub;8console.log(getVersionStub());9var getVersionStub = require('stryker-parent').getVersionStub;10console.log(getVersionStub());11var getVersionStub = require('stryker-parent').getVersionStub;12console.log(getVersionStub());13var getVersionStub = require('stryker-parent').getVersionStub;14console.log(getVersionStub());15var getVersionStub = require('stryker-parent').getVersionStub;16console.log(getVersionStub());17var getVersionStub = require('stryker-parent').getVersionStub;18console.log(getVersionStub());19var getVersionStub = require('stryker-parent').getVersionStub;20console.log(getVersionStub());21var getVersionStub = require('stryker-parent
Using AI Code Generation
1const getVersionStub = require('stryker-parent').getVersionStub;2console.log(getVersionStub());3const getVersionStub = require('stryker-parent').getVersionStub;4console.log(getVersionStub());5const getVersionStub = require('stryker-parent').getVersionStub;6console.log(getVersionStub());7const getVersionStub = require('stryker-parent').getVersionStub;8console.log(getVersionStub());9const getVersionStub = require('stryker-parent').getVersionStub;10console.log(getVersionStub());11const getVersionStub = require('stryker-parent').getVersionStub;12console.log(getVersionStub());13const getVersionStub = require('stryker-parent').getVersionStub;14console.log(getVersionStub());15const getVersionStub = require('stryker-parent').getVersionStub;16console.log(getVersionStub());17const getVersionStub = require('stryker-parent').getVersionStub;18console.log(getVersionStub());19const getVersionStub = require('stryker-parent').getVersionStub;20console.log(getVersionStub());21const getVersionStub = require('stryker-parent
Using AI Code Generation
1const getVersionStub = require('stryker-parent').getVersionStub;2console.log(getVersionStub());3const getVersionStub = require('stryker-parent').getVersionStub;4console.log(getVersionStub());5const getVersionStub = require('stryker-parent').getVersionStub;6console.log(getVersionStub());7const getVersionStub = require('stryker-parent').getVersionStub;8console.log(getVersionStub());9const getVersionStub = require('stryker-parent').getVersionStub;10console.log(getVersionStub());11const getVersionStub = require('stryker-parent').getVersionStub;12console.log(getVersionStub());13const getVersionStub = require('stryker-parent').getVersionStub;14console.log(getVersionStub());15const getVersionStub = require('stryker-parent').getVersionStub;16console.log(getVersionStub());17const getVersionStub = require('stryker-parent').getVersionStub;18console.log(getVersionStub());19const getVersionStub = require('stryker-parent').getVersionStub;20console.log(getVersionStub());
Using AI Code Generation
1var getVersionStub = require('stryker-parent').getVersionStub;2var getVersionStub = require('stryker-parent').getVersionStub;3var getVersionStub = require('stryker-parent').getVersionStub;4var versionStub = getVersionStub();5console.log(versionStub);6var getVersionStub = require('stryker-parent').getVersionStub;7var versionStub = getVersionStub();8console.log(versionStub);9var getVersionStub = require('stryker-parent').getVersionStub;10var versionStub = getVersionStub();11console.log(versionStub);12var getVersionStub = require('stryker-parent').getVersionStub;13var versionStub = getVersionStub();14console.log(versionStub);15var getVersionStub = require('stryker-parent').getVersionStub;16var versionStub = getVersionStub();17console.log(versionStub);18var getVersionStub = require('stryker-parent').getVersionStub;19var versionStub = getVersionStub();20console.log(versionStub);21var getVersionStub = require('stryker-parent').getVersionStub;22var versionStub = getVersionStub();23console.log(versionStub);24var getVersionStub = require('stryker-parent').getVersionStub;25var versionStub = getVersionStub();26console.log(versionStub);27var getVersionStub = require('stryker-parent').getVersionStub;28var versionStub = getVersionStub();29console.log(versionStub);30var getVersionStub = require('stryker-parent').getVersionStub;31var versionStub = getVersionStub();32console.log(versionStub);33var getVersionStub = require('stryker-parent').getVersionStub;34var versionStub = getVersionStub();35console.log(versionStub);36var getVersionStub = require('stryker-parent').getVersionStub;37var versionStub = getVersionStub();38console.log(versionStub);
Using AI Code Generation
1var getVersionStub = require('stryker-parent').getVersionStub;2console.log(getVersionStub());3var getVersionStub = require('stryker-parent').getVersionStub;4console.log(getVersionStub());5var getVersionStub = require('stryker-parent').getVersionStub;6console.log(getVersionStub());7var getVersionStub = require('stryker-parent').getVersionStub;8console.log(getVersionStub());9var getVersionStub = require('stryker-parent').getVersionStub;10console.log(getVersionStub());11var getVersionStub = require('stryker-parent').getVersionStub;12console.log(getVersionStub());13var getVersionStub = require('stryker-parent').getVersionStub;14console.log(getVersionStub());15var getVersionStub = require('stryker-parent').getVersionStub;16console.log(getVersionStub());17var getVersionStub = require('stryker-parent').getVersionStub;18console.log(getVersionStub());
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!!