Best JavaScript code snippet using stryker-parent
mocha-test-runner.spec.ts
Source:mocha-test-runner.spec.ts
1import { expect } from 'chai';2import Mocha from 'mocha';3import { testInjector, factory, assertions, tick } from '@stryker-mutator/test-helpers';4import sinon from 'sinon';5import { KilledMutantRunResult, MutantRunStatus, TestRunnerCapabilities } from '@stryker-mutator/api/test-runner';6import { Task } from '@stryker-mutator/util';7import { MochaTestRunner } from '../../src/mocha-test-runner.js';8import { StrykerMochaReporter } from '../../src/stryker-mocha-reporter.js';9import { MochaAdapter } from '../../src/mocha-adapter.js';10import * as pluginTokens from '../../src/plugin-tokens.js';11import { MochaOptionsLoader } from '../../src/mocha-options-loader.js';12import { createMochaOptions } from '../helpers/factories.js';13describe(MochaTestRunner.name, () => {14 let mocha: sinon.SinonStubbedInstance<Mocha> & { suite: sinon.SinonStubbedInstance<Mocha.Suite>; dispose?: sinon.SinonStub };15 let mochaAdapterMock: sinon.SinonStubbedInstance<MochaAdapter>;16 let mochaOptionsLoaderMock: sinon.SinonStubbedInstance<MochaOptionsLoader>;17 let reporterMock: sinon.SinonStubbedInstance<StrykerMochaReporter>;18 let testFileNames: string[];19 beforeEach(() => {20 reporterMock = sinon.createStubInstance(StrykerMochaReporter);21 reporterMock.tests = [];22 mochaAdapterMock = sinon.createStubInstance(MochaAdapter);23 mochaOptionsLoaderMock = sinon.createStubInstance(MochaOptionsLoader);24 mocha = sinon.createStubInstance(Mocha) as any;25 mocha.suite = sinon.createStubInstance(Mocha.Suite) as any;26 mocha.suite.suites = [];27 mochaAdapterMock.create.returns(mocha as unknown as Mocha);28 testFileNames = [];29 mochaAdapterMock.collectFiles.returns(testFileNames);30 });31 afterEach(() => {32 delete StrykerMochaReporter.log;33 });34 function createSut(): MochaTestRunner {35 return testInjector.injector36 .provideValue(pluginTokens.mochaAdapter, mochaAdapterMock)37 .provideValue(pluginTokens.loader, mochaOptionsLoaderMock)38 .provideValue(pluginTokens.globalNamespace, '__stryker2__' as const)39 .injectClass(MochaTestRunner);40 }41 describe('constructor', () => {42 it('should set the static `log` property on StrykerMochaReporter', () => {43 createSut();44 expect(StrykerMochaReporter.log).eq(testInjector.logger);45 });46 });47 describe('capabilities', () => {48 it('should communicate reloadEnvironment=false', async () => {49 const expectedCapabilities: TestRunnerCapabilities = { reloadEnvironment: false };50 expect(await createSut().capabilities()).deep.eq(expectedCapabilities);51 });52 });53 describe(MochaTestRunner.prototype.init.name, () => {54 let sut: MochaTestRunner;55 beforeEach(() => {56 sut = createSut();57 });58 it('should load mocha options', async () => {59 mochaOptionsLoaderMock.load.returns({});60 await sut.init();61 expect(mochaOptionsLoaderMock.load).calledWithExactly(testInjector.options);62 });63 it('should collect the files', async () => {64 testFileNames.push('foo.js', 'foo.spec.js');65 const mochaOptions = Object.freeze(createMochaOptions());66 mochaOptionsLoaderMock.load.returns(mochaOptions);67 await sut.init();68 expect(mochaAdapterMock.collectFiles).calledWithExactly(mochaOptions);69 testFileNames.forEach((fileName) => {70 expect(mocha.addFile).calledWith(fileName);71 });72 });73 it('should not handle requires when there are no `requires`', async () => {74 mochaOptionsLoaderMock.load.returns({});75 await sut.init();76 expect(mochaAdapterMock.handleRequires).not.called;77 });78 it('should handle requires and collect root hooks', async () => {79 const requires = ['test/setup.js'];80 // eslint-disable-next-line @typescript-eslint/no-empty-function81 const expectedRootHooks = { beforeEach() {} };82 mochaOptionsLoaderMock.load.returns(createMochaOptions({ require: requires }));83 mochaAdapterMock.handleRequires.resolves(expectedRootHooks);84 await sut.init();85 const expectedMochaOptions: Mocha.MochaOptions = { rootHooks: expectedRootHooks };86 expect(mochaAdapterMock.create).calledWith(sinon.match(expectedMochaOptions));87 });88 it('should reject when requires contains "esm" (see #3014)', async () => {89 const requires = ['esm', 'ts-node/require'];90 mochaOptionsLoaderMock.load.returns(createMochaOptions({ require: requires }));91 await expect(sut.init()).rejectedWith(92 'Config option "mochaOptions.require" does not support "esm", please use `"testRunnerNodeArgs": ["--require", "esm"]` instead. See https://github.com/stryker-mutator/stryker-js/issues/3014 for more information.'93 );94 });95 it('should pass along supported options to mocha', async () => {96 // Arrange97 const mochaOptions = Object.freeze(98 createMochaOptions({99 'async-only': true,100 grep: 'grepme',101 opts: 'opts',102 require: [],103 ui: 'exports',104 })105 );106 mochaOptionsLoaderMock.load.returns(mochaOptions);107 // Act108 await sut.init();109 // Assert110 expect(mocha.asyncOnly).called;111 expect(mocha.ui).calledWith('exports');112 expect(mocha.grep).calledWith('grepme');113 });114 it('should force timeout off', async () => {115 mochaOptionsLoaderMock.load.returns({});116 await sut.init();117 expect(mochaAdapterMock.create).calledWithMatch({ timeout: 0 });118 });119 it('should not set asyncOnly if asyncOnly is false', async () => {120 // Arrange121 const mochaOptions = Object.freeze(122 createMochaOptions({123 'async-only': false,124 })125 );126 mochaOptionsLoaderMock.load.returns(mochaOptions);127 // Act128 await sut.init();129 expect(mocha.asyncOnly).not.called;130 });131 });132 describe(MochaTestRunner.prototype.dryRun.name, () => {133 let sut: MochaTestRunner;134 beforeEach(async () => {135 mochaOptionsLoaderMock.load.returns({});136 sut = createSut();137 await sut.init();138 });139 it('should load files the first time', async () => {140 await actDryRun();141 expect(mocha.loadFilesAsync).calledOnceWithExactly();142 expect(mocha.loadFilesAsync).calledBefore(mocha.run);143 });144 it('should not load files again the second time', async () => {145 await actDryRun();146 await actDryRun();147 expect(mocha.loadFilesAsync).calledOnceWithExactly();148 });149 it('should set bail to true when disableBail is false', async () => {150 const childSuite = sinon.createStubInstance(Mocha.Suite);151 mocha.suite.suites.push(childSuite);152 childSuite.suites = [];153 await actDryRun(factory.dryRunOptions({ disableBail: false }));154 expect(mocha.suite.bail).calledWith(true);155 expect(childSuite.bail).calledWith(true);156 });157 it('should set bail to false when disableBail is true', async () => {158 const childSuite = sinon.createStubInstance(Mocha.Suite);159 mocha.suite.suites.push(childSuite);160 childSuite.suites = [];161 await actDryRun(factory.dryRunOptions({ disableBail: true }));162 expect(mocha.suite.bail).calledWith(false);163 expect(childSuite.bail).calledWith(false);164 });165 it('should add a beforeEach hook if coverage analysis is "perTest"', async () => {166 const runPromise = sut.dryRun(factory.dryRunOptions({ coverageAnalysis: 'perTest' }));167 sut.beforeEach!({ currentTest: { fullTitle: () => 'foo should be bar' } } as Mocha.Context);168 mocha.run.callsArg(0);169 await runPromise;170 expect(sut.beforeEach).undefined;171 expect(global.__stryker2__?.currentTestId).eq('foo should be bar');172 });173 it('should not add a beforeEach hook if coverage analysis isn\'t "perTest"', async () => {174 const runPromise = sut.dryRun(factory.dryRunOptions({ coverageAnalysis: 'all' }));175 expect(sut.beforeEach).undefined;176 mocha.run.callsArg(0);177 await runPromise;178 });179 it('should collect mutant coverage', async () => {180 StrykerMochaReporter.currentInstance = reporterMock;181 reporterMock.tests = [];182 global.__stryker2__!.mutantCoverage = factory.mutantCoverage({ static: { 1: 2 } });183 const result = await actDryRun(factory.dryRunOptions({ coverageAnalysis: 'all' }));184 assertions.expectCompleted(result);185 expect(result.mutantCoverage).deep.eq(factory.mutantCoverage({ static: { 1: 2 } }));186 });187 it('should not collect mutant coverage if coverageAnalysis is "off"', async () => {188 StrykerMochaReporter.currentInstance = reporterMock;189 reporterMock.tests = [];190 global.__stryker2__!.mutantCoverage = factory.mutantCoverage({ static: { 1: 2 } });191 const result = await actDryRun(factory.dryRunOptions({ coverageAnalysis: 'off' }));192 assertions.expectCompleted(result);193 expect(result.mutantCoverage).undefined;194 });195 it('should result in the reported tests', async () => {196 const expectedTests = [factory.successTestResult(), factory.failedTestResult()];197 StrykerMochaReporter.currentInstance = reporterMock;198 reporterMock.tests = expectedTests;199 const result = await actDryRun(factory.dryRunOptions({ coverageAnalysis: 'off' }));200 assertions.expectCompleted(result);201 expect(result.tests).eq(expectedTests);202 });203 it("should result an error if the StrykerMochaReporter isn't set correctly", async () => {204 const result = await actDryRun(factory.dryRunOptions({ coverageAnalysis: 'off' }));205 assertions.expectErrored(result);206 expect(result.errorMessage).eq("Mocha didn't instantiate the StrykerMochaReporter correctly. Test result cannot be reported.");207 });208 async function actDryRun(options = factory.dryRunOptions()) {209 mocha.run.callsArg(0);210 return sut.dryRun(options);211 }212 });213 describe(MochaTestRunner.prototype.mutantRun.name, () => {214 let sut: MochaTestRunner;215 beforeEach(async () => {216 mochaOptionsLoaderMock.load.returns({});217 sut = createSut();218 await sut.init();219 StrykerMochaReporter.currentInstance = reporterMock;220 });221 it("should activate the given mutant statically when mutantActivation = 'static'", async () => {222 // Arrange223 const loadFilesAsyncTask = new Task();224 mocha.loadFilesAsync.returns(loadFilesAsyncTask.promise);225 // Act226 const onGoingAct = actMutantRun(227 factory.mutantRunOptions({ activeMutant: factory.mutantTestCoverage({ id: '42' }), mutantActivation: 'static' })228 );229 // Assert230 expect(global.__stryker2__?.activeMutant).eq('42');231 loadFilesAsyncTask.resolve();232 await tick();233 expect(global.__stryker2__?.activeMutant).eq('42');234 expect(mocha.run).called;235 await onGoingAct;236 });237 it("should activate the given mutant at runtime when mutantActivation = 'runtime'", async () => {238 // Arrange239 const loadFilesAsyncTask = new Task();240 mocha.loadFilesAsync.returns(loadFilesAsyncTask.promise);241 // Act242 const onGoingAct = actMutantRun(243 factory.mutantRunOptions({ activeMutant: factory.mutantTestCoverage({ id: '42' }), mutantActivation: 'runtime' })244 );245 // Assert246 expect(global.__stryker2__?.activeMutant).eq(undefined);247 loadFilesAsyncTask.resolve();248 await tick();249 expect(global.__stryker2__?.activeMutant).eq('42');250 expect(mocha.run).called;251 await onGoingAct;252 });253 it('should set bail to true when disableBail is false', async () => {254 const childSuite = sinon.createStubInstance(Mocha.Suite);255 mocha.suite.suites.push(childSuite);256 childSuite.suites = [];257 await actMutantRun(factory.mutantRunOptions({ disableBail: false }));258 expect(mocha.suite.bail).calledWith(true);259 expect(childSuite.bail).calledWith(true);260 });261 it('should set bail to false when disableBail is true', async () => {262 const childSuite = sinon.createStubInstance(Mocha.Suite);263 mocha.suite.suites.push(childSuite);264 childSuite.suites = [];265 await actMutantRun(factory.mutantRunOptions({ disableBail: true }));266 expect(mocha.suite.bail).calledWith(false);267 expect(childSuite.bail).calledWith(false);268 });269 it('should use `grep` to when the test filter is specified', async () => {270 await actMutantRun(factory.mutantRunOptions({ testFilter: ['foo should be bar', 'baz should be qux'] }));271 expect(mocha.grep).calledWith(new RegExp('(foo should be bar)|(baz should be qux)'));272 });273 it('should escape regex characters when filtering', async () => {274 await actMutantRun(factory.mutantRunOptions({ testFilter: ['should escape *.\\, but not /'] }));275 expect(mocha.grep).calledWith(new RegExp('(should escape \\*\\.\\\\, but not /)'));276 });277 it('should be able to report a killed mutant when a test fails', async () => {278 reporterMock.tests = [factory.successTestResult(), factory.failedTestResult({ id: 'foo should be bar', failureMessage: 'foo was baz' })];279 const result = await actMutantRun();280 const expectedResult: KilledMutantRunResult = {281 failureMessage: 'foo was baz',282 killedBy: ['foo should be bar'],283 status: MutantRunStatus.Killed,284 nrOfTests: 2,285 };286 expect(result).deep.eq(expectedResult);287 });288 it('should be able report a survived mutant when all test succeed', async () => {289 reporterMock.tests = [factory.successTestResult(), factory.successTestResult()];290 const result = await actMutantRun();291 assertions.expectSurvived(result);292 });293 it('should report a timeout when the hitLimit was reached', async () => {294 reporterMock.tests = [factory.failedTestResult()];295 const result = await actMutantRun(factory.mutantRunOptions({ hitLimit: 9 }), 10);296 assertions.expectTimeout(result);297 expect(result.reason).contains('Hit limit reached (10/9)');298 });299 it('should reset the hitLimit between runs', async () => {300 reporterMock.tests = [factory.failedTestResult()];301 const firstResult = await actMutantRun(factory.mutantRunOptions({ hitLimit: 9 }), 10);302 reporterMock.tests = [factory.failedTestResult()];303 const secondResult = await actMutantRun(factory.mutantRunOptions({ hitLimit: undefined }), 10);304 assertions.expectTimeout(firstResult);305 assertions.expectKilled(secondResult);306 });307 async function actMutantRun(options = factory.mutantRunOptions(), hitCount?: number) {308 mocha.run.callsArg(0);309 const result = sut.mutantRun(options);310 global.__stryker2__!.hitCount = hitCount;311 return result;312 }313 });314 describe(MochaTestRunner.prototype.dispose.name, () => {315 let sut: MochaTestRunner;316 beforeEach(async () => {317 mochaOptionsLoaderMock.load.returns({});318 sut = createSut();319 await sut.init();320 });321 it('should dispose of mocha', async () => {322 await sut.dispose();323 expect(mocha.dispose).called;324 });325 });...
Using AI Code Generation
1var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;2var files = loadFilesAsyncTask(['src/**/*.js', 'test/**/*.js']);3var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;4var files = loadFilesAsyncTask(['src/**/*.js', 'test/**/*.js']);5var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;6var files = loadFilesAsyncTask(['src/**/*.js', 'test/**/*.js']);7var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;8var files = loadFilesAsyncTask(['src/**/*.js', 'test/**/*.js']);9var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;10var files = loadFilesAsyncTask(['src/**/*.js', 'test/**/*.js']);11var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;12var files = loadFilesAsyncTask(['src/**/*.js', 'test/**/*.js']);13var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;14var files = loadFilesAsyncTask(['src/**/*.js', 'test/**/*.js']);15var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;16var files = loadFilesAsyncTask(['src/**/*.js', 'test/**/*.js']);17var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;18var files = loadFilesAsyncTask(['src/**/*.js', 'test/**/*.js']);19var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;20var files = loadFilesAsyncTask(['src/**/*.js', 'test/**/*.js']);21var loadFilesAsyncTask = require('stry
Using AI Code Generation
1var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;2var files = ['file1.js', 'file2.js'];3var fileContents = loadFilesAsyncTask(files).run();4var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;5var files = ['file1.js', 'file2.js'];6var fileContents = loadFilesAsyncTask(files).run();7var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;8var files = ['file1.js', 'file2.js'];9var fileContents = loadFilesAsyncTask(files).run();10var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;11var files = ['file1.js', 'file2.js'];12var fileContents = loadFilesAsyncTask(files).run();13var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;14var files = ['file1.js', 'file2.js'];15var fileContents = loadFilesAsyncTask(files).run();16var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;17var files = ['file1.js', 'file2.js'];18var fileContents = loadFilesAsyncTask(files).run();19var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;20var files = ['file1.js', 'file2.js'];21var fileContents = loadFilesAsyncTask(files).run();22var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;23var files = ['file1.js', 'file2.js'];24var fileContents = loadFilesAsyncTask(files).run();
Using AI Code Generation
1var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;2loadFilesAsyncTask('myFiles/**/*.js')3 .subscribe(function (files) {4 });5var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;6loadFilesAsyncTask('myFiles/**/*.js')7 .subscribe(function (files) {8 });9var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;10loadFilesAsyncTask('myFiles/**/*.js')11 .subscribe(function (files) {12 });13var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;14loadFilesAsyncTask('myFiles/**/*.js')15 .subscribe(function (files) {16 });17var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;18loadFilesAsyncTask('myFiles/**/*.js')19 .subscribe(function (files) {20 });21var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;22loadFilesAsyncTask('myFiles/**/*.js')23 .subscribe(function (files) {24 });25var loadFilesAsyncTask = require('stryker-parent').loadFilesAsyncTask;
Using AI Code Generation
1loadFilesAsyncTask({2});3loadFilesAsync({4});5loadFilesAsync({6});7loadFilesAsyncTask({8});9loadFilesAsyncTask({10});11loadFilesAsyncTask({12});13loadFilesAsyncTask({14});15loadFilesAsyncTask({16});17loadFilesAsyncTask({18});19loadFilesAsyncTask({20});21loadFilesAsyncTask({
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!!