Skip to content

Commit

Permalink
Remove String prototype pollution in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bcaudan committed Sep 19, 2020
1 parent 8d08ee4 commit 38c3fa1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
3 changes: 3 additions & 0 deletions spec/helpers/before-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ declare namespace NodeJS {
SpecReporter;
DisplayProcessor;
TestProcessor;
colors;
}
}

global.SpecReporter = require("../../built/main").SpecReporter;
global.DisplayProcessor = require("../../built/main").DisplayProcessor;
global.TestProcessor = require("./test-processor").TestProcessor;
// tslint:disable-next-line:no-submodule-imports
global.colors = require("colors/safe");

beforeAll(() => {
const addMatchers = require("./test-helper").addMatchers;
Expand Down
14 changes: 3 additions & 11 deletions spec/helpers/test-helper.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
require("colors");

interface String {
stripTime(): string;
}

// tslint:disable-next-line:no-unbound-method
String.prototype.stripTime = function(): string {
return this.replace(/in (\d+\.?\d*|\.\d+) secs?/, "in {time}") // replace time in summary
let stripTime = (str: string) =>
str.replace(/in (\d+\.?\d*|\.\d+) secs?/, "in {time}") // replace time in summary
.replace(/\((\d+\.?\d*|\.\d+) secs?\)/, "({time})"); // replace time in specs
};

let isArray = value => value.toString() === "[object Array]";

Expand Down Expand Up @@ -69,7 +61,7 @@ const JasmineEnv = {
logInSummary = false;
console.log = stuff => {
if (!options.withColor) {
stuff = stuff.stripColors.stripTime();
stuff = global.colors.stripColors(stripTime(stuff));
}
if (/^(Executed|\*\*\*\*\*\*\*)/.test(stuff)) {
logInSummary = true;
Expand Down
12 changes: 6 additions & 6 deletions spec/unit/colors-display.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("with colors", () => {
},
outputs => {
expect(outputs).not.contains(" ✓ successful spec");
expect(outputs).contains(" " + "✓ successful spec".green);
expect(outputs).contains(" " + global.colors.green("✓ successful spec"));
done();
},
{ withColor: true }
Expand All @@ -40,7 +40,7 @@ describe("with colors", () => {
},
outputs => {
expect(outputs).not.contains(" ✗ failed spec");
expect(outputs).contains(" " + "✗ failed spec".red);
expect(outputs).contains(" " + global.colors.red("✗ failed spec"));
done();
},
{ withColor: true }
Expand All @@ -59,7 +59,7 @@ describe("with colors", () => {
},
outputs => {
expect(outputs).not.contains(" * pending spec");
expect(outputs).contains(" " + "* pending spec".yellow);
expect(outputs).contains(" " + global.colors.yellow("* pending spec"));
done();
},
{ withColor: true }
Expand Down Expand Up @@ -94,7 +94,7 @@ describe("with colors", () => {
});
},
outputs => {
expect(outputs).contains(" " + "✓ successful spec".magenta);
expect(outputs).contains(" " + global.colors.magenta("✓ successful spec"));
done();
},
{ withColor: true }
Expand All @@ -112,7 +112,7 @@ describe("with colors", () => {
});
},
outputs => {
expect(outputs).contains(" " + "✗ failed spec".white);
expect(outputs).contains(" " + global.colors.white("✗ failed spec"));
done();
},
{ withColor: true }
Expand All @@ -130,7 +130,7 @@ describe("with colors", () => {
});
},
outputs => {
expect(outputs).contains(" " + "* pending spec".blue);
expect(outputs).contains(" " + global.colors.blue("* pending spec"));
done();
},
{ withColor: true }
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/custom-print.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe("spec reporter", () => {
expect(outputs.length).toEqual(0);
expect(customOutpouts).contains([
" suite",
" " + "✓ spec to be started".green
" " + global.colors.green("✓ spec to be started")
]);
done();
}
Expand Down

0 comments on commit 38c3fa1

Please sign in to comment.