@approval-tests/approvals 7.5.11 → 7.5.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.d.ts +1 -1
- package/bin/index.js +1 -1
- package/package.json +9 -4
- package/bin/index.ts +0 -108
- package/lib/AUtils.ts +0 -178
- package/lib/Approvals.ts +0 -321
- package/lib/Core/Namer.ts +0 -14
- package/lib/Core/Options.ts +0 -94
- package/lib/Core/Reporter.ts +0 -13
- package/lib/Core/Writer.ts +0 -5
- package/lib/FileApprover.ts +0 -116
- package/lib/FinalMessages.ts +0 -16
- package/lib/Logs/ApprovedFileLog.ts +0 -38
- package/lib/Logs/FailedFileLog.ts +0 -36
- package/lib/Logs/LogUtils.ts +0 -44
- package/lib/ManualNamer.ts +0 -12
- package/lib/Namer.ts +0 -28
- package/lib/Providers/BeforeEachVerifierBase.ts +0 -68
- package/lib/Providers/Jest/CombinationApprovals.ts +0 -161
- package/lib/Providers/Jest/JestApprovals.ts +0 -50
- package/lib/Providers/Jest/JestNamer.ts +0 -15
- package/lib/Providers/Jest/JestReporter.ts +0 -25
- package/lib/Providers/Jest/JestSetup.ts +0 -40
- package/lib/Providers/Mocha/MochaApprovals.ts +0 -74
- package/lib/Providers/Mocha/MochaNamer.ts +0 -84
- package/lib/Providers/OptionalFramework.ts +0 -11
- package/lib/Providers/Vitest/VitestApprovals.ts +0 -39
- package/lib/Providers/Vitest/VitestNamer.ts +0 -27
- package/lib/Providers/Vitest/VitestReporter.ts +0 -25
- package/lib/Reporting/DiffReporterAggregate.ts +0 -36
- package/lib/Reporting/GenericDiffReporterBase.ts +0 -154
- package/lib/Reporting/ReportByOpeningTheReceivedFile.ts +0 -22
- package/lib/Reporting/ReportLaunchingCircuitBreaker.ts +0 -40
- package/lib/Reporting/ReporterFactory.ts +0 -85
- package/lib/Reporting/Reporters/beyondcompareReporter.ts +0 -30
- package/lib/Reporting/Reporters/codiumReporter.ts +0 -26
- package/lib/Reporting/Reporters/copycommandReporter.ts +0 -46
- package/lib/Reporting/Reporters/diffmergeReporter.ts +0 -25
- package/lib/Reporting/Reporters/donothingReporter.ts +0 -13
- package/lib/Reporting/Reporters/gitdiffReporter.ts +0 -28
- package/lib/Reporting/Reporters/icdiffReporter.ts +0 -10
- package/lib/Reporting/Reporters/kdiff3Reporter.ts +0 -10
- package/lib/Reporting/Reporters/kompareReporter.ts +0 -10
- package/lib/Reporting/Reporters/meldReporter.ts +0 -10
- package/lib/Reporting/Reporters/multiReporter.ts +0 -55
- package/lib/Reporting/Reporters/nodediffReporter.ts +0 -61
- package/lib/Reporting/Reporters/opendiffReporter.ts +0 -10
- package/lib/Reporting/Reporters/p4mergeReporter.ts +0 -39
- package/lib/Reporting/Reporters/tortoisemergeReporter.ts +0 -25
- package/lib/Reporting/Reporters/vimdiffReporter.ts +0 -25
- package/lib/Reporting/Reporters/visualstudioReporter.ts +0 -48
- package/lib/Reporting/Reporters/vscodeReporter.ts +0 -26
- package/lib/Scrubbers/DateScrubber.ts +0 -83
- package/lib/Scrubbers/Scrubbers.ts +0 -106
- package/lib/StringWriter.ts +0 -73
- package/lib/Utilities/ConsoleUtils.ts +0 -15
- package/lib/Utilities/JestUtils.ts +0 -5
- package/lib/Utilities/Logger/LoggingInstance.ts +0 -247
- package/lib/Utilities/Logger/SimpleLogger.ts +0 -83
- package/lib/Utilities/Logger/SimpleLoggerApprovals.ts +0 -15
- package/lib/Utilities/Logger/StringWrapper.ts +0 -12
- package/lib/Utilities/Logger/Wrapper.ts +0 -27
- package/lib/Utilities/Printers.ts +0 -77
- package/lib/Utilities/StringUtils.ts +0 -8
- package/lib/Writers/BinaryWriter.ts +0 -42
- package/lib/config.ts +0 -95
- package/lib/osTools.ts +0 -11
- package/lib/postRunCleanup.ts +0 -63
package/bin/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
#!/usr/bin/env
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
export {};
|
package/bin/index.js
CHANGED
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
6
|
"description": "Approval Tests Library - Capturing Human Intelligence",
|
|
7
|
-
"version": "7.5.
|
|
7
|
+
"version": "7.5.13",
|
|
8
8
|
"homepage": "http://approvaltests.com",
|
|
9
9
|
"author": {
|
|
10
10
|
"name": "Jason Jarrett",
|
|
@@ -27,9 +27,14 @@
|
|
|
27
27
|
},
|
|
28
28
|
"license": "Apache-2.0",
|
|
29
29
|
"main": "lib/Approvals",
|
|
30
|
+
"types": "lib/Approvals.d.ts",
|
|
30
31
|
"files": [
|
|
31
|
-
"lib",
|
|
32
|
-
"
|
|
32
|
+
"lib/**/*.js",
|
|
33
|
+
"lib/**/*.d.ts",
|
|
34
|
+
"lib/DummyApprovedFiles/UnapprovedImage.png",
|
|
35
|
+
"bin/**/*.js",
|
|
36
|
+
"bin/**/*.d.ts",
|
|
37
|
+
"bin/help.md"
|
|
33
38
|
],
|
|
34
39
|
"bin": {
|
|
35
40
|
"approvals": "bin/index.js"
|
|
@@ -39,6 +44,7 @@
|
|
|
39
44
|
},
|
|
40
45
|
"scripts": {
|
|
41
46
|
"build": "tsc",
|
|
47
|
+
"prepack": "npm run build",
|
|
42
48
|
"test": "node scripts/test.cjs",
|
|
43
49
|
"vitest": "node scripts/test-isolated.cjs vitest",
|
|
44
50
|
"test2": "npm run mocha && npm run lint",
|
|
@@ -78,7 +84,6 @@
|
|
|
78
84
|
"pre-commit": "^2.0.0",
|
|
79
85
|
"prettier": "^3.4.2",
|
|
80
86
|
"sinon": "^22.0.0",
|
|
81
|
-
"ts-node": "^10.9.1",
|
|
82
87
|
"typescript": "^6.0.2"
|
|
83
88
|
},
|
|
84
89
|
"keywords": [
|
package/bin/index.ts
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ts-node
|
|
2
|
-
import es from "event-stream";
|
|
3
|
-
import * as autils from "../lib/AUtils";
|
|
4
|
-
import fs from "fs";
|
|
5
|
-
import path from "path";
|
|
6
|
-
import { marked } from "marked";
|
|
7
|
-
import { createTerminalRenderer, darkTheme } from "marked-terminal-renderer";
|
|
8
|
-
import minimist from "minimist";
|
|
9
|
-
import { configure, verify } from "../lib/Approvals";
|
|
10
|
-
import { redText } from "../lib/Utilities/ConsoleUtils";
|
|
11
|
-
|
|
12
|
-
const verbose = process.argv.includes("--verbose");
|
|
13
|
-
const printHelp = process.argv.includes("--help");
|
|
14
|
-
|
|
15
|
-
async function printHelpMessage(): Promise<void> {
|
|
16
|
-
if (verbose) {
|
|
17
|
-
console.log("printing help...");
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const helpFile = fs.readFileSync(path.join(__dirname, "help.md"), "utf8");
|
|
21
|
-
marked.use(createTerminalRenderer(darkTheme()));
|
|
22
|
-
console.log(await marked.parse(helpFile));
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async function errAndExit(msg: string): Promise<void> {
|
|
26
|
-
await printHelpMessage();
|
|
27
|
-
console.log(redText(msg));
|
|
28
|
-
process.exit(1);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async function main(): Promise<void> {
|
|
32
|
-
if (printHelp) {
|
|
33
|
-
await printHelpMessage();
|
|
34
|
-
process.exit();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (verbose) {
|
|
38
|
-
console.log("process.argv: ", process.argv);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const argv = minimist(process.argv.slice(2), {
|
|
42
|
-
alias: {
|
|
43
|
-
reporters: ["r"],
|
|
44
|
-
},
|
|
45
|
-
boolean: ["verbose"],
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
if (verbose) {
|
|
49
|
-
console.log("parsed args: ", argv);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
let reporters = argv.reporter as string | string[];
|
|
53
|
-
if (typeof reporters === "string") {
|
|
54
|
-
reporters = [reporters];
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const testname = argv._[0];
|
|
58
|
-
if (!testname) {
|
|
59
|
-
await errAndExit(
|
|
60
|
-
'Missing parameter: supply a test name ex: echo "hello" | approvals myFirstTest. This will become the file name myFirstTest.approved.txt in the current directory',
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const outdir = argv.outdir || process.cwd();
|
|
65
|
-
if (!fs.existsSync(outdir)) {
|
|
66
|
-
await errAndExit("Directory not found: " + outdir);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const errorOnStaleApprovedFiles = argv.errorOnStaleApprovedFiles === "true";
|
|
70
|
-
|
|
71
|
-
if (verbose) {
|
|
72
|
-
console.log("outdir: ", outdir);
|
|
73
|
-
console.log("errorOnStaleApprovedFiles: ", errorOnStaleApprovedFiles);
|
|
74
|
-
console.log("testname: ", testname);
|
|
75
|
-
console.log(
|
|
76
|
-
"reporters: ",
|
|
77
|
-
reporters ||
|
|
78
|
-
"undefined (but will fallback to approvals preconfigure defaults)",
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const opts: any = {};
|
|
83
|
-
if (reporters) {
|
|
84
|
-
opts.reporters = reporters;
|
|
85
|
-
}
|
|
86
|
-
opts.errorOnStaleApprovedFiles = errorOnStaleApprovedFiles;
|
|
87
|
-
|
|
88
|
-
if (verbose) {
|
|
89
|
-
console.log("approval opts: ", opts);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
opts.forceApproveAll =
|
|
93
|
-
autils.hasCommandLineArgument("--forceapproveall") ||
|
|
94
|
-
autils.hasCommandLineArgument("-f");
|
|
95
|
-
|
|
96
|
-
process.stdin.pipe(
|
|
97
|
-
es.mapSync((data: Buffer) => {
|
|
98
|
-
const dataToVerify = data.toString();
|
|
99
|
-
configure(opts);
|
|
100
|
-
verify(outdir, testname, dataToVerify);
|
|
101
|
-
}),
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
void main().catch((error: unknown) => {
|
|
106
|
-
console.error(error);
|
|
107
|
-
process.exitCode = 1;
|
|
108
|
-
});
|
package/lib/AUtils.ts
DELETED
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
import * as fs from "fs";
|
|
2
|
-
import * as path from "path";
|
|
3
|
-
import * as shell from "shelljs";
|
|
4
|
-
import * as osTools from "./osTools";
|
|
5
|
-
|
|
6
|
-
interface CachedProgramLookups {
|
|
7
|
-
[programName: string]: string | null;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const _cachedProgramLookups: CachedProgramLookups = {};
|
|
11
|
-
|
|
12
|
-
const createEmptyFileIfNotExists = (file: string): void => {
|
|
13
|
-
if (!fs.existsSync(file)) {
|
|
14
|
-
const ext = (path.extname(file) || "").toLowerCase();
|
|
15
|
-
if (ext === ".png") {
|
|
16
|
-
fs.writeFileSync(
|
|
17
|
-
file,
|
|
18
|
-
fs.readFileSync(
|
|
19
|
-
path.join(__dirname, "DummyApprovedFiles", "UnapprovedImage.png"),
|
|
20
|
-
),
|
|
21
|
-
);
|
|
22
|
-
} else {
|
|
23
|
-
fs.writeFileSync(file, "");
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const assertFileExists = (file: string): void => {
|
|
29
|
-
if (!fs.existsSync(file)) {
|
|
30
|
-
throw new Error("File not found: " + file);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const hasCommandLineArgument = (arg: string): boolean => {
|
|
35
|
-
return process.argv.some((val) => (val || "").toLowerCase() === arg);
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const isBinaryFile = (buffer: Buffer | String): boolean => {
|
|
39
|
-
const contentStartUTF8 = buffer.toString("utf8", 0, 24);
|
|
40
|
-
for (let i = 0, _ref = contentStartUTF8.length; i < _ref; i++) {
|
|
41
|
-
const charCode = contentStartUTF8.charCodeAt(i);
|
|
42
|
-
if (charCode === 65533 || charCode <= 8) {
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return false;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const trimExtension = (filePath: string): string => {
|
|
50
|
-
if (!filePath) return filePath;
|
|
51
|
-
const i = filePath.lastIndexOf(".");
|
|
52
|
-
const ext = i < 0 ? "" : filePath.substr(i);
|
|
53
|
-
return ext ? filePath.substring(0, filePath.length - ext.length) : filePath;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const findProgramOnPath = (programName: string): string | null => {
|
|
57
|
-
if (_cachedProgramLookups.hasOwnProperty(programName)) {
|
|
58
|
-
return _cachedProgramLookups[programName];
|
|
59
|
-
}
|
|
60
|
-
const output = shell.exec(
|
|
61
|
-
`${osTools.findProgramPathCommand} ${programName}`,
|
|
62
|
-
{ silent: true },
|
|
63
|
-
);
|
|
64
|
-
let result: string | null = null;
|
|
65
|
-
if (output.stdout) {
|
|
66
|
-
const file = output.stdout.split("\n")[0].trim();
|
|
67
|
-
const fixedFile = fixFilePathSlashes(file);
|
|
68
|
-
if (fs.existsSync(fixedFile)) {
|
|
69
|
-
result = fixedFile;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
if (!result) {
|
|
73
|
-
const pathMinusExtension = trimExtension(programName);
|
|
74
|
-
if (pathMinusExtension !== programName) {
|
|
75
|
-
result = findProgramOnPath(pathMinusExtension);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
_cachedProgramLookups[programName] = result;
|
|
79
|
-
return result;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
const searchForExecutable = (
|
|
83
|
-
folderInProgramInFiles?: string,
|
|
84
|
-
fileName?: string,
|
|
85
|
-
): string => {
|
|
86
|
-
if (!fileName) {
|
|
87
|
-
fileName = folderInProgramInFiles;
|
|
88
|
-
folderInProgramInFiles = undefined;
|
|
89
|
-
}
|
|
90
|
-
const programOnPath = findProgramOnPath(fileName!);
|
|
91
|
-
if (programOnPath) {
|
|
92
|
-
return programOnPath;
|
|
93
|
-
}
|
|
94
|
-
const lookInProgramFiles = (fileName_: string): string | null => {
|
|
95
|
-
if (osTools.platform.isWindows) {
|
|
96
|
-
let tryVar = findInPath(
|
|
97
|
-
"C:/Program Files",
|
|
98
|
-
folderInProgramInFiles || "",
|
|
99
|
-
fileName_,
|
|
100
|
-
);
|
|
101
|
-
if (tryVar) {
|
|
102
|
-
return tryVar;
|
|
103
|
-
}
|
|
104
|
-
tryVar = findInPath(
|
|
105
|
-
"C:/Program Files (x86)",
|
|
106
|
-
folderInProgramInFiles || "",
|
|
107
|
-
fileName_,
|
|
108
|
-
);
|
|
109
|
-
if (tryVar) {
|
|
110
|
-
return tryVar;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return null;
|
|
114
|
-
};
|
|
115
|
-
const findInPath = (
|
|
116
|
-
root: string,
|
|
117
|
-
dir: string,
|
|
118
|
-
file: string,
|
|
119
|
-
): string | null => {
|
|
120
|
-
const fullPath = path.join(root, dir, file);
|
|
121
|
-
const fixedFullPath = fixFilePathSlashes(fullPath);
|
|
122
|
-
if (fs.existsSync(fixedFullPath)) {
|
|
123
|
-
return fixedFullPath;
|
|
124
|
-
}
|
|
125
|
-
return null;
|
|
126
|
-
};
|
|
127
|
-
let fileFound = lookInProgramFiles(fileName!);
|
|
128
|
-
if (fileFound) {
|
|
129
|
-
return fileFound;
|
|
130
|
-
}
|
|
131
|
-
const suffix = ".exe";
|
|
132
|
-
if (!fileName!.endsWith(suffix)) {
|
|
133
|
-
fileFound = lookInProgramFiles(fileName! + suffix);
|
|
134
|
-
if (fileFound) {
|
|
135
|
-
return fileFound;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return "";
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
const fixFilePathSlashes = (path_: string): string => {
|
|
142
|
-
return path_.replace(/\\/g, "/");
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
const recursivelyOrderKeys = (unordered: any): any => {
|
|
146
|
-
if (unordered === null) {
|
|
147
|
-
return null;
|
|
148
|
-
}
|
|
149
|
-
if (Array.isArray(unordered)) {
|
|
150
|
-
return unordered.map(recursivelyOrderKeys);
|
|
151
|
-
}
|
|
152
|
-
if (typeof unordered === "object" && unordered !== null) {
|
|
153
|
-
const ordered: { [key: string]: any } = {};
|
|
154
|
-
Object.keys(unordered)
|
|
155
|
-
.sort()
|
|
156
|
-
.forEach((key) => {
|
|
157
|
-
ordered[key] = recursivelyOrderKeys(unordered[key]);
|
|
158
|
-
});
|
|
159
|
-
return ordered;
|
|
160
|
-
}
|
|
161
|
-
return unordered;
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
const stringifyKeysInOrder = (data: any): string => {
|
|
165
|
-
const sortedData = recursivelyOrderKeys(data);
|
|
166
|
-
return JSON.stringify(sortedData, null, " ");
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
export {
|
|
170
|
-
createEmptyFileIfNotExists,
|
|
171
|
-
assertFileExists,
|
|
172
|
-
hasCommandLineArgument,
|
|
173
|
-
isBinaryFile,
|
|
174
|
-
findProgramOnPath,
|
|
175
|
-
searchForExecutable,
|
|
176
|
-
fixFilePathSlashes,
|
|
177
|
-
stringifyKeysInOrder,
|
|
178
|
-
};
|
package/lib/Approvals.ts
DELETED
|
@@ -1,321 +0,0 @@
|
|
|
1
|
-
/*jshint freeze:false */
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Approvals module.
|
|
5
|
-
* @module approvals
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import callsite from "callsite";
|
|
9
|
-
import path from "path";
|
|
10
|
-
import { fileURLToPath } from "url";
|
|
11
|
-
import * as cfg from "./config";
|
|
12
|
-
import { BinaryWriter } from "./Writers/BinaryWriter";
|
|
13
|
-
import { FileApprover } from "./FileApprover";
|
|
14
|
-
import { FinalMessages } from "./FinalMessages";
|
|
15
|
-
import { ManualNamer } from "./ManualNamer";
|
|
16
|
-
import { Namer } from "./Core/Namer";
|
|
17
|
-
import { ReporterFactory, ReporterLoader } from "./Reporting/ReporterFactory";
|
|
18
|
-
import { Scrubber, Scrubbers } from "./Scrubbers/Scrubbers";
|
|
19
|
-
import { StringWriter } from "./StringWriter";
|
|
20
|
-
import { Writer } from "./Core/Writer";
|
|
21
|
-
import { postRunCleanup } from "./postRunCleanup";
|
|
22
|
-
import { stringifyKeysInOrder } from "./AUtils";
|
|
23
|
-
|
|
24
|
-
// if someone tries to call 'require("@approval-tests/approvals")...' without calling ".mocha(...) or
|
|
25
|
-
// they won't get a helpful error. So we put this in there - just in case"
|
|
26
|
-
import { MochaNamer } from "./Providers/Mocha/MochaNamer";
|
|
27
|
-
|
|
28
|
-
import { beforeEachVerifierBase } from "./Providers/BeforeEachVerifierBase";
|
|
29
|
-
import { isFrameworkAvailable } from "./Providers/OptionalFramework";
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Contains some helpful and util scrubbers that can be used for scrubbing data before saving to a received file.
|
|
33
|
-
*/
|
|
34
|
-
export { Scrubbers as scrubbers };
|
|
35
|
-
|
|
36
|
-
if (typeof beforeEach === "function") {
|
|
37
|
-
beforeEach(function (this: any): void {
|
|
38
|
-
if (!this) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
this.verify = function () {
|
|
42
|
-
throw new Error("You must call either .mocha()");
|
|
43
|
-
};
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// keep track of approved files we run into with tests
|
|
48
|
-
const listOfApprovedFiles: string[] = [];
|
|
49
|
-
process.on("approvalFileApproved", function (fileName): void {
|
|
50
|
-
if (listOfApprovedFiles.indexOf(fileName) === -1) {
|
|
51
|
-
listOfApprovedFiles.push(fileName);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
process.on("exit", function (): void {
|
|
56
|
-
const options = cfg.currentConfig();
|
|
57
|
-
postRunCleanup(options, listOfApprovedFiles);
|
|
58
|
-
const finalMessages = FinalMessages.getMessages();
|
|
59
|
-
if (finalMessages.length) {
|
|
60
|
-
console.log("******************************");
|
|
61
|
-
console.log("* Approvals Report: WARNING!");
|
|
62
|
-
finalMessages.forEach((message: string) => {
|
|
63
|
-
console.log(`* - ${message}`);
|
|
64
|
-
});
|
|
65
|
-
console.log("******************************");
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Allows you to provide overrides to the default configuration.
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* const approvals = require('@approval-tests/approvals');
|
|
74
|
-
* approvals.configure({
|
|
75
|
-
* reporters: ['p4merge']
|
|
76
|
-
* });
|
|
77
|
-
*
|
|
78
|
-
* @param {*} overrideOptions
|
|
79
|
-
*/
|
|
80
|
-
export function configure(overrideOptions: cfg.Config): typeof module.exports {
|
|
81
|
-
cfg.configure(overrideOptions);
|
|
82
|
-
return module.exports;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Allows the creation of an approvals configuration object using any passed in options to override the defaults.
|
|
87
|
-
* @param {Object} overrideOptions
|
|
88
|
-
* @returns {Object} approvals config object with any options overridden.
|
|
89
|
-
*/
|
|
90
|
-
export function getConfig(overrideOptions?: cfg.Config): cfg.Config {
|
|
91
|
-
return cfg.getConfig(overrideOptions);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Configure approvals to hook into Mocha tests.
|
|
96
|
-
* @param {*} optionalBaseDir - An optional folder to save approval files to.
|
|
97
|
-
*/
|
|
98
|
-
|
|
99
|
-
export function mocha(optionalBaseDir?: string): typeof module.exports {
|
|
100
|
-
if (!isFrameworkAvailable("mocha")) return module.exports;
|
|
101
|
-
// if not providing a base dir, fallback to the current calling code's directory
|
|
102
|
-
if (!optionalBaseDir) {
|
|
103
|
-
let fileName = callsite()[1].getFileName();
|
|
104
|
-
if (fileName.startsWith("file://")) {
|
|
105
|
-
fileName = fileURLToPath(fileName);
|
|
106
|
-
}
|
|
107
|
-
optionalBaseDir = path.dirname(fileName);
|
|
108
|
-
}
|
|
109
|
-
beforeEachVerifierBase(
|
|
110
|
-
MochaNamer,
|
|
111
|
-
"require('@approval-tests/approvals').mocha();",
|
|
112
|
-
optionalBaseDir,
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
return module.exports;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* `reporters` gives access to the `MultiReporter`
|
|
120
|
-
*
|
|
121
|
-
* @example
|
|
122
|
-
* const MultiReporter = approvals.reporters.MultiReporter
|
|
123
|
-
*/
|
|
124
|
-
|
|
125
|
-
export const reporters: { MultiReporter: any } = {
|
|
126
|
-
/**
|
|
127
|
-
* This allows access to the MultiReporter constructor.
|
|
128
|
-
* You can use this to run multiple reporters at the same time.
|
|
129
|
-
*
|
|
130
|
-
* @example
|
|
131
|
-
* approvals.verify(__dirname, "multi-reporter-example", "some data", {
|
|
132
|
-
* reporters: [
|
|
133
|
-
* new MultiReporter('p4merge', 'copycommand')
|
|
134
|
-
* ]
|
|
135
|
-
* });
|
|
136
|
-
*/
|
|
137
|
-
MultiReporter: require("./Reporting/Reporters/multiReporter").default,
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Use this to apply the scrubber function to any data before running verify.
|
|
142
|
-
*
|
|
143
|
-
* @example
|
|
144
|
-
* // basic approval test with a custom scrubber
|
|
145
|
-
* const approvals = require('@approval-tests/approvals');
|
|
146
|
-
* const scrubber = approvals.scrubbers.multiScrubber([
|
|
147
|
-
* function (data) {
|
|
148
|
-
* return (data || '').replace("some text", "some other text");
|
|
149
|
-
* },
|
|
150
|
-
* approvals.scrubbers.guidScrubber // to remove guids from the received data
|
|
151
|
-
* });
|
|
152
|
-
* approvals.verifyAndScrub(__dirname, 'sample-approval-test', "some text to verify", scrubber);
|
|
153
|
-
*
|
|
154
|
-
* @param {string} dirName - Typically `__dirname` but could be the base-directory (anywhere) to store both approved and received files.
|
|
155
|
-
* @param {string} testName - A file name safe string to call the file associated with this test.
|
|
156
|
-
* @param {(string|Buffer)} data - Either the string to save as a text file or a Buffer that represents an image
|
|
157
|
-
* @param {*} scrubber - A function that takes a string and returns a string. Approvals will call this if it exists to scrub the "data" before writing to any files.
|
|
158
|
-
* @param {*} optionsOverride - An object that can contain configurational overrides as defined in the approvals configuration object.
|
|
159
|
-
*/
|
|
160
|
-
function verifyAndScrub(
|
|
161
|
-
dirName: string,
|
|
162
|
-
testName: string,
|
|
163
|
-
data: BinaryWriter | string,
|
|
164
|
-
scrubber: Scrubber,
|
|
165
|
-
optionsOverride: any,
|
|
166
|
-
): void {
|
|
167
|
-
scrubber = scrubber || Scrubbers.noScrubber;
|
|
168
|
-
|
|
169
|
-
// backwards compatible option
|
|
170
|
-
if (Array.isArray(optionsOverride)) {
|
|
171
|
-
optionsOverride = {
|
|
172
|
-
reporters: optionsOverride,
|
|
173
|
-
};
|
|
174
|
-
const stringReporters = JSON.stringify(optionsOverride.reporters);
|
|
175
|
-
console.error(
|
|
176
|
-
'For the last arg of the approvals.verify(...) function, if you passed in an array of reporters, something like ["opendiff","nodediff"], this has been deprecated and replaced by passing in a config object. Please re-evaluate if you can remove this parameter alltogether and use individual user configs in ~/.approvalConfig, if not then replace this: ' +
|
|
177
|
-
stringReporters +
|
|
178
|
-
" with this { reporters: " +
|
|
179
|
-
stringReporters +
|
|
180
|
-
"}.",
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
const newOptions = cfg.getConfig(optionsOverride);
|
|
185
|
-
|
|
186
|
-
const namer = new ManualNamer(dirName, testName);
|
|
187
|
-
|
|
188
|
-
let writer: Writer;
|
|
189
|
-
if (data instanceof Buffer) {
|
|
190
|
-
writer = new BinaryWriter(data);
|
|
191
|
-
} else {
|
|
192
|
-
data = scrubber(data as string);
|
|
193
|
-
|
|
194
|
-
writer = new StringWriter(newOptions, data as string);
|
|
195
|
-
}
|
|
196
|
-
verifyWithControl(namer, writer, null, newOptions);
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
*
|
|
200
|
-
* @example
|
|
201
|
-
* // basic approval test
|
|
202
|
-
* const approvals = require('@approval-tests/approvals');
|
|
203
|
-
* approvals.verify(__dirname, 'sample-approval-test', "some text to verify");
|
|
204
|
-
*
|
|
205
|
-
* @example
|
|
206
|
-
* // basic approval test providing an option to override configuration
|
|
207
|
-
* const approvals = require('@approval-tests/approvals');
|
|
208
|
-
* approvals.verify(__dirname, 'sample-approval-test', "some text to verify", { normalizeLineEndingsTo: true });
|
|
209
|
-
*
|
|
210
|
-
* @param {string} dirName - Typically `__dirname` but could be the base-directory (anywhere) to store both approved and received files.
|
|
211
|
-
* @param {string} testName - A file name save string to call the file associated with this test.
|
|
212
|
-
* @param {(string|Buffer)} data - Either the string to save as a text file or a Buffer that represents an image
|
|
213
|
-
* @param {*} optionsOverride - An object that can contain configurational overrides as defined in the approvals configuration object.
|
|
214
|
-
*/
|
|
215
|
-
|
|
216
|
-
export function verify(
|
|
217
|
-
dirName: string,
|
|
218
|
-
testName: string,
|
|
219
|
-
data: any,
|
|
220
|
-
optionsOverride?: any,
|
|
221
|
-
): void {
|
|
222
|
-
return verifyAndScrub(
|
|
223
|
-
dirName,
|
|
224
|
-
testName,
|
|
225
|
-
data,
|
|
226
|
-
Scrubbers.noScrubber,
|
|
227
|
-
optionsOverride,
|
|
228
|
-
);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* You can pass as "data" any javascript object to be JSON.stringified and run verify against.
|
|
233
|
-
*
|
|
234
|
-
* @example
|
|
235
|
-
* const approvals = require('@approval-tests/approvals');
|
|
236
|
-
* approvals.verifyAndScrub(__dirname, 'sample-approval-test', { a: "some text in an object" });
|
|
237
|
-
*
|
|
238
|
-
* @param {string} dirName - Typically `__dirname` but could be the base-directory (anywhere) to store both approved and received files.
|
|
239
|
-
* @param {string} testName - A file name safe string to call the file associated with this test.
|
|
240
|
-
* @param {(string|Buffer)} data - This can be any JavaScript object/array that will be JSON.stringified before running verify
|
|
241
|
-
* @param {*} optionsOverride - An object that can contain configurational overrides as defined in the approvals configuration object.
|
|
242
|
-
*/
|
|
243
|
-
export function verifyAsJSON(
|
|
244
|
-
dirName: string,
|
|
245
|
-
testName: string,
|
|
246
|
-
data: any,
|
|
247
|
-
optionsOverride: any,
|
|
248
|
-
) {
|
|
249
|
-
return verifyAsJSONAndScrub(
|
|
250
|
-
dirName,
|
|
251
|
-
testName,
|
|
252
|
-
data,
|
|
253
|
-
Scrubbers.noScrubber,
|
|
254
|
-
optionsOverride,
|
|
255
|
-
);
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* You can pass as "data" any javascript object to be JSON.stringified. Before we run verify the scrubber will be run against the complete string before running verify against it.
|
|
260
|
-
|
|
261
|
-
* @example
|
|
262
|
-
* // basic approval test with a custom scrubber
|
|
263
|
-
* const approvals = require('@approval-tests/approvals');
|
|
264
|
-
* const scrubber = approvals.scrubbers.multiScrubber([
|
|
265
|
-
* function (data) {
|
|
266
|
-
* return (data || '').replace("some text", "some other text");
|
|
267
|
-
* },
|
|
268
|
-
* approvals.scrubbers.guidScrubber // to remove guids from the received data
|
|
269
|
-
* });
|
|
270
|
-
* approvals.verifyAndScrub(__dirname, 'sample-approval-test', { a: "some text in an object" }, scrubber);
|
|
271
|
-
*
|
|
272
|
-
* @param {string} dirName - Typically `__dirname` but could be the base-directory (anywhere) to store both approved and received files.
|
|
273
|
-
* @param {string} testName - A file name safe string to call the file associated with this test.
|
|
274
|
-
* @param {(string|Buffer)} data - This can be any JavaScript object/array that will be JSON.stringified before running verify
|
|
275
|
-
* @param {*} scrubber - A function that takes a string and returns a string. Approvals will call this if it exists to scrub the "data" before writing to any files.
|
|
276
|
-
* @param {*} optionsOverride - An object that can contain configurational overrides as defined in the approvals configuration object.
|
|
277
|
-
*/
|
|
278
|
-
|
|
279
|
-
export function verifyAsJSONAndScrub(
|
|
280
|
-
dirName: string,
|
|
281
|
-
testName: string,
|
|
282
|
-
data: any,
|
|
283
|
-
scrubber: Scrubber,
|
|
284
|
-
optionsOverride: any,
|
|
285
|
-
): void {
|
|
286
|
-
return verifyAndScrub(
|
|
287
|
-
dirName,
|
|
288
|
-
testName,
|
|
289
|
-
stringifyKeysInOrder(data),
|
|
290
|
-
scrubber,
|
|
291
|
-
optionsOverride,
|
|
292
|
-
);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* This allows you to take full control of naming and writing files before verifying.
|
|
297
|
-
*
|
|
298
|
-
* For an example that we use to generate the docs within the readme, check out the [test/mocha/readmeTests.mts](test/mocha/readmeTests.mts) in this project.
|
|
299
|
-
*
|
|
300
|
-
* @param {Object} namer
|
|
301
|
-
* @param {Object} writer
|
|
302
|
-
* @param {Function} [reporterFactory]
|
|
303
|
-
* @param {Object} [optionsOverride]
|
|
304
|
-
*/
|
|
305
|
-
|
|
306
|
-
export function verifyWithControl(
|
|
307
|
-
namer: Namer,
|
|
308
|
-
writer: Writer,
|
|
309
|
-
reporterFactory?: ReporterLoader | null,
|
|
310
|
-
optionsOverride?: Partial<cfg.Config>,
|
|
311
|
-
) {
|
|
312
|
-
const newOptions = cfg.getConfig(optionsOverride);
|
|
313
|
-
|
|
314
|
-
const loader =
|
|
315
|
-
(reporterFactory as ReporterLoader) ||
|
|
316
|
-
function () {
|
|
317
|
-
return [ReporterFactory.loadReporter(newOptions.reporters)];
|
|
318
|
-
};
|
|
319
|
-
|
|
320
|
-
FileApprover.verify(namer, writer, loader, newOptions);
|
|
321
|
-
}
|
package/lib/Core/Namer.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { StringUtils } from "../Utilities/StringUtils";
|
|
2
|
-
|
|
3
|
-
export interface Namer {
|
|
4
|
-
getApprovedFile(ext: string): string;
|
|
5
|
-
getReceivedFile(ext: string): string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function convertToFilename(name: string): string {
|
|
9
|
-
const forbidden = "\\/:?\"<>|' ";
|
|
10
|
-
for (let forbiddenKey of forbidden) {
|
|
11
|
-
name = StringUtils.replaceAll(name, forbiddenKey, "_");
|
|
12
|
-
}
|
|
13
|
-
return name;
|
|
14
|
-
}
|