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 } from '@stryker-mutator/test-helpers';4import sinon from 'sinon';5import { KilledMutantRunResult, MutantRunStatus } from '@stryker-mutator/api/test-runner';6import { DirectoryRequireCache } from '@stryker-mutator/util';7import { MochaTestRunner } from '../../src/mocha-test-runner';8import { StrykerMochaReporter } from '../../src/stryker-mocha-reporter';9import { MochaAdapter } from '../../src/mocha-adapter';10import * as pluginTokens from '../../src/plugin-tokens';11import { MochaOptionsLoader } from '../../src/mocha-options-loader';12import { createMochaOptions } from '../helpers/factories';13describe(MochaTestRunner.name, () => {14 let directoryRequireCacheMock: sinon.SinonStubbedInstance<DirectoryRequireCache>;15 let mocha: sinon.SinonStubbedInstance<Mocha> & { suite: sinon.SinonStubbedInstance<Mocha.Suite>; dispose?: sinon.SinonStub };16 let mochaAdapterMock: sinon.SinonStubbedInstance<MochaAdapter>;17 let mochaOptionsLoaderMock: sinon.SinonStubbedInstance<MochaOptionsLoader>;18 let reporterMock: sinon.SinonStubbedInstance<StrykerMochaReporter>;19 beforeEach(() => {20 reporterMock = sinon.createStubInstance(StrykerMochaReporter);21 directoryRequireCacheMock = sinon.createStubInstance(DirectoryRequireCache);22 reporterMock.tests = [];23 mochaAdapterMock = sinon.createStubInstance(MochaAdapter);24 mochaOptionsLoaderMock = sinon.createStubInstance(MochaOptionsLoader);25 mocha = sinon.createStubInstance(Mocha) as any;26 mocha.suite = sinon.createStubInstance(Mocha.Suite) as any;27 mochaAdapterMock.create.returns(mocha as unknown as Mocha);28 });29 afterEach(() => {30 // These keys can be used to test the nodejs cache31 delete StrykerMochaReporter.log;32 });33 function createSut(): MochaTestRunner {34 return testInjector.injector35 .provideValue(pluginTokens.mochaAdapter, mochaAdapterMock)36 .provideValue(pluginTokens.loader, mochaOptionsLoaderMock)37 .provideValue(pluginTokens.directoryRequireCache, directoryRequireCacheMock)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(MochaTestRunner.prototype.init.name, () => {48 let sut: MochaTestRunner;49 beforeEach(() => {50 sut = createSut();51 });52 it('should load mocha options', async () => {53 mochaOptionsLoaderMock.load.returns({});54 await sut.init();55 expect(mochaOptionsLoaderMock.load).calledWithExactly(testInjector.options);56 });57 it('should collect the files', async () => {58 const expectedTestFileNames = ['foo.js', 'foo.spec.js'];59 const mochaOptions = Object.freeze(createMochaOptions());60 mochaOptionsLoaderMock.load.returns(mochaOptions);61 mochaAdapterMock.collectFiles.returns(expectedTestFileNames);62 await sut.init();63 expect(mochaAdapterMock.collectFiles).calledWithExactly(mochaOptions);64 expect(sut.testFileNames).eq(expectedTestFileNames);65 });66 it('should not handle requires when there are no `requires`', async () => {67 mochaOptionsLoaderMock.load.returns({});68 await sut.init();69 expect(mochaAdapterMock.handleRequires).not.called;70 });71 it('should handle requires and collect root hooks', async () => {72 const requires = ['test/setup.js'];73 // eslint-disable-next-line @typescript-eslint/no-empty-function74 const expectedRootHooks = { beforeEach() {} };75 mochaOptionsLoaderMock.load.returns(createMochaOptions({ require: requires }));76 mochaAdapterMock.handleRequires.resolves(expectedRootHooks);77 await sut.init();78 expect(sut.rootHooks).eq(expectedRootHooks);79 });80 it('should reject when requires contains "esm" (see #3014)', async () => {81 const requires = ['esm', 'ts-node/require'];82 mochaOptionsLoaderMock.load.returns(createMochaOptions({ require: requires }));83 await expect(sut.init()).rejectedWith(84 '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.'85 );86 });87 });88 describe(MochaTestRunner.prototype.dryRun.name, () => {89 let sut: MochaTestRunner;90 let testFileNames: string[];91 beforeEach(() => {92 testFileNames = [];93 sut = createSut();94 sut.testFileNames = testFileNames;95 sut.mochaOptions = {};96 });97 it('should pass along supported options to mocha', async () => {98 // Arrange99 testFileNames.push('foo.js', 'bar.js', 'foo2.js');100 sut.mochaOptions['async-only'] = true;101 sut.mochaOptions.grep = 'grepme';102 sut.mochaOptions.opts = 'opts';103 sut.mochaOptions.require = [];104 sut.mochaOptions.ui = 'exports';105 // Act106 await actDryRun();107 // Assert108 expect(mocha.asyncOnly).called;109 expect(mocha.ui).calledWith('exports');110 expect(mocha.grep).calledWith('grepme');111 });112 it('should force timeout off', async () => {113 await actDryRun();114 expect(mochaAdapterMock.create).calledWithMatch({ timeout: false });115 });116 it('should set bail to true when disableBail is false', async () => {117 await actDryRun(factory.dryRunOptions({ disableBail: false }));118 expect(mochaAdapterMock.create).calledWithMatch({ bail: true });119 });120 it('should set bail to false when disableBail is true', async () => {121 await actDryRun(factory.dryRunOptions({ disableBail: true }));122 expect(mochaAdapterMock.create).calledWithMatch({ bail: false });123 });124 it("should don't set asyncOnly if asyncOnly is false", async () => {125 sut.mochaOptions['async-only'] = false;126 await actDryRun();127 expect(mocha.asyncOnly).not.called;128 });129 it('should pass rootHooks to the mocha instance', async () => {130 // eslint-disable-next-line @typescript-eslint/no-empty-function131 const rootHooks = { beforeEach() {} };132 sut.rootHooks = rootHooks;133 await actDryRun();134 expect(mochaAdapterMock.create).calledWithMatch({ rootHooks });135 });136 it('should add collected files ', async () => {137 sut.testFileNames!.push('foo.js', 'bar.js', 'foo2.js');138 await actDryRun();139 expect(mocha.addFile).calledThrice;140 expect(mocha.addFile).calledWith('foo.js');141 expect(mocha.addFile).calledWith('foo2.js');142 expect(mocha.addFile).calledWith('bar.js');143 });144 it('should add a beforeEach hook if coverage analysis is "perTest"', async () => {145 testFileNames.push('');146 await actDryRun(factory.dryRunOptions({ coverageAnalysis: 'perTest' }));147 expect(mocha.suite.beforeEach).calledWithMatch('StrykerIntercept', sinon.match.func);148 mocha.suite.beforeEach.callArgOnWith(1, { currentTest: { fullTitle: () => 'foo should be bar' } });149 expect(global.__stryker2__?.currentTestId).eq('foo should be bar');150 });151 it('should not add a beforeEach hook if coverage analysis isn\'t "perTest"', async () => {152 testFileNames.push('');153 await actDryRun(factory.dryRunOptions({ coverageAnalysis: 'all' }));154 expect(mocha.suite.beforeEach).not.called;155 });156 it('should collect mutant coverage', async () => {157 testFileNames.push('');158 StrykerMochaReporter.currentInstance = reporterMock;159 reporterMock.tests = [];160 global.__stryker2__!.mutantCoverage = factory.mutantCoverage({ static: { 1: 2 } });161 const result = await actDryRun(factory.dryRunOptions({ coverageAnalysis: 'all' }));162 assertions.expectCompleted(result);163 expect(result.mutantCoverage).deep.eq(factory.mutantCoverage({ static: { 1: 2 } }));164 });165 it('should not collect mutant coverage if coverageAnalysis is "off"', async () => {166 testFileNames.push('');167 StrykerMochaReporter.currentInstance = reporterMock;168 reporterMock.tests = [];169 global.__stryker2__!.mutantCoverage = factory.mutantCoverage({ static: { 1: 2 } });170 const result = await actDryRun(factory.dryRunOptions({ coverageAnalysis: 'off' }));171 assertions.expectCompleted(result);172 expect(result.mutantCoverage).undefined;173 });174 it('should result in the reported tests', async () => {175 testFileNames.push('');176 const expectedTests = [factory.successTestResult(), factory.failedTestResult()];177 StrykerMochaReporter.currentInstance = reporterMock;178 reporterMock.tests = expectedTests;179 const result = await actDryRun(factory.dryRunOptions({ coverageAnalysis: 'off' }));180 assertions.expectCompleted(result);181 expect(result.tests).eq(expectedTests);182 });183 it("should result an error if the StrykerMochaReporter isn't set correctly", async () => {184 testFileNames.push('');185 const result = await actDryRun(factory.dryRunOptions({ coverageAnalysis: 'off' }));186 assertions.expectErrored(result);187 expect(result.errorMessage).eq("Mocha didn't instantiate the StrykerMochaReporter correctly. Test result cannot be reported.");188 });189 it('should collect and purge the requireCache between runs', async () => {190 // Arrange191 testFileNames.push('');192 // Act193 await actDryRun(factory.dryRunOptions());194 // Assert195 expect(directoryRequireCacheMock.clear).called;196 expect(directoryRequireCacheMock.record).called;197 expect(directoryRequireCacheMock.clear).calledBefore(directoryRequireCacheMock.record);198 });199 it('should dispose of mocha when it supports it', async () => {200 await actDryRun();201 expect(mocha.dispose).called;202 });203 async function actDryRun(options = factory.dryRunOptions()) {204 mocha.run.onFirstCall().callsArg(0);205 return sut.dryRun(options);206 }207 });208 describe(MochaTestRunner.prototype.mutantRun.name, () => {209 let sut: MochaTestRunner;210 beforeEach(() => {211 sut = createSut();212 sut.testFileNames = [];213 sut.mochaOptions = {};214 StrykerMochaReporter.currentInstance = reporterMock;215 });216 it('should activate the given mutant', async () => {217 await actMutantRun(factory.mutantRunOptions({ activeMutant: factory.mutant({ id: '42' }) }));218 expect(global.__stryker2__?.activeMutant).eq('42');219 });220 it('should set bail to false when disableBail is true', async () => {221 await actMutantRun(factory.mutantRunOptions({ disableBail: true }));222 expect(mochaAdapterMock.create).calledWithMatch({ bail: false });223 });224 it('should set bail to true when disableBail is false', async () => {225 await actMutantRun(factory.mutantRunOptions({ disableBail: false }));226 expect(mochaAdapterMock.create).calledWithMatch({ bail: true });227 });228 it('should use `grep` to when the test filter is specified', async () => {229 await actMutantRun(factory.mutantRunOptions({ testFilter: ['foo should be bar', 'baz should be qux'] }));230 expect(mocha.grep).calledWith(new RegExp('(foo should be bar)|(baz should be qux)'));231 });232 it('should escape regex characters when filtering', async () => {233 await actMutantRun(factory.mutantRunOptions({ testFilter: ['should escape *.\\, but not /'] }));234 expect(mocha.grep).calledWith(new RegExp('(should escape \\*\\.\\\\, but not /)'));235 });236 it('should be able to report a killed mutant when a test fails', async () => {237 reporterMock.tests = [factory.successTestResult(), factory.failedTestResult({ id: 'foo should be bar', failureMessage: 'foo was baz' })];238 const result = await actMutantRun();239 const expectedResult: KilledMutantRunResult = {240 failureMessage: 'foo was baz',241 killedBy: ['foo should be bar'],242 status: MutantRunStatus.Killed,243 nrOfTests: 2,244 };245 expect(result).deep.eq(expectedResult);246 });247 it('should be able report a survived mutant when all test succeed', async () => {248 reporterMock.tests = [factory.successTestResult(), factory.successTestResult()];249 const result = await actMutantRun();250 assertions.expectSurvived(result);251 });252 it('should report a timeout when the hitLimit was reached', async () => {253 reporterMock.tests = [factory.failedTestResult()];254 const result = await actMutantRun(factory.mutantRunOptions({ hitLimit: 9 }), 10);255 assertions.expectTimeout(result);256 expect(result.reason).contains('Hit limit reached (10/9)');257 });258 it('should reset the hitLimit between runs', async () => {259 reporterMock.tests = [factory.failedTestResult()];260 const firstResult = await actMutantRun(factory.mutantRunOptions({ hitLimit: 9 }), 10);261 reporterMock.tests = [factory.failedTestResult()];262 const secondResult = await actMutantRun(factory.mutantRunOptions({ hitLimit: undefined }), 10);263 assertions.expectTimeout(firstResult);264 assertions.expectKilled(secondResult);265 });266 async function actMutantRun(options = factory.mutantRunOptions(), hitCount?: number) {267 mocha.run.callsArg(0);268 const result = sut.mutantRun(options);269 global.__stryker2__!.hitCount = hitCount;270 return result;271 }272 });...
Using AI Code Generation
1var expectedRootHooks = require('stryker-parent').expectedRootHooks;2var expectedChildHooks = require('stryker-parent').expectedChildHooks;3var expectedChildHooks = require('stryker-parent').expectedChildHooks;4var expectedChildHooks = require('stryker-parent').expectedChildHooks;5var expectedRootHooks = require('stryker-parent').expectedRootHooks;6var expectedChildHooks = require('stryker-parent').expectedChildHooks;7var expectedRootHooks = require('stryker-parent').expectedRootHooks;8var expectedChildHooks = require('stryker-parent').expectedChildHooks;9var expectedChildHooks = require('stryker-parent').expectedChildHooks;10var expectedChildHooks = require('stryker-parent').expectedChildHooks;11var expectedRootHooks = require('stryker-parent').expectedRootHooks;12var expectedChildHooks = require('stryker-parent').expectedChildHooks;13var expectedChildHooks = require('stryker-parent').expectedChildHooks;14var expectedChildHooks = require('stryker-parent').expectedChildHooks;15var expectedChildHooks = require('stryker-parent').expectedChildHooks;16var expectedRootHooks = require('stryker-parent').expectedRootHooks;
Using AI Code Generation
1const expectedRootHooks = require('stryker-parent').expectedRootHooks;2const expectedRootHooks = require('stryker-parent').expectedRootHooks;3const expectedRootHooks = require('stryker-parent').expectedRootHooks;4const expectedRootHooks = require('stryker-parent').expectedRootHooks;5const expectedRootHooks = require('stryker-parent').expectedRootHooks;6const expectedRootHooks = require('stryker-parent').expectedRootHooks;7const expectedRootHooks = require('stryker-parent').expectedRootHooks;8const expectedRootHooks = require('stryker-parent').expectedRootHooks;9const expectedRootHooks = require('stryker-parent').expectedRootHooks;10const expectedRootHooks = require('stryker-parent').expectedRootHooks;11const expectedRootHooks = require('stryker-parent').expectedRootHooks;12const expectedRootHooks = require('stryker-parent').expectedRootHooks;13const expectedRootHooks = require('stryker-parent').expectedRootHooks;14const expectedRootHooks = require('stryker-parent').expectedRootHooks;15const expectedRootHooks = require('stryker-parent').expectedRootHooks;
Using AI Code Generation
1const {expectedRootHooks} = require('stryker-parent');2module.exports = function(config) {3 config.set({4 });5 expectedRootHooks(config);6};7module.exports = function(config) {8 config.set({9 });10 require('./test.js')(config);11};12{13 "scripts": {14 }15}16module.exports = {17 parserOptions: {18 },19 env: {20 },21 rules: {22 }23};24{25}
Using AI Code Generation
1const expectedRootHooks = require('stryker-parent').expectedRootHooks;2console.log(expectedRootHooks);3module.exports = {4 expectedRootHooks: function () {5 return 'expectedRootHooks';6 }7}8{9}
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!!