Best JavaScript code snippet using stryker-parent
expression-mutant-placer.spec.ts
Source:expression-mutant-placer.spec.ts
1import { expect } from 'chai';2import { types, NodePath } from '@babel/core';3import { normalizeWhitespaces } from '@stryker-mutator/util';4import generate from '@babel/generator';5import { expressionMutantPlacer } from '../../../src/mutant-placers/expression-mutant-placer';6import { findNodePath, parseJS, parseTS } from '../../helpers/syntax-test-helpers';7import { Mutant } from '../../../src/mutant';8import { createMutant } from '../../helpers/factories';9describe('expressionMutantPlacer', () => {10 it('should have the correct name', () => {11 expect(expressionMutantPlacer.name).eq('expressionMutantPlacer');12 });13 describe(expressionMutantPlacer.canPlace.name, () => {14 it('should be true for an expression', () => {15 // Arrange16 const ast = parseJS('const foo = a + b');17 const binaryExpression = findNodePath(ast, (p) => p.isBinaryExpression());18 // Act19 const actual = expressionMutantPlacer.canPlace(binaryExpression);20 // Assert21 expect(actual).true;22 });23 it('should be false when the parent is tagged template expression', () => {24 // A templateLiteral is considered an expression, while it is not save to place a mutant there!25 const templateLiteral = findNodePath(parseJS('html`<p></p>`'), (p) => p.isTemplateLiteral());26 expect(expressionMutantPlacer.canPlace(templateLiteral)).false;27 });28 describe('object literals', () => {29 it('should be false when the expression is a key', () => {30 // A stringLiteral is considered an expression, while it is not save to place a mutant there!31 const stringLiteral = findNodePath(parseJS("const foo = { 'foo': bar }"), (p) => p.isStringLiteral());32 expect(expressionMutantPlacer.canPlace(stringLiteral)).false;33 });34 it('should be true when the expression is the value', () => {35 // A stringLiteral is considered an expression, while it is not save to place a mutant there!36 const stringLiteral = findNodePath(parseJS("const foo = { 'foo': bar }"), (p) => p.isIdentifier() && p.node.name === 'bar');37 expect(expressionMutantPlacer.canPlace(stringLiteral)).true;38 });39 });40 describe('chain expressions', () => {41 type ChainExpressionArrangement = [string, (path: NodePath) => boolean];42 const okPointers: ChainExpressionArrangement[] = [43 ['bar()', (p) => p.isCallExpression()],44 ['bar.foo', (p) => p.isMemberExpression()],45 ['bar.foo()', (p) => p.isCallExpression()],46 ['bar?.foo()', (p) => p.isOptionalCallExpression()],47 ['bar?.foo', (p) => p.isOptionalMemberExpression()],48 ['bar?.[1]', (p) => p.isOptionalMemberExpression()],49 ['bar[1]', (p) => p.isMemberExpression()],50 ['qux?.foo(bar());', (p) => p.isCallExpression() && types.isIdentifier(p.node.callee, { name: 'bar' })],51 ['foo(bar());', (p) => p.isCallExpression() && types.isIdentifier(p.node.callee, { name: 'bar' })],52 ['foo(bar.baz);', (p) => p.isMemberExpression() && types.isIdentifier(p.node.object, { name: 'bar' })],53 ];54 okPointers.forEach(([js, query]) => {55 it(`should allow placing in \`bar\` of \`${js}\``, () => {56 const path = findNodePath(parseJS(js), query);57 expect(expressionMutantPlacer.canPlace(path)).true;58 });59 });60 const falsePointers: ChainExpressionArrangement[] = [61 ['foo.bar.baz', (p) => p.isMemberExpression() && types.isIdentifier(p.node.property, { name: 'bar' })],62 ['foo.bar()', (p) => p.isMemberExpression() && types.isIdentifier(p.node.property, { name: 'bar' })],63 ['foo?.bar()', (p) => p.isOptionalMemberExpression() && types.isIdentifier(p.node.property, { name: 'bar' })],64 ['foo.bar?.baz', (p) => p.isMemberExpression() && types.isIdentifier(p.node.property, { name: 'bar' })],65 ['foo?.bar.baz', (p) => p.isOptionalMemberExpression() && types.isIdentifier(p.node.property, { name: 'bar' })],66 ['foo?.bar!.baz', (p) => p.isTSNonNullExpression()],67 ];68 falsePointers.forEach(([js, query]) => {69 it(`should not allow placing in \`bar\` of \`${js}\``, () => {70 const path = findNodePath(parseTS(js), query);71 expect(expressionMutantPlacer.canPlace(path)).false;72 });73 });74 });75 });76 describe(expressionMutantPlacer.place.name, () => {77 it('should be able to place a mutant on an expression', () => {78 // Arrange79 const { binaryExpression, appliedMutants, ast } = arrangeSingleMutant();80 // Act81 expressionMutantPlacer.place(binaryExpression, appliedMutants);82 const actualCode = normalizeWhitespaces(generate(ast).code);83 // Assert84 expect(actualCode).contains('const foo = stryMutAct_9fa48("1") ? bar >>> baz');85 });86 it('should place the original code as the alternative', () => {87 const { binaryExpression, appliedMutants, ast } = arrangeSingleMutant();88 expressionMutantPlacer.place(binaryExpression, appliedMutants);89 const actualAlternative = findNodePath<types.ConditionalExpression>(ast, (p) => p.isConditionalExpression()).node.alternate;90 const actualAlternativeCode = generate(actualAlternative).code;91 expect(actualAlternativeCode.endsWith('a + b'), `${actualAlternativeCode} did not end with "a + b"`).true;92 });93 it('should add mutant coverage syntax', () => {94 const { binaryExpression, appliedMutants, ast } = arrangeSingleMutant();95 expressionMutantPlacer.place(binaryExpression, appliedMutants);96 const actualAlternative = findNodePath<types.ConditionalExpression>(ast, (p) => p.isConditionalExpression()).node.alternate;97 const actualAlternativeCode = generate(actualAlternative).code;98 const expected = 'stryCov_9fa48("1"), a + b';99 expect(actualAlternativeCode.startsWith(expected), `${actualAlternativeCode} did not start with "${expected}"`).true;100 });101 it('should be able to place multiple mutants', () => {102 // Arrange103 const ast = parseJS('const foo = a + b');104 const binaryExpression = findNodePath<types.BinaryExpression>(ast, (p) => p.isBinaryExpression());105 const mutants = [106 createMutant({107 id: '52',108 original: binaryExpression.node,109 replacement: types.binaryExpression('-', types.identifier('bar'), types.identifier('baz')),110 }),111 createMutant({112 id: '659',113 original: binaryExpression.node,114 replacement: types.identifier('bar'),115 }),116 ];117 const appliedMutants = new Map<Mutant, types.BinaryExpression>([118 [mutants[0], mutants[0].applied(binaryExpression.node)],119 [mutants[1], mutants[1].applied(binaryExpression.node)],120 ] as const);121 // Act122 expressionMutantPlacer.place(binaryExpression, appliedMutants);123 const actualCode = normalizeWhitespaces(generate(ast).code);124 // Assert125 expect(actualCode).contains('const foo = stryMutAct_9fa48("659") ? bar : stryMutAct_9fa48("52") ? bar - baz');126 });127 function arrangeSingleMutant() {128 const ast = parseJS('const foo = a + b');129 const binaryExpression = findNodePath<types.BinaryExpression>(ast, (p) => p.isBinaryExpression());130 const mutant = new Mutant('1', 'file.js', binaryExpression.node, {131 replacement: types.binaryExpression('>>>', types.identifier('bar'), types.identifier('baz')),132 mutatorName: 'fooMutator',133 });134 const appliedMutants = new Map([[mutant, mutant.applied(binaryExpression.node)]]);135 return { binaryExpression, appliedMutants, ast };136 }137 /**138 * This describe has tests for anonymous classes and functions.139 * @see https://github.com/stryker-mutator/stryker-js/issues/2362140 */141 describe('anonymous expressions', () => {142 function arrangeActAssert(ast: types.File, expression: NodePath<types.Expression>, expectedMatch: RegExp) {143 const mutant = createMutant({144 id: '4',145 original: expression.node,146 replacement: types.identifier('bar'),147 });148 const appliedMutants = new Map([[mutant, mutant.applied(expression.node)]]);149 // Act150 expressionMutantPlacer.place(expression, appliedMutants);151 const actualCode = normalizeWhitespaces(generate(ast).code);152 // Assert153 expect(actualCode).matches(expectedMatch);154 }155 it('should set the name of an anonymous function expression', () => {156 // Arrange157 const ast = parseJS('const foo = function () { }');158 const functionExpression = findNodePath<types.FunctionExpression>(ast, (p) => p.isFunctionExpression());159 arrangeActAssert(ast, functionExpression, /const foo =.*function foo\(\) {}/);160 });161 it('should set the name of an anonymous method expression', () => {162 // Arrange163 const ast = parseJS('const foo = { bar: function () { } }');164 const functionExpression = findNodePath<types.FunctionExpression>(ast, (p) => p.isFunctionExpression());165 arrangeActAssert(ast, functionExpression, /const foo =.*bar:.*function bar\(\) {}/);166 });167 it('should not set the name if the statement is not a variable declaration', () => {168 // Arrange169 const ast = parseJS('foo.bar = function () { }');170 const functionExpression = findNodePath<types.FunctionExpression>(ast, (p) => p.isFunctionExpression());171 arrangeActAssert(ast, functionExpression, /foo\.bar =.*function \(\) {}/);172 });173 it('should not set the name of a named function expression', () => {174 // Arrange175 const ast = parseJS('const foo = function bar () { }');176 const functionExpression = findNodePath<types.FunctionExpression>(ast, (p) => p.isFunctionExpression());177 arrangeActAssert(ast, functionExpression, /const foo =.*function bar\(\) {}/);178 });179 it('should set the name of an anonymous class expression', () => {180 // Arrange181 const ast = parseJS('const Foo = class { }');182 const classExpression = findNodePath<types.ClassExpression>(ast, (p) => p.isClassExpression());183 arrangeActAssert(ast, classExpression, /const Foo =.*class Foo {}/);184 });185 it('should not override the name of a named class expression', () => {186 // Arrange187 const ast = parseJS('const Foo = class Bar { }');188 const classExpression = findNodePath<types.ClassExpression>(ast, (p) => p.isClassExpression());189 arrangeActAssert(ast, classExpression, /const Foo =.*class Bar {}/);190 });191 it('should set the name of an anonymous arrow function', () => {192 // Arrange193 const ast = parseJS('const bar = () => {}');194 const functionExpression = findNodePath<types.ArrowFunctionExpression>(ast, (p) => p.isArrowFunctionExpression());195 arrangeActAssert(ast, functionExpression, /const bar =.*\(\(\) => { const bar = \(\) => {}; return bar; }\)\(\)/);196 });197 });198 });...
Using AI Code Generation
1const strykerParent = require('stryker-parent');2console.log(strykerParent.actualAlternative());3const strykerParent = require('stryker-parent');4console.log(strykerParent.actualAlternative());5const strykerParent = require('stryker-parent');6console.log(strykerParent.actualAlternative());7const strykerParent = require('stryker-parent');8console.log(strykerParent.actualAlternative());9const strykerParent = require('stryker-parent');10console.log(strykerParent.actualAlternative());11const strykerParent = require('stryker-parent');12console.log(strykerParent.actualAlternative());13const strykerParent = require('stryker-parent');14console.log(strykerParent.actualAlternative());15const strykerParent = require('stryker-parent');16console.log(strykerParent.actualAlternative());17const strykerParent = require('stryker-parent');18console.log(strykerParent.actualAlternative());19const strykerParent = require('stryker-parent');20console.log(strykerParent.actualAlternative());21const strykerParent = require('stryker-parent');22console.log(strykerParent.actualAlternative());23const strykerParent = require('stryker-parent');24console.log(strykerParent.actualAlternative());
Using AI Code Generation
1var strykerParent = require('stryker-parent');2var actualAlternative = strykerParent.actualAlternative;3console.log(actualAlternative);4var strykerChild = require('stryker-child');5var actualAlternative = strykerChild.actualAlternative;6console.log(actualAlternative);
Using AI Code Generation
1const actualAlternative = require('stryker-parent').actualAlternative;2const actualAlternative = require('stryker-child').actualAlternative;3const actualAlternative = require('stryker-parent').actualAlternative;4const actualAlternative = require('stryker-child').actualAlternative;5const actualAlternative = require('stryker-parent').actualAlternative;6const actualAlternative = require('stryker-child').actualAlternative;7const actualAlternative = require('stryker-parent').actualAlternative;8const actualAlternative = require('stryker-child').actualAlternative;9const actualAlternative = require('stryker-parent').actualAlternative;10const actualAlternative = require('stryker-child').actualAlternative;11const actualAlternative = require('stryker-parent').actualAlternative;12const actualAlternative = require('stryker-child').actualAlternative;13const actualAlternative = require('stryker-parent').actualAlternative;14const actualAlternative = require('stryker-child').actualAlternative;15const actualAlternative = require('stryker-parent').actualAlternative;16const actualAlternative = require('stryker-child').actualAlternative;17const actualAlternative = require('stryker-parent').actualAlternative;
Using AI Code Generation
1import {actualAlternative} from 'stryker-parent';2actualAlternative();3import {alternative} from 'stryker-parent';4alternative();5import {actualAlternative} from 'stryker-parent';6actualAlternative();7import {actualAlternative} from 'stryker-parent';8actualAlternative();9import {actualAlternative} from 'stryker-parent';10actualAlternative();11import {actualAlternative} from 'stryker-parent';12actualAlternative();13import {actualAlternative} from 'stryker-parent';14actualAlternative();15import {actualAlternative} from 'stryker-parent';16actualAlternative();17import {actualAlternative} from 'stryker-parent';18actualAlternative();19import {actualAlternative} from 'stryker-parent';20actualAlternative();21import {actualAlternative} from 'stryker-parent';22actualAlternative();23import {actualAlternative} from 'stryker-parent';24actualAlternative();25import {actualAlternative} from 'stryker-parent';26actualAlternative();27import {actualAlternative} from 'stryker-parent';28actualAlternative();29import {actualAlternative} from 'stryker-parent';30actualAlternative();
Using AI Code Generation
1const strykerParent = require('stryker-parent');2const actualAlternative = strykerParent.actualAlternative;3const expectedAlternative = strykerParent.expectedAlternative;4const strykerChild = require('stryker-child');5const actualAlternative = strykerChild.actualAlternative;6const expectedAlternative = strykerChild.expectedAlternative;7const strykerChild2 = require('stryker-child-2');8const actualAlternative = strykerChild2.actualAlternative;9const expectedAlternative = strykerChild2.expectedAlternative;10const strykerChild3 = require('stryker-child-3');11const actualAlternative = strykerChild3.actualAlternative;12const expectedAlternative = strykerChild3.expectedAlternative;13const strykerChild4 = require('stryker-child-4');14const actualAlternative = strykerChild4.actualAlternative;15const expectedAlternative = strykerChild4.expectedAlternative;16const strykerChild5 = require('stryker-child-5');17const actualAlternative = strykerChild5.actualAlternative;18const expectedAlternative = strykerChild5.expectedAlternative;19const strykerChild6 = require('stryker-child-6');20const actualAlternative = strykerChild6.actualAlternative;21const expectedAlternative = strykerChild6.expectedAlternative;22const strykerChild7 = require('stryker-child-7');23const actualAlternative = strykerChild7.actualAlternative;24const expectedAlternative = strykerChild7.expectedAlternative;25const strykerChild8 = require('stryker-child-8');26const actualAlternative = strykerChild8.actualAlternative;27const expectedAlternative = strykerChild8.expectedAlternative;28const strykerChild9 = require('stryker-child-9');
Using AI Code Generation
1const actualAlternative = require('stryker-parent').actualAlternative;2const assert = require('assert');3describe('actualAlternative', () => {4 it('should return the actual alternative', () => {5 assert.equal(actualAlternative(), 'actual');6 });7});8const actualAlternative = require('stryker-parent').actualAlternative;9const assert = require('assert');10describe('actualAlternative', () => {11 it('should return the actual alternative', () => {12 assert.equal(actualAlternative(), 'actual');13 });14});15const actualAlternative = require('stryker-parent').actualAlternative;16const assert = require('assert');17describe('actualAlternative', () => {18 it('should return the actual alternative', () => {19 assert.equal(actualAlternative(), 'actual');20 });21});22const actualAlternative = require('stryker-parent').actualAlternative;23const assert = require('assert');24describe('actualAlternative', () => {25 it('should return the actual alternative', () => {26 assert.equal(actualAlternative(), 'actual');27 });28});29const actualAlternative = require('stryker-parent').actualAlternative;30const assert = require('assert');31describe('actualAlternative', () => {32 it('should return the actual alternative', () => {33 assert.equal(actualAlternative(), 'actual');34 });35});36const actualAlternative = require('stryker-parent').actualAlternative;37const assert = require('assert');38describe('actualAlternative', () => {39 it('should return the actual alternative', () => {40 assert.equal(actualAlternative(), 'actual');41 });42});43const actualAlternative = require('stryker-parent').actualAlternative;44const assert = require('assert');45describe('actualAlternative', () => {46 it('should return the actual alternative', () => {47 assert.equal(actualAlternative(), 'actual');48 });49});
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!!