Best JavaScript code snippet using stryker-parent
jasmine-test-runner.spec.ts
Source:jasmine-test-runner.spec.ts
1import sinon from 'sinon';2import { expect } from 'chai';3import { factory, assertions, testInjector, tick } from '@stryker-mutator/test-helpers';4import { TestStatus, CompleteDryRunResult, DryRunStatus, TestRunnerCapabilities } from '@stryker-mutator/api/test-runner';5import jasmine from 'jasmine';6import { MutantCoverage } from '@stryker-mutator/api/core';7import { Task } from '@stryker-mutator/util';8import * as pluginTokens from '../../src/plugin-tokens.js';9import { helpers } from '../../src/helpers.js';10import { JasmineTestRunner } from '../../src/index.js';11import { expectTestResultsToEqual } from '../helpers/assertions.js';12import { createEnvStub, createJasmineDoneInfo, createSpec, createSpecResult, createJasmineStartedInfo } from '../helpers/mock-factories.js';13describe(JasmineTestRunner.name, () => {14 let reporter: jasmine.CustomReporter;15 let jasmineStub: sinon.SinonStubbedInstance<jasmine>;16 let jasmineEnvStub: sinon.SinonStubbedInstance<jasmine.Env>;17 let sut: JasmineTestRunner;18 let clock: sinon.SinonFakeTimers;19 beforeEach(() => {20 jasmineStub = sinon.createStubInstance(jasmine);21 jasmineEnvStub = createEnvStub();22 jasmineStub.env = jasmineEnvStub as unknown as jasmine.Env;23 sinon.stub(helpers, 'createJasmine').returns(jasmineStub);24 clock = sinon.useFakeTimers();25 jasmineEnvStub.addReporter.callsFake((rep: jasmine.CustomReporter) => (reporter = rep));26 testInjector.options.jasmineConfigFile = 'jasmineConfFile';27 sut = testInjector.injector.provideValue(pluginTokens.globalNamespace, '__stryker2__' as const).injectClass(JasmineTestRunner);28 });29 describe('capabilities', () => {30 it('should communicate reloadEnvironment=false', () => {31 const expectedCapabilities: TestRunnerCapabilities = { reloadEnvironment: false };32 expect(sut.capabilities()).deep.eq(expectedCapabilities);33 });34 });35 describe('mutantRun', () => {36 it('should configure jasmine on run', async () => {37 await actEmptyMutantRun();38 expect(jasmineStub.execute).called;39 expect(helpers.createJasmine).calledWith({ projectBaseDir: process.cwd() });40 expect(jasmineStub.loadConfigFile).calledWith('jasmineConfFile');41 expect(jasmineStub.env.configure).calledWith({42 failFast: true,43 stopOnSpecFailure: true,44 oneFailurePerSpec: true,45 autoCleanClosures: false,46 random: false,47 stopSpecOnExpectationFailure: true,48 specFilter: sinon.match.func,49 });50 expect(jasmineStub.exitOnCompletion).eq(false);51 expect(jasmineStub.env.clearReporters).called;52 });53 it('should reuse the jasmine instance when mutantRun is called a second time', async () => {54 await actEmptyMutantRun();55 await actEmptyMutantRun();56 expect(helpers.createJasmine).calledOnce;57 expect(jasmineStub.loadConfigFile).calledOnce;58 });59 it('should filter tests based on testFilter', async () => {60 await actEmptyMutantRun(['1']);61 expect(jasmineEnvStub.configure).calledWithMatch({62 specFilter: sinon.match.func,63 });64 const actualSpecFilter: (spec: jasmine.Spec) => boolean = jasmineEnvStub.configure.getCall(0).args[0].specFilter!;65 expect(actualSpecFilter(createSpec({ id: '1' }))).true;66 expect(actualSpecFilter(createSpec({ id: '2' }))).false;67 });68 it('should not filter tests when testFilter is empty', async () => {69 await actEmptyMutantRun();70 expect(jasmineEnvStub.configure).calledWithMatch({71 specFilter: sinon.match.func,72 });73 const actualSpecFilter: (spec: jasmine.Spec) => boolean = jasmineEnvStub.configure.getCall(0).args[0].specFilter!;74 expect(actualSpecFilter(createSpec({ id: '1' }))).true;75 expect(actualSpecFilter(createSpec({ id: '2' }))).true;76 });77 it("should set the activeMutant on global scope statically when mutantActivation = 'static'", async () => {78 // Arrange79 const executeTask = new Task<jasmine.JasmineDoneInfo>();80 let customReporter: jasmine.CustomReporter | undefined;81 function addReporter(rep: jasmine.CustomReporter) {82 customReporter = rep;83 }84 jasmineEnvStub.addReporter.callsFake(addReporter);85 jasmineStub.execute.returns(executeTask.promise);86 // Act87 const onGoingAct = sut.mutantRun(factory.mutantRunOptions({ activeMutant: factory.mutant({ id: '23' }), mutantActivation: 'static' }));88 await tick();89 // Assert90 expect(global.__stryker2__?.activeMutant).eq('23');91 await customReporter!.jasmineStarted!(createJasmineStartedInfo());92 expect(global.__stryker2__?.activeMutant).eq('23');93 const doneInfo = createJasmineDoneInfo();94 await customReporter!.jasmineDone!(doneInfo);95 executeTask.resolve(doneInfo);96 await onGoingAct;97 });98 it("should set the activeMutant on global scope at runtime when mutantActivation = 'runtime'", async () => {99 // Arrange100 const executeTask = new Task<jasmine.JasmineDoneInfo>();101 let customReporter: jasmine.CustomReporter | undefined;102 function addReporter(rep: jasmine.CustomReporter) {103 customReporter = rep;104 }105 jasmineEnvStub.addReporter.callsFake(addReporter);106 jasmineStub.execute.returns(executeTask.promise);107 // Act108 const onGoingAct = sut.mutantRun(factory.mutantRunOptions({ activeMutant: factory.mutant({ id: '23' }), mutantActivation: 'runtime' }));109 await tick();110 // Assert111 expect(global.__stryker2__?.activeMutant).undefined;112 await customReporter!.jasmineStarted!(createJasmineStartedInfo());113 expect(global.__stryker2__?.activeMutant).eq('23');114 const doneInfo = createJasmineDoneInfo();115 await customReporter!.jasmineDone!(doneInfo);116 executeTask.resolve(doneInfo);117 await onGoingAct;118 });119 function actEmptyMutantRun(testFilter?: string[], activeMutant = factory.mutant(), sandboxFileName = 'sandbox/file') {120 let customReporter: jasmine.CustomReporter;121 function addReporter(rep: jasmine.CustomReporter) {122 customReporter = rep;123 }124 jasmineEnvStub.addReporter.callsFake(addReporter);125 jasmineStub.execute.callsFake(async () => {126 await customReporter.jasmineDone!(createJasmineDoneInfo());127 return createJasmineDoneInfo();128 });129 return sut.mutantRun(factory.mutantRunOptions({ activeMutant, testFilter, timeout: 2000, sandboxFileName }));130 }131 });132 describe('dryRun', () => {133 it('should time spec duration', async () => {134 // Arrange135 clock.setSystemTime(new Date(2010, 1, 1));136 jasmineStub.execute.callsFake(async () => {137 const spec = createSpecResult();138 await reporter.specStarted!(spec);139 clock.tick(10);140 await reporter.specDone!(spec);141 await reporter.jasmineDone!(createJasmineDoneInfo());142 return createJasmineDoneInfo();143 });144 // Act145 const result = await sut.dryRun(factory.dryRunOptions());146 // Assert147 assertions.expectCompleted(result);148 expect(result.tests[0].timeSpentMs).deep.eq(10);149 });150 it('should configure failFast: false when bail is disabled', async () => {151 // Arrange152 jasmineStub.execute.callsFake(async () => {153 await reporter.jasmineDone!(createJasmineDoneInfo());154 return createJasmineDoneInfo();155 });156 // Act157 await sut.dryRun(factory.dryRunOptions({ disableBail: true }));158 // Assert159 expect(jasmineEnvStub.configure).calledWithMatch(sinon.match({ failFast: false, stopOnSpecFailure: false }));160 });161 (['perTest', 'all'] as const).forEach((coverageAnalysis) =>162 it(`should report mutation coverage when coverage analysis is ${coverageAnalysis}`, async () => {163 // Arrange164 const expectedMutationCoverage: MutantCoverage = {165 perTest: {166 '1': { '0': 2, '1': 3 },167 },168 static: {},169 };170 global.__stryker2__!.mutantCoverage = expectedMutationCoverage;171 jasmineStub.execute.callsFake(async () => {172 await reporter.jasmineDone!(createJasmineDoneInfo());173 return createJasmineDoneInfo();174 });175 // Act176 const result = await sut.dryRun(factory.dryRunOptions({ coverageAnalysis }));177 // Assert178 const expectedResult: CompleteDryRunResult = {179 status: DryRunStatus.Complete,180 tests: [],181 mutantCoverage: expectedMutationCoverage,182 };183 expect(result).deep.eq(expectedResult);184 })185 );186 it('should not report mutation coverage when coverage analysis is "off"', async () => {187 // Arrange188 const expectedMutationCoverage = {189 perTest: {},190 static: {},191 };192 global.__stryker2__!.mutantCoverage = expectedMutationCoverage;193 jasmineStub.execute.callsFake(async () => {194 await reporter.jasmineDone!(createJasmineDoneInfo());195 return createJasmineDoneInfo();196 });197 // Act198 const result = await sut.dryRun(factory.dryRunOptions({ coverageAnalysis: 'off' }));199 // Assert200 const expectedResult: CompleteDryRunResult = {201 status: DryRunStatus.Complete,202 tests: [],203 mutantCoverage: undefined,204 };205 expect(result).deep.eq(expectedResult);206 });207 it('set current test id between specs when coverageAnalysis = "perTest"', async () => {208 // Arrange209 let firstCurrentTestId: string | undefined;210 let secondCurrentTestId: string | undefined;211 jasmineStub.execute.callsFake(async () => {212 const spec0 = createSpecResult({ id: 'spec0' });213 const spec1 = createSpecResult({ id: 'spec23' });214 await reporter.specStarted!(spec0);215 firstCurrentTestId = global.__stryker2__!.currentTestId;216 await reporter.specDone!(spec0);217 await reporter.specStarted!(spec1);218 secondCurrentTestId = global.__stryker2__!.currentTestId;219 await reporter.specDone!(spec1);220 await reporter.jasmineDone!(createJasmineDoneInfo());221 return createJasmineDoneInfo();222 });223 // Act224 await sut.dryRun(factory.dryRunOptions({ coverageAnalysis: 'perTest' }));225 // Assert226 expect(firstCurrentTestId).eq('spec0');227 expect(secondCurrentTestId).eq('spec23');228 });229 it('not set current test id between specs when coverageAnalysis = "all"', async () => {230 // Arrange231 let firstCurrentTestId: string | undefined;232 jasmineStub.execute.callsFake(async () => {233 const spec0 = createSpecResult({ id: 'spec0' });234 await reporter.specStarted!(spec0);235 firstCurrentTestId = global.__stryker2__!.currentTestId;236 await reporter.specDone!(spec0);237 await reporter.jasmineDone!(createJasmineDoneInfo());238 return createJasmineDoneInfo();239 });240 // Act241 await sut.dryRun(factory.dryRunOptions({ coverageAnalysis: 'all' }));242 // Assert243 expect(firstCurrentTestId).undefined;244 });245 it('should report completed specs', async () => {246 // Arrange247 jasmineStub.execute.callsFake(async () => {248 await reporter.specDone!(createSpecResult({ id: 'spec0', fullName: 'foo spec', status: 'success', description: 'string' }));249 await reporter.specDone!(250 createSpecResult({251 id: 'spec1',252 fullName: 'bar spec',253 status: 'failure',254 failedExpectations: [{ actual: 'foo', expected: 'bar', matcherName: 'fooMatcher', passed: false, message: 'bar failed', stack: 'stack' }],255 description: 'string',256 })257 );258 await reporter.specDone!(createSpecResult({ id: 'spec2', fullName: 'disabled', status: 'disabled', description: 'string' }));259 await reporter.specDone!(createSpecResult({ id: 'spec3', fullName: 'pending', status: 'pending', description: 'string' }));260 await reporter.specDone!(createSpecResult({ id: 'spec4', fullName: 'excluded', status: 'excluded', description: 'string' }));261 await reporter.jasmineDone!(createJasmineDoneInfo());262 return createJasmineDoneInfo();263 });264 // Act265 const result = await sut.dryRun(factory.dryRunOptions());266 // Assert267 assertions.expectCompleted(result);268 expectTestResultsToEqual(result.tests, [269 { id: 'spec0', name: 'foo spec', status: TestStatus.Success },270 { id: 'spec1', name: 'bar spec', status: TestStatus.Failed, failureMessage: 'bar failed' },271 { id: 'spec2', name: 'disabled', status: TestStatus.Skipped },272 { id: 'spec3', name: 'pending', status: TestStatus.Skipped },273 { id: 'spec4', name: 'excluded', status: TestStatus.Skipped },274 ]);275 });276 it('should report errors on run', async () => {277 const error = new Error('foobar');278 jasmineStub.execute.throws(error);279 const result = await sut.dryRun(factory.dryRunOptions());280 assertions.expectErrored(result);281 expect(result.errorMessage)282 .matches(/An error occurred.*/)283 .and.matches(/.*Error: foobar.*/);284 });285 it('should throw when the reporter doesn\'t report "jasmineDone"', async () => {286 jasmineStub.execute.resolves(createJasmineDoneInfo());287 // Act288 const actualResult = await sut.dryRun(factory.dryRunOptions());289 // Assert290 assertions.expectErrored(actualResult);291 expect(actualResult.errorMessage).contains('Jasmine reporter didn\'t report "jasmineDone", this shouldn\'t happen');292 });293 });...
setup.js
Source:setup.js
...18 var loadJasmine = function load_jasmine() {19 var jasmine = window.document.createElement("script");20 jasmine.src = "jasmine/jasmine.js";21 jasmine.onload = function () {22 createJasmine(window);23 loadSource();24 };25 window.document.body.appendChild(jasmine);26 },27 loadSource = function load_source() {28 var source = window.document.createElement("script");29 source.src = "src/calculator.js";30 source.onload = function () {31 loadSpecs();32 };33 window.document.body.appendChild(source);34 },35 loadSpecs = function load_specs() {36 var specs = window.document.createElement("script");37 specs.src = "spec/calculator.spec.js";38 specs.onload = function () {39 execute();40 };41 window.document.body.appendChild(specs);42 },43 execute = function execute() {44 global.env.execute();45 done();46 };47 loadJasmine();48};49function createJasmine(window) {50 var jasmineRequire = window.jasmineRequire,51 jasmine = jasmineRequire.core(jasmineRequire);52 global.env = jasmine.getEnv();53 var jasmineInterface = jasmineRequire.interface(jasmine, global.env);54 global.reporter = new reporter();55 global.env.addReporter(global.reporter);56 extend(window, jasmineInterface);57}58function extend(destination, source) {59 for (var property in source) destination[property] = source[property];60 return destination;...
helpers.ts
Source:helpers.ts
...24 failureMessage: specResult.failedExpectations.map((failedExpectation) => failedExpectation.message).join(','),25 };26 }27 },28 createJasmine(options: jasmine.JasmineOptions): jasmine {29 return new jasmine(options);30 },...
Using AI Code Generation
1var Jasmine = require('stryker-parent').createJasmine();2var jasmine = new Jasmine();3jasmine.loadConfigFile('spec/support/jasmine.json');4jasmine.execute();5{6}7describe('Calculator', function() {8 it('should add 1 + 1', function() {9 expect(1 + 1).toBe(2);10 });11});12module.exports = function Calculator() {13 this.add = function(a, b) {14 return a + b;15 };16};17var Calculator = require('../../src/Calculator');18describe('Calculator', function() {19 it('should add 1 + 1', function() {20 var calculator = new Calculator();21 expect(calculator.add(1, 1)).toBe(2);22 });23});24module.exports = function Calculator() {25 this.add = function(a, b) {26 return a + b;27 };28};29var Calculator = require('../../src/Calculator');30describe('Calculator', function() {31 it('should add 1 + 1', function() {32 var calculator = new Calculator();33 expect(calculator.add(1, 1)).toBe(2);34 });35});36module.exports = function Calculator() {37 this.add = function(a, b) {38 return a + b;39 };40};41var Calculator = require('../../src/Calculator');42describe('Calculator', function() {43 it('should add 1 + 1', function() {44 var calculator = new Calculator();45 expect(calculator.add(1, 1)).toBe(2);46 });47});48module.exports = function Calculator() {49 this.add = function(a, b) {50 return a + b;51 };52};53var Calculator = require('../../src/Calculator');54describe('Calculator', function() {55 it('should add 1 + 1', function() {56 var calculator = new Calculator();57 expect(calculator.add(
Using AI Code Generation
1var stryker = require('stryker');2var jasmine = stryker.createJasmine();3jasmine.loadConfig({4});5jasmine.execute();6describe('Calculator', function() {7 it('should be able to add two numbers', function() {8 expect(1 + 1).toBe(2);9 });10});11describe('Calculator', function() {12 it('should be able to add two numbers', function() {13 expect(1 + 1).toBe(3);14 });15});16describe('Calculator', function() {17 it('should be able to add two numbers', function() {18 expect(1 + 1).toBe(2);19 });20});21describe('Calculator', function() {22 it('should be able to add two numbers', function() {23 expect(1 + 1).toBe(3);24 });25});26describe('Calculator', function() {27 it('should be able to add two numbers', function() {28 expect(1 + 1).toBe(2);29 });30});31describe('Calculator', function() {32 it('should be able to add two numbers', function() {33 expect(1 + 1).toBe(3);34 });35});36describe('Calculator', function() {37 it('should be able to add two numbers', function() {38 expect(1 + 1).toBe(2);39 });40});41describe('Calculator', function() {42 it('should be able to add two numbers', function() {43 expect(1 + 1).toBe(3);44 });45});46describe('Calculator', function() {47 it('should be able to add two numbers', function() {48 expect(1 + 1).toBe(2);49 });50});51describe('Calculator', function() {52 it('should be able to add two numbers', function
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!!