Best JavaScript code snippet using stryker-parent
karma-test-runner.spec.ts
Source:karma-test-runner.spec.ts
1import { LoggerFactoryMethod } from '@stryker-mutator/api/logging';2import { commonTokens } from '@stryker-mutator/api/plugin';3import { testInjector, assertions, factory, tick } from '@stryker-mutator/test-helpers';4import { expect } from 'chai';5import { TestResults } from 'karma';6import sinon from 'sinon';7// eslint-disable-next-line @typescript-eslint/no-require-imports8import { Task } from '@stryker-mutator/util';9import { DryRunOptions, MutantRunOptions, TestStatus } from '@stryker-mutator/api/test-runner';10import { MutantCoverage } from '@stryker-mutator/api/core';11import strykerKarmaConf from '../../src/starters/stryker-karma.conf';12import { KarmaTestRunner } from '../../src/karma-test-runner';13import * as projectStarter from '../../src/starters/project-starter';14import { StrykerKarmaSetup, NgConfigOptions } from '../../src-generated/karma-runner-options';15import { Browser, KarmaSpec, StrykerReporter } from '../../src/karma-plugins/stryker-reporter';16import { TestHooksMiddleware } from '../../src/karma-plugins/test-hooks-middleware';17import { karma } from '../../src/karma-wrapper';18// Unit tests for both the KarmaTestRunner and the StrykerReporter, as they are closely related19describe(KarmaTestRunner.name, () => {20 let projectStarterMock: sinon.SinonStubbedInstance<projectStarter.ProjectStarter>;21 let setGlobalsStub: sinon.SinonStubbedMember<typeof strykerKarmaConf.setGlobals>;22 let karmaRunStub: sinon.SinonStubbedMember<typeof karma.runner.run>;23 let getLogger: LoggerFactoryMethod;24 let testHooksMiddlewareMock: sinon.SinonStubbedInstance<TestHooksMiddleware>;25 beforeEach(() => {26 projectStarterMock = sinon.createStubInstance(projectStarter.ProjectStarter);27 testHooksMiddlewareMock = sinon.createStubInstance(TestHooksMiddleware);28 sinon.stub(projectStarter, 'ProjectStarter').returns(projectStarterMock);29 setGlobalsStub = sinon.stub(strykerKarmaConf, 'setGlobals');30 karmaRunStub = sinon.stub(karma.runner, 'run');31 sinon.stub(TestHooksMiddleware, 'instance').value(testHooksMiddlewareMock);32 getLogger = testInjector.injector.resolve(commonTokens.getLogger);33 });34 function createSut() {35 return testInjector.injector.injectClass(KarmaTestRunner);36 }37 it('should load default setup', () => {38 createSut();39 expect(setGlobalsStub).calledWith({40 getLogger,41 karmaConfig: undefined,42 karmaConfigFile: undefined,43 disableBail: false,44 });45 });46 it('should setup karma from stryker options', () => {47 const expectedSetup: StrykerKarmaSetup = {48 config: {49 basePath: 'foo/bar',50 },51 configFile: 'baz.conf.js',52 projectType: 'angular-cli',53 };54 testInjector.options.karma = expectedSetup;55 testInjector.options.disableBail = true;56 createSut();57 expect(setGlobalsStub).calledWith({58 getLogger,59 karmaConfig: expectedSetup.config,60 karmaConfigFile: expectedSetup.configFile,61 disableBail: true,62 });63 expect(testInjector.logger.warn).not.called;64 expect(projectStarter.ProjectStarter).calledWith(sinon.match.func, expectedSetup);65 });66 it('should run ng test with parameters from stryker options', () => {67 const ngConfig: NgConfigOptions = {};68 ngConfig.testArguments = {69 project: '@ns/mypackage',70 };71 const expectedSetup: StrykerKarmaSetup = {72 config: {73 basePath: 'foo/bar',74 },75 configFile: 'baz.conf.js',76 ngConfig,77 projectType: 'angular-cli',78 };79 testInjector.options.karma = expectedSetup;80 testInjector.options.disableBail = true;81 createSut();82 expect(setGlobalsStub).calledWith({83 getLogger,84 karmaConfig: expectedSetup.config,85 karmaConfigFile: expectedSetup.configFile,86 disableBail: true,87 });88 expect(testInjector.logger.warn).not.called;89 expect(projectStarter.ProjectStarter).calledWith(sinon.match.func, expectedSetup);90 });91 describe('init', () => {92 let sut: KarmaTestRunner;93 async function actInit() {94 const initPromise = sut.init();95 StrykerReporter.instance.onBrowsersReady();96 await initPromise;97 }98 beforeEach(async () => {99 sut = createSut();100 StrykerReporter.instance.karmaConfig = await karma.config.parseConfig(null, {}, { promiseConfig: true });101 });102 it('should start karma', async () => {103 projectStarterMock.start.resolves({ exitPromise: new Task<number>().promise });104 await actInit();105 expect(projectStarterMock.start).called;106 });107 it('should reject when karma start rejects', async () => {108 const expected = new Error('karma unavailable');109 projectStarterMock.start.rejects(expected);110 await expect(sut.init()).rejectedWith(expected);111 });112 it('should reject on browser errors', async () => {113 projectStarterMock.start.resolves({ exitPromise: new Task<number>().promise });114 const expected = new Error('karma unavailable');115 const onGoingInit = sut.init();116 StrykerReporter.instance.onBrowserError(createBrowser(), expected);117 await expect(onGoingInit).rejectedWith(expected);118 });119 it('should reject when karma exists during init', async () => {120 const exitTask = new Task<number>();121 projectStarterMock.start.resolves({ exitPromise: exitTask.promise });122 const onGoingInit = sut.init();123 exitTask.resolve(1);124 await expect(onGoingInit).rejectedWith(125 "Karma exited prematurely with exit code 1. Please run stryker with `--logLevel trace` to see the karma logging and figure out what's wrong."126 );127 });128 });129 describe('dryRun', () => {130 let sut: KarmaTestRunner;131 beforeEach(() => {132 sut = createSut();133 karmaRunStub.callsArgOnWith(1, null, 0);134 });135 function actDryRun({136 specResults = [],137 runResults = createKarmaTestResults(),138 options = factory.dryRunOptions(),139 mutantCoverage = factory.mutantCoverage(),140 browserError = undefined,141 }: {142 specResults?: KarmaSpec[];143 runResults?: TestResults;144 options?: DryRunOptions;145 mutantCoverage?: MutantCoverage;146 browserError?: [browser: Browser, error: any] | undefined;147 }) {148 const dryRunPromise = sut.dryRun(options);149 actRun({ browserError, specResults, mutantCoverage, hitCount: undefined, runResults });150 return dryRunPromise;151 }152 it('should report completed specs as test results', async () => {153 const specResults = [154 createKarmaSpec({ id: 'spec1', suite: ['foo'], description: 'should bar', time: 42, success: true }),155 createKarmaSpec({156 id: 'spec2',157 suite: ['bar', 'when baz'],158 description: 'should qux',159 log: ['expected quux to be qux', 'expected length of 1'],160 time: 44,161 success: false,162 }),163 createKarmaSpec({ id: 'spec3', suite: ['quux'], description: 'should corge', time: 45, skipped: true }),164 ];165 const expectedTests = [166 factory.testResult({ id: 'spec1', status: TestStatus.Success, timeSpentMs: 42, name: 'foo should bar' }),167 factory.testResult({168 id: 'spec2',169 status: TestStatus.Failed,170 timeSpentMs: 44,171 name: 'bar when baz should qux',172 failureMessage: 'expected quux to be qux, expected length of 1',173 }),174 factory.testResult({ id: 'spec3', status: TestStatus.Skipped, timeSpentMs: 45, name: 'quux should corge' }),175 ];176 const actualResult = await actDryRun({ specResults });177 assertions.expectCompleted(actualResult);178 expect(actualResult.tests).deep.eq(expectedTests);179 });180 it('should run karma with custom karma configuration', async () => {181 // Arrange182 projectStarterMock.start.resolves({ exitPromise: new Task<number>().promise });183 StrykerReporter.instance.karmaConfig = await karma.config.parseConfig(null, {}, { promiseConfig: true });184 StrykerReporter.instance.karmaConfig.hostname = 'www.localhost.com';185 StrykerReporter.instance.karmaConfig.port = 1337;186 StrykerReporter.instance.karmaConfig.listenAddress = 'www.localhost.com:1337';187 const parseConfigStub = sinon.stub(karma.config, 'parseConfig');188 const expectedRunConfig = { custom: 'config' };189 parseConfigStub.resolves(expectedRunConfig);190 const initPromise = sut.init();191 StrykerReporter.instance.onBrowsersReady();192 await initPromise;193 // Act194 await actDryRun({});195 // Assert196 expect(karmaRunStub).calledWith(expectedRunConfig, sinon.match.func);197 expect(parseConfigStub).calledWithExactly(null, { hostname: 'www.localhost.com', port: 1337, listenAddress: 'www.localhost.com:1337' });198 });199 it('should log when karma run is done', async () => {200 await actDryRun({});201 expect(testInjector.logger.debug).calledWith('karma run done with ', 0);202 });203 it('should clear run results between runs', async () => {204 const firstTestResult = createKarmaSpec({ id: 'spec1', suite: ['foo'], description: 'should bar', time: 42, success: true });205 const expectedTestResult = factory.testResult({ id: 'spec1', status: TestStatus.Success, timeSpentMs: 42, name: 'foo should bar' });206 const actualFirstResult = await actDryRun({ specResults: [firstTestResult] });207 const actualSecondResult = await actDryRun({});208 assertions.expectCompleted(actualFirstResult);209 assertions.expectCompleted(actualSecondResult);210 expect(actualFirstResult.tests).deep.eq([expectedTestResult]);211 expect(actualSecondResult.tests).lengthOf(0);212 });213 it('should configure the coverage analysis in the test hooks middleware', async () => {214 await actDryRun({ options: factory.dryRunOptions({ coverageAnalysis: 'all' }) });215 expect(testHooksMiddlewareMock.configureCoverageAnalysis).calledWithExactly('all');216 });217 it('should add coverage report when the reporter raises the "coverage_report" event', async () => {218 const expectedCoverageReport = factory.mutantCoverage({ static: { '1': 4 } });219 const actualResult = await actDryRun({ mutantCoverage: expectedCoverageReport });220 assertions.expectCompleted(actualResult);221 expect(actualResult.mutantCoverage).eq(expectedCoverageReport);222 });223 it('should add error when the reporter raises the "browser_error" event', async () => {224 const expectedError = 'Global variable undefined';225 const actualResult = await actDryRun({ runResults: createKarmaTestResults({ error: true }), browserError: [createBrowser(), expectedError] });226 assertions.expectErrored(actualResult);227 expect(actualResult.errorMessage).deep.eq(expectedError);228 });229 it('should report state Error when tests where ran and run completed in an error', async () => {230 const actualResult = await actDryRun({231 runResults: createKarmaTestResults({ error: true }),232 specResults: [createKarmaSpec()],233 browserError: [createBrowser(), 'some error'],234 });235 assertions.expectErrored(actualResult);236 });237 });238 describe('mutantRun', () => {239 let sut: KarmaTestRunner;240 function actMutantRun({241 specResults = [],242 runResults = createKarmaTestResults(),243 options = factory.mutantRunOptions(),244 mutantCoverage = factory.mutantCoverage(),245 hitCount = undefined,246 browserError = undefined,247 }: {248 specResults?: KarmaSpec[];249 runResults?: TestResults;250 options?: MutantRunOptions;251 hitCount?: number;252 mutantCoverage?: MutantCoverage;253 browserError?: [browser: Browser, error: any] | undefined;254 }) {255 const promise = sut.mutantRun(options);256 actRun({ specResults, runResults, mutantCoverage, hitCount, browserError });257 return promise;258 }259 beforeEach(() => {260 sut = createSut();261 karmaRunStub.callsArgOn(1, 0);262 });263 it('should configure the mutant run on the middleware', async () => {264 const options = factory.mutantRunOptions({ testFilter: ['foobar'] });265 await actMutantRun({ options });266 expect(testHooksMiddlewareMock.configureMutantRun).calledOnceWithExactly(options);267 });268 it('should correctly report a survived mutant', async () => {269 const result = await actMutantRun({ specResults: [createKarmaSpec({ success: true })] });270 assertions.expectSurvived(result);271 expect(result.nrOfTests).eq(1);272 });273 it('should correctly report a killed mutant', async () => {274 const result = await actMutantRun({ specResults: [createKarmaSpec({ success: true }), createKarmaSpec({ success: false })] });275 assertions.expectKilled(result);276 expect(result.nrOfTests).eq(2);277 });278 it('should report a timeout when the browser disconnects', async () => {279 arrangeLauncherMock();280 const onGoingRun = sut.mutantRun(factory.mutantRunOptions());281 StrykerReporter.instance.onRunStart();282 StrykerReporter.instance.onBrowserError(createBrowser({ id: '42', state: 'DISCONNECTED' }), 'disconnected');283 StrykerReporter.instance.onRunComplete(null, createKarmaTestResults({ disconnected: true }));284 StrykerReporter.instance.onBrowsersReady();285 const result = await onGoingRun;286 assertions.expectTimeout(result);287 expect(result.reason).eq('Browser disconnected during test execution. Karma error: disconnected');288 });289 it('should restart the browser and wait until it is restarted when it gets disconnected (issue #2989)', async () => {290 // Arrange291 const { launcher, karmaServer } = arrangeLauncherMock();292 // Act293 let runCompleted = false;294 const onGoingRun = sut.mutantRun(factory.mutantRunOptions()).then(() => (runCompleted = true));295 StrykerReporter.instance.onRunStart();296 StrykerReporter.instance.onBrowserError(createBrowser({ id: '42', state: 'DISCONNECTED' }), 'disconnected');297 // Assert298 expect(launcher.restart).calledWith('42');299 expect(karmaServer.get).calledWith('launcher');300 StrykerReporter.instance.onRunComplete(null, createKarmaTestResults({ disconnected: true }));301 await tick();302 expect(runCompleted).false;303 StrykerReporter.instance.onBrowsersReady();304 await onGoingRun;305 });306 it('should report a timeout when the hitLimit was reached', async () => {307 const result = await actMutantRun({308 options: factory.mutantRunOptions({ hitLimit: 9 }),309 specResults: [createKarmaSpec({ success: false })],310 hitCount: 10,311 });312 assertions.expectTimeout(result);313 expect(result.reason).contains('Hit limit reached (10/9)');314 });315 it('should reset the hitLimit between runs', async () => {316 const firstResult = await actMutantRun({317 options: factory.mutantRunOptions({ hitLimit: 9 }),318 specResults: [createKarmaSpec({ success: false })],319 hitCount: 10,320 });321 const secondResult = await actMutantRun({322 options: factory.mutantRunOptions({ hitLimit: undefined }),323 specResults: [createKarmaSpec({ success: false })],324 hitCount: 10,325 });326 assertions.expectTimeout(firstResult);327 assertions.expectKilled(secondResult);328 });329 });330 describe('dispose', () => {331 beforeEach(async () => {332 StrykerReporter.instance.karmaConfig = await karma.config.parseConfig(null, {}, { promiseConfig: true });333 });334 it('should not do anything if there is no karma server', async () => {335 const sut = createSut();336 await expect(sut.dispose()).not.rejected;337 });338 it('should stop the karma server', async () => {339 const karmaServerMock = sinon.createStubInstance(karma.Server);340 StrykerReporter.instance.karmaServer = karmaServerMock;341 const sut = createSut();342 await sut.dispose();343 expect(karmaServerMock.stop).called;344 });345 it('should await the exit promise provided at startup', async () => {346 // Arrange347 const sut = createSut();348 const karmaServerMock = sinon.createStubInstance(karma.Server);349 StrykerReporter.instance.karmaServer = karmaServerMock;350 const exitTask = new Task<number>();351 let disposeResolved = false;352 projectStarterMock.start.resolves({ exitPromise: exitTask.promise });353 const initPromise = sut.init();354 StrykerReporter.instance.onBrowsersReady();355 await initPromise;356 // Act357 const onGoingDisposal = sut.dispose().then(() => (disposeResolved = true));358 // Assert359 await tick();360 expect(disposeResolved).false;361 exitTask.resolve(1);362 await onGoingDisposal;363 });364 });365 function actRun({366 specResults,367 runResults,368 mutantCoverage,369 hitCount,370 browserError,371 }: {372 specResults: KarmaSpec[];373 runResults: TestResults;374 mutantCoverage: MutantCoverage;375 hitCount: number | undefined;376 browserError: [browser: Browser, error: any] | undefined;377 }) {378 StrykerReporter.instance.onRunStart();379 specResults.forEach((spec) => StrykerReporter.instance.onSpecComplete(null, spec));380 if (browserError) {381 StrykerReporter.instance.onBrowserError(...browserError);382 }383 StrykerReporter.instance.onBrowserComplete(null, { mutantCoverage, hitCount });384 StrykerReporter.instance.onRunComplete(null, runResults);385 }386});387function arrangeLauncherMock() {388 const karmaServerMock = sinon.createStubInstance(karma.Server);389 const launcherMock = sinon.createStubInstance(karma.launcher.Launcher);390 StrykerReporter.instance.karmaServer = karmaServerMock;391 karmaServerMock.get.returns(launcherMock);392 return { launcher: launcherMock, karmaServer: karmaServerMock };393}394function createKarmaSpec(overrides?: Partial<KarmaSpec>): KarmaSpec {395 return {396 description: 'baz',397 id: '1',398 log: [],399 skipped: false,400 success: true,401 suite: ['foo', 'bar'],402 time: 42,403 ...overrides,404 };405}406function createKarmaTestResults(overrides?: Partial<TestResults>): TestResults {407 return {408 disconnected: false,409 error: false,410 exitCode: 0,411 failed: 0,412 success: 0,413 ...overrides,414 };415}416function createBrowser(overrides?: Partial<Browser>): Browser {417 return {418 id: '123123',419 state: 'CONNECTED',420 ...overrides,421 };...
Using AI Code Generation
1const karmaServerMock = require('stryker-parent').karmaServerMock;2const karmaServerMock = require('stryker-karma-runner').karmaServerMock;3const karmaServerMock = require('stryker').karmaServerMock;4const karmaServerMock = require('stryker-parent').karmaServerMock;5const karmaServerMock = require('stryker-karma-runner').karmaServerMock;6const karmaServerMock = require('stryker').karmaServerMock;7const karmaServerMock = require('stryker-parent').karmaServerMock;8const karmaServerMock = require('stryker-karma-runner').karmaServerMock;9const karmaServerMock = require('stryker').karmaServerMock;10const karmaServerMock = require('stryker-parent').karmaServerMock;11const karmaServerMock = require('stryker-karma-runner').karmaServerMock;12const karmaServerMock = require('stryker').karmaServerMock;13const karmaServerMock = require('stryker-parent').karmaServerMock;14const karmaServerMock = require('stryker-karma-runner').karmaServerMock;15const karmaServerMock = require('stryker').karmaServerMock;
Using AI Code Generation
1var karmaServerMock = require('stryker-parent').karmaServerMock;2karmaServerMock.mock('test.js', function (karmaConfig) {3 karmaConfig.set({4 });5});6module.exports = function (config) {7 config.set({8 karma: {9 }10 });11};
Using AI Code Generation
1karmaServerMock = require('stryker-parent').karmaServerMock;2karmaServerMock = require('stryker-parent').karmaServerMock;3karmaServerMock = require('stryker-parent').karmaServerMock;4karmaServerMock = require('stryker-parent').karmaServerMock;5karmaServerMock = require('stryker-parent').karmaServerMock;6karmaServerMock = require('stryker-parent').karmaServerMock;7karmaServerMock = require('stryker-parent').karmaServerMock;8karmaServerMock = require('stryker-parent').karmaServerMock;9karmaServerMock = require('stryker-parent').karmaServerMock;10karmaServerMock = require('stryker-parent').karmaServerMock;11karmaServerMock = require('stryker-parent').karmaServerMock;12karmaServerMock = require('stryker-parent').karmaServerMock;13karmaServerMock = require('stryker-parent').karmaServerMock;14karmaServerMock = require('stryker-parent').karmaServerMock;15karmaServerMock = require('stryker-parent').karmaServerMock;16karmaServerMock = require('
Using AI Code Generation
1var karmaServerMock = require('stryker-parent').karmaServerMock;2karmaServerMock.start();3var karmaServerMock = require('stryker').karmaServerMock;4karmaServerMock.start();5var karmaServerMock = require('stryker').karmaServerMock;6var mock = karmaServerMock.start();7mock.on('start', function() {8console.log('started');9});10mock.on('run_complete', function() {11console.log('run complete');12});13mock.start();14mock.runComplete();15var karmaServerMock = require('stryker').karmaServerMock;16var mock = karmaServerMock.start();17mock.on('start', function() {18console.log('started');19});20mock.on('run_complete', function() {21console.log('run complete');22});23mock.start();24mock.runComplete();25var karmaServerMock = require('stryker').karmaServerMock;26var mock = karmaServerMock.start();27mock.on('start', function() {28console.log('started');29});30mock.on('run_complete', function() {31console.log('run complete');32});33mock.start();34mock.runComplete();35var karmaServerMock = require('stryker').karmaServerMock;36var mock = karmaServerMock.start();37mock.on('start', function() {38console.log('started');39});40mock.on('run_complete', function() {41console.log('run complete');42});43mock.start();44mock.runComplete();
Using AI Code Generation
1var karmaServerMock = require('stryker-parent').karmaServerMock;2karmaServerMock.run({3 { pattern: 'src/**/*.js', included: false },4 { pattern: 'test/**/*.js', included: false },5});6describe('my test', function () {7 it('should do something', function () {8 expect(true).toBe(true);9 });10});11[2016-07-16 12:35:45.070] [INFO] Stryker - 1 Mutant(s) generated12[2016-07-16 12:35:45.071] [INFO] SandboxPool - Creating 1 test runners (based on CPU count)13[2016-07-16 12:35:45.078] [INFO] Sandbox - Running initial test(s)
Using AI Code Generation
1var karmaServerMock = require('stryker-parent').karmaServerMock;2var config = {3};4karmaServerMock(config, function(err, result) {5 console.log(result);6});7var karmaServerMock = require('stryker-parent').karmaServerMock;8var config = {9};10karmaServerMock(config, function(err, result) {11 console.log(result);12});13var karmaServerMock = require('stryker-parent').karmaServerMock;14var config = {15};16karmaServerMock(config, function(err, result) {17 console.log(result);18});19var karmaServerMock = require('stryker-parent').karmaServerMock;20var config = {21};22karmaServerMock(config, function(err, result) {23 console.log(result);24});25var karmaServerMock = require('stryker-parent').karmaServerMock;26var config = {27};28karmaServerMock(config, function(err, result) {29 console.log(result);30});31var karmaServerMock = require('stryker-parent').karmaServerMock;32var config = {33};34karmaServerMock(config, function(err, result) {35 console.log(result);36});
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!!