string-literal
'EUR' '' This mutator empties a string or the text of a template literal: 'EUR' becomes
''. An empty string gets the text mootant instead.
Tests catch these mutants
by checking the whole result with input that makes the string appear in it, such
as two coupon codes joined with ', '.
| Change | Code | Mutant |
|---|---|---|
Empty a string or a template without ${} |
' |
'' |
| Fill an empty string or template | '' |
'mootant' |
| Empty a template that only reads variables and properties | |
'' |
Keep only the ${} of a template with a call or a calculation |
`${price.toFixed(2)} |
`${price.toFixed(2)}` |
Mootant skips strings in error and log messages and in comparisons such as
status === 'paid'; see Skipped changes. A log template
with more than a variable name in ${}, such as ${cart.id}, still gets a
mutant.
By default, Mootant also skips every quoted value of a JSX attribute, those of
type, href, aria-* and name included: "password" in type="password"
gets no mutant. A value in braces, such as '/reset' in href={'/reset'}, gets
one.
With the caseTests option, on in strict, Mootant also changes
the strings of case labels. With jsxAttributes, which
strict leaves off, it also changes the quoted values of JSX attributes.
Check the whole result with input that makes the string appear in it. Take this function:
export function formatCoupons(codes: string[]): string {
return codes.join(', ')
return codes.join('')
}A test with one coupon code does not catch ' → , ''': a list of one has no
separator, so both versions return the code alone. Use two codes:
it('separates the codes with a comma', () => {
expect(formatCoupons(['SAVE10', 'FREESHIP'])).toBe('SAVE10, FREESHIP')
})With the mutant, formatCoupons(['SAVE10', 'FREESHIP']) returns
'SAVE10FREESHIP'.
What to check for the other changes:
- A template that keeps its
${}, such as`${price.toFixed(2)} EUR`: the whole result withtoBe, nottoContain. ''→'mootant': two or more parts inparts.join(''), ortoBe('')on a value that must stay empty.- A
caselabel, withcaseTests: the value of the label, such as'paid', and what its branch returns. - A quoted JSX attribute value, with
jsxAttributes: the attribute of the rendered element, such as thetypeof a password field, or a query that finds the element by it, such asgetByLabelText('Password').
Turn the mutator off in the configuration:
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'string-literal': 'off',
},
})To ignore the mutants of this mutator on one line, put an ignore comment above it:
// mootant-ignore-next-line string-literal, statement-removal: styling only
button.classList.add('is-loading')The comment also names statement-removal: without it, the removal of the call comes back as a mutant; see Ignore mutants.
With strict, 'string-literal': 'on' turns caseTests back off; see
Mutators.
false by default, true in strict. With it, Mootant also changes the
strings of case labels: ' → paid''' in case 'paid':.
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'string-literal': ['on', { caseTests: true }],
},
})false by default, and strict leaves it off: in braces, a value can change
what some code does with no mutant on, as shown below. With it, Mootant also
changes the quoted values of JSX attributes and component props: " →
password""" in type="password", and "" → "mootant" in href="".
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'string-literal': ['on', { jsxAttributes: true }],
},
})The values of className, class, style and data-testid stay skipped:
Mootant guesses from the name that they only change how an element looks or how
a test finds it. To switch a mutant on, Mootant moves the value into braces, so
it also skips the values that would mean something else there:
- A value with
\, a tab, a line break or a character reference such as&. - The value of an attribute that Solid reads another way once the value is in
braces. Mootant can’t tell which framework compiles a file, so it skips these
in React code too:
valueand the boolean attributes of a built-in element, such asdisabled, and every attribute of a custom element: Solid sets them as properties, and thedisabledproperty reads""as false.- A name that starts with
on, such asonclick, which becomes an event handler, andref,children,classList,innerHTML,innerTextandtextContent. - The attributes of Solid’s namespaces, such as
bool:disabled, where a"0"in braces counts as true.
Some mutants of this option need a particular test:
- An emptied value that the attribute has by default, such as
type="submit"on a<button>, changes nothing a user sees, so only a test of the attribute itself, such as a snapshot, catches it. - A changed drawing attribute of SVG, such as
dorfill, only changes the picture, which a snapshot test catches. - A changed
keythat only React reads survives every test, unless another element in its place has the new key:"→a"""inflag ? <Counter key="a" /> : <Counter key="" />keeps the state ofCounterwhenflagchanges.
Some code treats a value in braces another way, which these skips can’t see:
- Solid sets a value in braces only after the element is created and its refs
and
use:directives have run, soref={el => record(el.getAttribute('type'))}finds notype. - Build plugins and macros that read a quoted value as written, such as
twin.macro for
tw, Lingui for<Trans>and<Plural>and formatjs for<FormattedMessage>, reject the value in braces or compile it another way.
With no mutant on, such code can then work differently: a test that notices
fails, and the mutants of the file end with an error. Keep jsxAttributes off
for such code, or ignore its string-literal mutants.
Mootant leaves out these changes. npx mootant list src --skipped shows them
with the category and the rule; Skipped changes
explains the categories.
| Change | Reason |
|---|---|
'Stock cannot be negative'→'' | Text of an error message, such as in new Error()noiseerror-message |
'Cart saved'→'' | Text of a log message, such as in console.log()noiselog-message |
'paid'→'' in status === 'paid' | Literal in a comparison, left to comparison-operatorsubsumedO4 |
"password"→"" in type="password" | By default, every quoted JSX attribute value, type, href, aria-* and name included; a value in {} gets mutants; see jsxAttributesnoisejsx-attribute |
"\d+"→"" in pattern="\d+" | Mootant can’t switch a mutant on here, with jsxAttributesinvalidinvalid-target |
'GIFT-'→'' in if (code.startsWith('GIFT-')) | Empty key gives a fixed result, as another mutator doesownedO8 |
'Sale'→'' in sale ? 'Sale' : '' | Same result as a true or false of another mutator, for a condition without an effectownedO19 |
'Free'→'' in total >= 50 ? 'Free' : '' | Copies the other branch, as comparison-operator does at the boundary, with the same effectssubsumedR5 |
'paid'→'' in case 'paid': | Value of a case labelnoisecase-label |
'X-Cart-Id'→'' in { ['X-Cart-Id']: cartId } | Key of an object or a classnoiseproperty-key |
`color: ${value}`→`${value}` | Text of a tagged template, which its tag readsnoisetagged-template |
'Unnamed'→'' in name || 'Unnamed' | logical-operator drops the default insteadownedO16 |
'Guest'→'' in name || 'Guest' with name?: string | logical-operator swaps || for ?? insteadsubsumedR7 |
'^SALE-'→'' in new RegExp('^SALE-') | Pattern or flags of RegExp, left to the regex mutatorsnoiseregex-argument |
'title'→'' in defineProps(['title']) | Read by the Vue or Svelte compiler as writteninvalidcompiler-macro |
`/cart/${id}`→'' | Same code as another mutantduplicateduplicate |
'owner'→'' in role === 'admin' || 'owner' | Side of && or ||, left to logical-operatorownedO3 |
'cart'→'' in Symbol('cart') | Description of a Symbol()noisesymbol-description |
'use client'→'' after an import | String that stands alone as a statementnoisebare-string |
'done'→'' in state = 'done' | May keep a loop running forevertimeoutloop-advance |
Changes that come back:
case-label, with thecaseTestsoption.jsx-attribute, with thejsxAttributesoption, except forclassName,class,styleanddata-testid. The values that would mean something else in braces becomeinvalid-target.loop-advance, with thetimeoutssetting.log-message, for a name removed fromloggers.O4,R5andR7, when the other mutant doesn’t run, as when its mutator is off or ignored on that line.- The
RegExpflags, when regex-flag is off.
strict turns on caseTests and timeouts. The other changes stay skipped.