/
string-literal-mutator.spec.ts
103 lines (85 loc) · 3.77 KB
/
string-literal-mutator.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { expect } from 'chai';
import { expectJSMutation } from '../../helpers/expect-mutation';
import { StringLiteralMutator } from '../../../src/mutators/string-literal-mutator';
describe(StringLiteralMutator.name, () => {
let sut: StringLiteralMutator;
beforeEach(() => {
sut = new StringLiteralMutator();
});
it('should have name "StringLiteral"', () => {
expect(sut.name).eq('StringLiteral');
});
describe('string literals', () => {
it('should mutate a string literal with double quotes', () => {
expectJSMutation(sut, 'const b = "Hello world!";', 'const b = "";');
});
it('should mutate a string literal with single quotes', () => {
expectJSMutation(sut, "const b = 'Hello world!';", 'const b = "";');
});
it('should mutate a template string', () => {
expectJSMutation(sut, 'const b = `Hello world!`;', 'const b = ``;');
});
it('should mutate a template string referencing another variable', () => {
expectJSMutation(sut, 'const a = 10; const b = `${a} mutations`;', 'const a = 10; const b = ``;');
expectJSMutation(sut, 'const a = 10; const b = `mutations: ${a}`;', 'const a = 10; const b = ``;');
expectJSMutation(sut, 'const a = 10; const b = `mutations: ${a} out of 10`;', 'const a = 10; const b = ``;');
});
it('should mutate empty strings', () => {
expectJSMutation(sut, 'const b = "";', 'const b = "Stryker was here!";');
});
it('should mutate empty template strings', () => {
expectJSMutation(sut, 'const b = ``;', 'const b = `Stryker was here!`;');
});
it('should not mutate directive prologues', () => {
expectJSMutation(sut, '"use strict";"use asm";');
expectJSMutation(sut, 'function a() {"use strict";"use asm";}');
});
});
describe('imports/exports', () => {
it('should not mutate import statements', () => {
expectJSMutation(sut, 'import * as foo from "foo";');
expectJSMutation(sut, 'import { foo } from "foo";');
expectJSMutation(sut, 'import foo from "foo";');
expectJSMutation(sut, 'import "foo";');
});
it('should not mutate require call statements', () => {
expectJSMutation(sut, 'require("./lib/square");');
});
it('should mutate other call statements', () => {
expectJSMutation(sut, 'require2("./lib/square");', 'require2("");');
});
it('should not mutate export statements', () => {
expectJSMutation(sut, 'export * from "./foo";');
expectJSMutation(sut, 'export { foo as boo } from "./foo";');
});
});
describe('type declarations', () => {
it('should not mutate type declarations', () => {
expectJSMutation(sut, 'const a: "hello" = "hello";', 'const a: "hello" = "";');
expectJSMutation(sut, 'const a: Record<"id", number> = { id: 10 }');
});
// interfaces itself are skipped entirely by the babel-transformer
});
describe('object properties', () => {
it('should not mutate inside object property keys', () => {
expectJSMutation(sut, 'const { className, "aria-label": label } = props;');
});
it('should not mutate inside object property keys', () => {
expectJSMutation(sut, 'const foo = { className, ["aria-label"]: label };');
});
it('should still mutate inside object property values', () => {
expectJSMutation(sut, 'const foo = { bar: "baz" };', 'const foo = { bar: "" };');
});
it('should not mutate class property keys', () => {
expectJSMutation(sut, 'class Foo { "baz-bar" = 4; }');
});
it('should mutate class property values', () => {
expectJSMutation(sut, 'class Foo { bar = "4"; }', 'class Foo { bar = ""; }');
});
});
describe('jsx', () => {
it('should not mutate string JSX attributes', () => {
expectJSMutation(sut, '<Record class="row" />');
});
});
});