@approval-tests/approvals 7.5.7
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/help.md +31 -0
- package/bin/index.d.ts +2 -0
- package/bin/index.js +119 -0
- package/bin/index.ts +106 -0
- package/lib/AUtils.d.ts +9 -0
- package/lib/AUtils.js +175 -0
- package/lib/AUtils.ts +178 -0
- package/lib/Approvals.d.ts +104 -0
- package/lib/Approvals.js +280 -0
- package/lib/Approvals.ts +319 -0
- package/lib/Core/Namer.d.ts +5 -0
- package/lib/Core/Namer.js +11 -0
- package/lib/Core/Namer.ts +14 -0
- package/lib/Core/Options.d.ts +31 -0
- package/lib/Core/Options.js +74 -0
- package/lib/Core/Options.ts +94 -0
- package/lib/Core/Reporter.d.ts +6 -0
- package/lib/Core/Reporter.js +2 -0
- package/lib/Core/Reporter.ts +13 -0
- package/lib/Core/Writer.d.ts +4 -0
- package/lib/Core/Writer.js +2 -0
- package/lib/Core/Writer.ts +5 -0
- package/lib/DummyApprovedFiles/UnapprovedImage.png +0 -0
- package/lib/FileApprover.d.ts +9 -0
- package/lib/FileApprover.js +85 -0
- package/lib/FileApprover.ts +116 -0
- package/lib/FinalMessages.d.ts +5 -0
- package/lib/FinalMessages.js +17 -0
- package/lib/FinalMessages.ts +16 -0
- package/lib/Logs/ApprovedFileLog.d.ts +6 -0
- package/lib/Logs/ApprovedFileLog.js +68 -0
- package/lib/Logs/ApprovedFileLog.ts +38 -0
- package/lib/Logs/FailedFileLog.d.ts +7 -0
- package/lib/Logs/FailedFileLog.js +65 -0
- package/lib/Logs/FailedFileLog.ts +36 -0
- package/lib/Logs/LogUtils.d.ts +7 -0
- package/lib/Logs/LogUtils.js +56 -0
- package/lib/Logs/LogUtils.ts +44 -0
- package/lib/ManualNamer.d.ts +4 -0
- package/lib/ManualNamer.js +15 -0
- package/lib/ManualNamer.ts +12 -0
- package/lib/Namer.d.ts +9 -0
- package/lib/Namer.js +27 -0
- package/lib/Namer.ts +28 -0
- package/lib/Providers/BeforeEachVerifierBase.d.ts +2 -0
- package/lib/Providers/BeforeEachVerifierBase.js +82 -0
- package/lib/Providers/BeforeEachVerifierBase.ts +68 -0
- package/lib/Providers/Jest/CombinationApprovals.d.ts +14 -0
- package/lib/Providers/Jest/CombinationApprovals.js +44 -0
- package/lib/Providers/Jest/CombinationApprovals.ts +161 -0
- package/lib/Providers/Jest/JestApprovals.d.ts +4 -0
- package/lib/Providers/Jest/JestApprovals.js +73 -0
- package/lib/Providers/Jest/JestApprovals.ts +48 -0
- package/lib/Providers/Jest/JestNamer.d.ts +2 -0
- package/lib/Providers/Jest/JestNamer.js +18 -0
- package/lib/Providers/Jest/JestNamer.ts +15 -0
- package/lib/Providers/Jest/JestReporter.d.ts +7 -0
- package/lib/Providers/Jest/JestReporter.js +22 -0
- package/lib/Providers/Jest/JestReporter.ts +22 -0
- package/lib/Providers/Jest/JestSetup.d.ts +3 -0
- package/lib/Providers/Jest/JestSetup.js +41 -0
- package/lib/Providers/Jest/JestSetup.ts +40 -0
- package/lib/Providers/Mocha/MochaApprovals.d.ts +8 -0
- package/lib/Providers/Mocha/MochaApprovals.js +94 -0
- package/lib/Providers/Mocha/MochaApprovals.ts +70 -0
- package/lib/Providers/Mocha/MochaNamer.d.ts +9 -0
- package/lib/Providers/Mocha/MochaNamer.js +72 -0
- package/lib/Providers/Mocha/MochaNamer.ts +84 -0
- package/lib/Providers/Vitest/VitestApprovals.d.ts +4 -0
- package/lib/Providers/Vitest/VitestApprovals.js +62 -0
- package/lib/Providers/Vitest/VitestApprovals.ts +37 -0
- package/lib/Providers/Vitest/VitestNamer.d.ts +2 -0
- package/lib/Providers/Vitest/VitestNamer.js +17 -0
- package/lib/Providers/Vitest/VitestNamer.ts +21 -0
- package/lib/Providers/Vitest/VitestReporter.d.ts +7 -0
- package/lib/Providers/Vitest/VitestReporter.js +21 -0
- package/lib/Providers/Vitest/VitestReporter.ts +23 -0
- package/lib/Reporting/DiffReporterAggregate.d.ts +10 -0
- package/lib/Reporting/DiffReporterAggregate.js +28 -0
- package/lib/Reporting/DiffReporterAggregate.ts +36 -0
- package/lib/Reporting/GenericDiffReporterBase.d.ts +22 -0
- package/lib/Reporting/GenericDiffReporterBase.js +136 -0
- package/lib/Reporting/GenericDiffReporterBase.ts +154 -0
- package/lib/Reporting/ReportByOpeningTheReceivedFile.d.ts +7 -0
- package/lib/Reporting/ReportByOpeningTheReceivedFile.js +19 -0
- package/lib/Reporting/ReportByOpeningTheReceivedFile.ts +22 -0
- package/lib/Reporting/ReportLaunchingCircuitBreaker.d.ts +8 -0
- package/lib/Reporting/ReportLaunchingCircuitBreaker.js +37 -0
- package/lib/Reporting/ReportLaunchingCircuitBreaker.ts +40 -0
- package/lib/Reporting/ReporterFactory.d.ts +8 -0
- package/lib/Reporting/ReporterFactory.js +67 -0
- package/lib/Reporting/ReporterFactory.ts +85 -0
- package/lib/Reporting/Reporters/beyondcompareReporter.d.ts +4 -0
- package/lib/Reporting/Reporters/beyondcompareReporter.js +32 -0
- package/lib/Reporting/Reporters/beyondcompareReporter.ts +30 -0
- package/lib/Reporting/Reporters/codiumReporter.d.ts +5 -0
- package/lib/Reporting/Reporters/codiumReporter.js +25 -0
- package/lib/Reporting/Reporters/codiumReporter.ts +26 -0
- package/lib/Reporting/Reporters/copycommandReporter.d.ts +7 -0
- package/lib/Reporting/Reporters/copycommandReporter.js +40 -0
- package/lib/Reporting/Reporters/copycommandReporter.ts +46 -0
- package/lib/Reporting/Reporters/diffmergeReporter.d.ts +5 -0
- package/lib/Reporting/Reporters/diffmergeReporter.js +54 -0
- package/lib/Reporting/Reporters/diffmergeReporter.ts +25 -0
- package/lib/Reporting/Reporters/donothingReporter.d.ts +6 -0
- package/lib/Reporting/Reporters/donothingReporter.js +14 -0
- package/lib/Reporting/Reporters/donothingReporter.ts +13 -0
- package/lib/Reporting/Reporters/gitdiffReporter.d.ts +6 -0
- package/lib/Reporting/Reporters/gitdiffReporter.js +23 -0
- package/lib/Reporting/Reporters/gitdiffReporter.ts +28 -0
- package/lib/Reporting/Reporters/icdiffReporter.d.ts +4 -0
- package/lib/Reporting/Reporters/icdiffReporter.js +11 -0
- package/lib/Reporting/Reporters/icdiffReporter.ts +10 -0
- package/lib/Reporting/Reporters/kdiff3Reporter.d.ts +4 -0
- package/lib/Reporting/Reporters/kdiff3Reporter.js +11 -0
- package/lib/Reporting/Reporters/kdiff3Reporter.ts +10 -0
- package/lib/Reporting/Reporters/kompareReporter.d.ts +4 -0
- package/lib/Reporting/Reporters/kompareReporter.js +11 -0
- package/lib/Reporting/Reporters/kompareReporter.ts +10 -0
- package/lib/Reporting/Reporters/meldReporter.d.ts +4 -0
- package/lib/Reporting/Reporters/meldReporter.js +11 -0
- package/lib/Reporting/Reporters/meldReporter.ts +10 -0
- package/lib/Reporting/Reporters/multiReporter.d.ts +8 -0
- package/lib/Reporting/Reporters/multiReporter.js +49 -0
- package/lib/Reporting/Reporters/multiReporter.ts +55 -0
- package/lib/Reporting/Reporters/nodediffReporter.d.ts +7 -0
- package/lib/Reporting/Reporters/nodediffReporter.js +50 -0
- package/lib/Reporting/Reporters/nodediffReporter.ts +61 -0
- package/lib/Reporting/Reporters/opendiffReporter.d.ts +4 -0
- package/lib/Reporting/Reporters/opendiffReporter.js +11 -0
- package/lib/Reporting/Reporters/opendiffReporter.ts +10 -0
- package/lib/Reporting/Reporters/p4mergeReporter.d.ts +4 -0
- package/lib/Reporting/Reporters/p4mergeReporter.js +41 -0
- package/lib/Reporting/Reporters/p4mergeReporter.ts +39 -0
- package/lib/Reporting/Reporters/tortoisemergeReporter.d.ts +4 -0
- package/lib/Reporting/Reporters/tortoisemergeReporter.js +25 -0
- package/lib/Reporting/Reporters/tortoisemergeReporter.ts +25 -0
- package/lib/Reporting/Reporters/vimdiffReporter.d.ts +4 -0
- package/lib/Reporting/Reporters/vimdiffReporter.js +24 -0
- package/lib/Reporting/Reporters/vimdiffReporter.ts +25 -0
- package/lib/Reporting/Reporters/visualstudioReporter.d.ts +6 -0
- package/lib/Reporting/Reporters/visualstudioReporter.js +42 -0
- package/lib/Reporting/Reporters/visualstudioReporter.ts +48 -0
- package/lib/Reporting/Reporters/vscodeReporter.d.ts +5 -0
- package/lib/Reporting/Reporters/vscodeReporter.js +25 -0
- package/lib/Reporting/Reporters/vscodeReporter.ts +26 -0
- package/lib/Scrubbers/DateScrubber.d.ts +12 -0
- package/lib/Scrubbers/DateScrubber.js +67 -0
- package/lib/Scrubbers/DateScrubber.ts +83 -0
- package/lib/Scrubbers/Scrubbers.d.ts +20 -0
- package/lib/Scrubbers/Scrubbers.js +75 -0
- package/lib/Scrubbers/Scrubbers.ts +106 -0
- package/lib/StringWriter.d.ts +11 -0
- package/lib/StringWriter.js +82 -0
- package/lib/StringWriter.ts +73 -0
- package/lib/Utilities/ConsoleUtils.d.ts +4 -0
- package/lib/Utilities/ConsoleUtils.js +19 -0
- package/lib/Utilities/ConsoleUtils.ts +15 -0
- package/lib/Utilities/JestUtils.d.ts +3 -0
- package/lib/Utilities/JestUtils.js +9 -0
- package/lib/Utilities/JestUtils.ts +5 -0
- package/lib/Utilities/Logger/LoggingInstance.d.ts +32 -0
- package/lib/Utilities/Logger/LoggingInstance.js +214 -0
- package/lib/Utilities/Logger/LoggingInstance.ts +247 -0
- package/lib/Utilities/Logger/SimpleLogger.d.ts +23 -0
- package/lib/Utilities/Logger/SimpleLogger.js +61 -0
- package/lib/Utilities/Logger/SimpleLogger.ts +83 -0
- package/lib/Utilities/Logger/SimpleLoggerApprovals.d.ts +2 -0
- package/lib/Utilities/Logger/SimpleLoggerApprovals.js +12 -0
- package/lib/Utilities/Logger/SimpleLoggerApprovals.ts +15 -0
- package/lib/Utilities/Logger/StringWrapper.d.ts +6 -0
- package/lib/Utilities/Logger/StringWrapper.js +15 -0
- package/lib/Utilities/Logger/StringWrapper.ts +12 -0
- package/lib/Utilities/Logger/Wrapper.d.ts +14 -0
- package/lib/Utilities/Logger/Wrapper.js +27 -0
- package/lib/Utilities/Logger/Wrapper.ts +27 -0
- package/lib/Utilities/Printers.d.ts +4 -0
- package/lib/Utilities/Printers.js +52 -0
- package/lib/Utilities/Printers.ts +77 -0
- package/lib/Utilities/StringUtils.d.ts +3 -0
- package/lib/Utilities/StringUtils.js +12 -0
- package/lib/Utilities/StringUtils.ts +8 -0
- package/lib/Writers/BinaryWriter.d.ts +8 -0
- package/lib/Writers/BinaryWriter.js +63 -0
- package/lib/Writers/BinaryWriter.ts +31 -0
- package/lib/config.d.ts +22 -0
- package/lib/config.js +106 -0
- package/lib/config.ts +95 -0
- package/lib/osTools.d.ts +5 -0
- package/lib/osTools.js +11 -0
- package/lib/osTools.ts +11 -0
- package/lib/postRunCleanup.d.ts +1 -0
- package/lib/postRunCleanup.js +51 -0
- package/lib/postRunCleanup.ts +63 -0
- package/package.json +109 -0
- package/readme.md +557 -0
package/lib/Approvals.ts
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
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
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Contains some helpful and util scrubbers that can be used for scrubbing data before saving to a received file.
|
|
32
|
+
*/
|
|
33
|
+
export { Scrubbers as scrubbers };
|
|
34
|
+
|
|
35
|
+
if (typeof beforeEach === "function") {
|
|
36
|
+
beforeEach(function (this: any): void {
|
|
37
|
+
if (!this) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
this.verify = function () {
|
|
41
|
+
throw new Error("You must call either .mocha()");
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// keep track of approved files we run into with tests
|
|
47
|
+
const listOfApprovedFiles: string[] = [];
|
|
48
|
+
process.on("approvalFileApproved", function (fileName): void {
|
|
49
|
+
if (listOfApprovedFiles.indexOf(fileName) === -1) {
|
|
50
|
+
listOfApprovedFiles.push(fileName);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
process.on("exit", function (): void {
|
|
55
|
+
const options = cfg.currentConfig();
|
|
56
|
+
postRunCleanup(options, listOfApprovedFiles);
|
|
57
|
+
const finalMessages = FinalMessages.getMessages();
|
|
58
|
+
if (finalMessages.length) {
|
|
59
|
+
console.log("******************************");
|
|
60
|
+
console.log("* Approvals Report: WARNING!");
|
|
61
|
+
finalMessages.forEach((message: string) => {
|
|
62
|
+
console.log(`* - ${message}`);
|
|
63
|
+
});
|
|
64
|
+
console.log("******************************");
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Allows you to provide overrides to the default configuration.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* const approvals = require('@approval-tests/approvals');
|
|
73
|
+
* approvals.configure({
|
|
74
|
+
* reporters: ['p4merge']
|
|
75
|
+
* });
|
|
76
|
+
*
|
|
77
|
+
* @param {*} overrideOptions
|
|
78
|
+
*/
|
|
79
|
+
export function configure(overrideOptions: cfg.Config): typeof module.exports {
|
|
80
|
+
cfg.configure(overrideOptions);
|
|
81
|
+
return module.exports;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Allows the creation of an approvals configuration object using any passed in options to override the defaults.
|
|
86
|
+
* @param {Object} overrideOptions
|
|
87
|
+
* @returns {Object} approvals config object with any options overridden.
|
|
88
|
+
*/
|
|
89
|
+
export function getConfig(overrideOptions?: cfg.Config): cfg.Config {
|
|
90
|
+
return cfg.getConfig(overrideOptions);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Configure approvals to hook into Mocha tests.
|
|
95
|
+
* @param {*} optionalBaseDir - An optional folder to save approval files to.
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
export function mocha(optionalBaseDir?: string): typeof module.exports {
|
|
99
|
+
// if not providing a base dir, fallback to the current calling code's directory
|
|
100
|
+
if (!optionalBaseDir) {
|
|
101
|
+
let fileName = callsite()[1].getFileName();
|
|
102
|
+
if (fileName.startsWith("file://")) {
|
|
103
|
+
fileName = fileURLToPath(fileName);
|
|
104
|
+
}
|
|
105
|
+
optionalBaseDir = path.dirname(fileName);
|
|
106
|
+
}
|
|
107
|
+
beforeEachVerifierBase(
|
|
108
|
+
MochaNamer,
|
|
109
|
+
"require('@approval-tests/approvals').mocha();",
|
|
110
|
+
optionalBaseDir,
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
return module.exports;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* `reporters` gives access to the `MultiReporter`
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* const MultiReporter = approvals.reporters.MultiReporter
|
|
121
|
+
*/
|
|
122
|
+
|
|
123
|
+
export const reporters: { MultiReporter: any } = {
|
|
124
|
+
/**
|
|
125
|
+
* This allows access to the MultiReporter constructor.
|
|
126
|
+
* You can use this to run multiple reporters at the same time.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* approvals.verify(__dirname, "multi-reporter-example", "some data", {
|
|
130
|
+
* reporters: [
|
|
131
|
+
* new MultiReporter('p4merge', 'copycommand')
|
|
132
|
+
* ]
|
|
133
|
+
* });
|
|
134
|
+
*/
|
|
135
|
+
MultiReporter: require("./Reporting/Reporters/multiReporter").default,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Use this to apply the scrubber function to any data before running verify.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* // basic approval test with a custom scrubber
|
|
143
|
+
* const approvals = require('@approval-tests/approvals');
|
|
144
|
+
* const scrubber = approvals.scrubbers.multiScrubber([
|
|
145
|
+
* function (data) {
|
|
146
|
+
* return (data || '').replace("some text", "some other text");
|
|
147
|
+
* },
|
|
148
|
+
* approvals.scrubbers.guidScrubber // to remove guids from the received data
|
|
149
|
+
* });
|
|
150
|
+
* approvals.verifyAndScrub(__dirname, 'sample-approval-test', "some text to verify", scrubber);
|
|
151
|
+
*
|
|
152
|
+
* @param {string} dirName - Typically `__dirname` but could be the base-directory (anywhere) to store both approved and received files.
|
|
153
|
+
* @param {string} testName - A file name safe string to call the file associated with this test.
|
|
154
|
+
* @param {(string|Buffer)} data - Either the string to save as a text file or a Buffer that represents an image
|
|
155
|
+
* @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.
|
|
156
|
+
* @param {*} optionsOverride - An object that can contain configurational overrides as defined in the approvals configuration object.
|
|
157
|
+
*/
|
|
158
|
+
function verifyAndScrub(
|
|
159
|
+
dirName: string,
|
|
160
|
+
testName: string,
|
|
161
|
+
data: BinaryWriter | string,
|
|
162
|
+
scrubber: Scrubber,
|
|
163
|
+
optionsOverride: any,
|
|
164
|
+
): void {
|
|
165
|
+
scrubber = scrubber || Scrubbers.noScrubber;
|
|
166
|
+
|
|
167
|
+
// backwards compatible option
|
|
168
|
+
if (Array.isArray(optionsOverride)) {
|
|
169
|
+
optionsOverride = {
|
|
170
|
+
reporters: optionsOverride,
|
|
171
|
+
};
|
|
172
|
+
const stringReporters = JSON.stringify(optionsOverride.reporters);
|
|
173
|
+
console.error(
|
|
174
|
+
'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: ' +
|
|
175
|
+
stringReporters +
|
|
176
|
+
" with this { reporters: " +
|
|
177
|
+
stringReporters +
|
|
178
|
+
"}.",
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const newOptions = cfg.getConfig(optionsOverride);
|
|
183
|
+
|
|
184
|
+
const namer = new ManualNamer(dirName, testName);
|
|
185
|
+
|
|
186
|
+
let writer: Writer;
|
|
187
|
+
if (data instanceof Buffer) {
|
|
188
|
+
writer = new BinaryWriter(data);
|
|
189
|
+
} else {
|
|
190
|
+
data = scrubber(data as string);
|
|
191
|
+
|
|
192
|
+
writer = new StringWriter(newOptions, data as string);
|
|
193
|
+
}
|
|
194
|
+
verifyWithControl(namer, writer, null, newOptions);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* // basic approval test
|
|
200
|
+
* const approvals = require('@approval-tests/approvals');
|
|
201
|
+
* approvals.verify(__dirname, 'sample-approval-test', "some text to verify");
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* // basic approval test providing an option to override configuration
|
|
205
|
+
* const approvals = require('@approval-tests/approvals');
|
|
206
|
+
* approvals.verify(__dirname, 'sample-approval-test', "some text to verify", { normalizeLineEndingsTo: true });
|
|
207
|
+
*
|
|
208
|
+
* @param {string} dirName - Typically `__dirname` but could be the base-directory (anywhere) to store both approved and received files.
|
|
209
|
+
* @param {string} testName - A file name save string to call the file associated with this test.
|
|
210
|
+
* @param {(string|Buffer)} data - Either the string to save as a text file or a Buffer that represents an image
|
|
211
|
+
* @param {*} optionsOverride - An object that can contain configurational overrides as defined in the approvals configuration object.
|
|
212
|
+
*/
|
|
213
|
+
|
|
214
|
+
export function verify(
|
|
215
|
+
dirName: string,
|
|
216
|
+
testName: string,
|
|
217
|
+
data: any,
|
|
218
|
+
optionsOverride?: any,
|
|
219
|
+
): void {
|
|
220
|
+
return verifyAndScrub(
|
|
221
|
+
dirName,
|
|
222
|
+
testName,
|
|
223
|
+
data,
|
|
224
|
+
Scrubbers.noScrubber,
|
|
225
|
+
optionsOverride,
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* You can pass as "data" any javascript object to be JSON.stringified and run verify against.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* const approvals = require('@approval-tests/approvals');
|
|
234
|
+
* approvals.verifyAndScrub(__dirname, 'sample-approval-test', { a: "some text in an object" });
|
|
235
|
+
*
|
|
236
|
+
* @param {string} dirName - Typically `__dirname` but could be the base-directory (anywhere) to store both approved and received files.
|
|
237
|
+
* @param {string} testName - A file name safe string to call the file associated with this test.
|
|
238
|
+
* @param {(string|Buffer)} data - This can be any JavaScript object/array that will be JSON.stringified before running verify
|
|
239
|
+
* @param {*} optionsOverride - An object that can contain configurational overrides as defined in the approvals configuration object.
|
|
240
|
+
*/
|
|
241
|
+
export function verifyAsJSON(
|
|
242
|
+
dirName: string,
|
|
243
|
+
testName: string,
|
|
244
|
+
data: any,
|
|
245
|
+
optionsOverride: any,
|
|
246
|
+
) {
|
|
247
|
+
return verifyAsJSONAndScrub(
|
|
248
|
+
dirName,
|
|
249
|
+
testName,
|
|
250
|
+
data,
|
|
251
|
+
Scrubbers.noScrubber,
|
|
252
|
+
optionsOverride,
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* 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.
|
|
258
|
+
|
|
259
|
+
* @example
|
|
260
|
+
* // basic approval test with a custom scrubber
|
|
261
|
+
* const approvals = require('@approval-tests/approvals');
|
|
262
|
+
* const scrubber = approvals.scrubbers.multiScrubber([
|
|
263
|
+
* function (data) {
|
|
264
|
+
* return (data || '').replace("some text", "some other text");
|
|
265
|
+
* },
|
|
266
|
+
* approvals.scrubbers.guidScrubber // to remove guids from the received data
|
|
267
|
+
* });
|
|
268
|
+
* approvals.verifyAndScrub(__dirname, 'sample-approval-test', { a: "some text in an object" }, scrubber);
|
|
269
|
+
*
|
|
270
|
+
* @param {string} dirName - Typically `__dirname` but could be the base-directory (anywhere) to store both approved and received files.
|
|
271
|
+
* @param {string} testName - A file name safe string to call the file associated with this test.
|
|
272
|
+
* @param {(string|Buffer)} data - This can be any JavaScript object/array that will be JSON.stringified before running verify
|
|
273
|
+
* @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.
|
|
274
|
+
* @param {*} optionsOverride - An object that can contain configurational overrides as defined in the approvals configuration object.
|
|
275
|
+
*/
|
|
276
|
+
|
|
277
|
+
export function verifyAsJSONAndScrub(
|
|
278
|
+
dirName: string,
|
|
279
|
+
testName: string,
|
|
280
|
+
data: any,
|
|
281
|
+
scrubber: Scrubber,
|
|
282
|
+
optionsOverride: any,
|
|
283
|
+
): void {
|
|
284
|
+
return verifyAndScrub(
|
|
285
|
+
dirName,
|
|
286
|
+
testName,
|
|
287
|
+
stringifyKeysInOrder(data),
|
|
288
|
+
scrubber,
|
|
289
|
+
optionsOverride,
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* This allows you to take full control of naming and writing files before verifying.
|
|
295
|
+
*
|
|
296
|
+
* For an example that we use to generate the docs within the readme, check out the [test/readmeTests.mts](test/readmeTests.mts) in this project.
|
|
297
|
+
*
|
|
298
|
+
* @param {Object} namer
|
|
299
|
+
* @param {Object} writer
|
|
300
|
+
* @param {Function} [reporterFactory]
|
|
301
|
+
* @param {Object} [optionsOverride]
|
|
302
|
+
*/
|
|
303
|
+
|
|
304
|
+
export function verifyWithControl(
|
|
305
|
+
namer: Namer,
|
|
306
|
+
writer: Writer,
|
|
307
|
+
reporterFactory?: ReporterLoader | null,
|
|
308
|
+
optionsOverride?: Partial<cfg.Config>,
|
|
309
|
+
) {
|
|
310
|
+
const newOptions = cfg.getConfig(optionsOverride);
|
|
311
|
+
|
|
312
|
+
const loader =
|
|
313
|
+
(reporterFactory as ReporterLoader) ||
|
|
314
|
+
function () {
|
|
315
|
+
return [ReporterFactory.loadReporter(newOptions.reporters)];
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
FileApprover.verify(namer, writer, loader, newOptions);
|
|
319
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertToFilename = convertToFilename;
|
|
4
|
+
const StringUtils_1 = require("../Utilities/StringUtils");
|
|
5
|
+
function convertToFilename(name) {
|
|
6
|
+
const forbidden = "\\/:?\"<>|' ";
|
|
7
|
+
for (let forbiddenKey of forbidden) {
|
|
8
|
+
name = StringUtils_1.StringUtils.replaceAll(name, forbiddenKey, "_");
|
|
9
|
+
}
|
|
10
|
+
return name;
|
|
11
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Scrubber } from "../Scrubbers/Scrubbers";
|
|
2
|
+
import { Namer as CoreNamer } from "./Namer";
|
|
3
|
+
import { Config } from "../config";
|
|
4
|
+
import { Reporter } from "./Reporter";
|
|
5
|
+
export type ConfigModifier = (t: any) => any;
|
|
6
|
+
declare class FileOptions {
|
|
7
|
+
private options;
|
|
8
|
+
constructor(options: Options);
|
|
9
|
+
/** @deprecated Use withFileExtension instead. This method has a typo and will be removed in a future version. */
|
|
10
|
+
withFileExtention(extensionWithDot: string): Options;
|
|
11
|
+
withFileExtension(extensionWithDot: string): Options;
|
|
12
|
+
getFileExtension(): string;
|
|
13
|
+
}
|
|
14
|
+
export declare class Options {
|
|
15
|
+
protected fields: {
|
|
16
|
+
[n: string]: any;
|
|
17
|
+
};
|
|
18
|
+
constructor();
|
|
19
|
+
modify(key: string, value: any): Options;
|
|
20
|
+
forFile(): FileOptions;
|
|
21
|
+
get<Type>(key: string, default1: () => Type): Type;
|
|
22
|
+
withScrubber(scrubber: Scrubber): Options;
|
|
23
|
+
getScrubber(): Scrubber;
|
|
24
|
+
scrub(text: string): string;
|
|
25
|
+
withConfig(configModifier: ConfigModifier): Options;
|
|
26
|
+
getConfig(config: any): Config;
|
|
27
|
+
withNamer(namer: CoreNamer): Options;
|
|
28
|
+
getNamer(): CoreNamer;
|
|
29
|
+
withReporter(reporter: Reporter): Options;
|
|
30
|
+
}
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Options = void 0;
|
|
4
|
+
const Namer_1 = require("../Namer");
|
|
5
|
+
class FileOptions {
|
|
6
|
+
constructor(options) {
|
|
7
|
+
this.options = options;
|
|
8
|
+
}
|
|
9
|
+
/** @deprecated Use withFileExtension instead. This method has a typo and will be removed in a future version. */
|
|
10
|
+
withFileExtention(extensionWithDot) {
|
|
11
|
+
return this.withFileExtension(extensionWithDot);
|
|
12
|
+
}
|
|
13
|
+
withFileExtension(extensionWithDot) {
|
|
14
|
+
return this.options.modify("FileExtension", extensionWithDot);
|
|
15
|
+
}
|
|
16
|
+
getFileExtension() {
|
|
17
|
+
return this.options.get("FileExtension", () => ".txt");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
class Options {
|
|
21
|
+
constructor() {
|
|
22
|
+
this.fields = {};
|
|
23
|
+
}
|
|
24
|
+
modify(key, value) {
|
|
25
|
+
const next = new Options();
|
|
26
|
+
for (const key1 in this.fields) {
|
|
27
|
+
next.fields[key1] = this.fields[key1];
|
|
28
|
+
}
|
|
29
|
+
next.fields[key] = value;
|
|
30
|
+
return next;
|
|
31
|
+
}
|
|
32
|
+
forFile() {
|
|
33
|
+
return new FileOptions(this);
|
|
34
|
+
}
|
|
35
|
+
get(key, default1) {
|
|
36
|
+
if (this.fields[key] == undefined) {
|
|
37
|
+
return default1();
|
|
38
|
+
}
|
|
39
|
+
return this.fields[key];
|
|
40
|
+
}
|
|
41
|
+
withScrubber(scrubber) {
|
|
42
|
+
return this.modify("Scrubber", scrubber);
|
|
43
|
+
}
|
|
44
|
+
getScrubber() {
|
|
45
|
+
return this.get("Scrubber", () => (t) => t);
|
|
46
|
+
}
|
|
47
|
+
scrub(text) {
|
|
48
|
+
const scrubber = this.getScrubber();
|
|
49
|
+
return scrubber(text);
|
|
50
|
+
}
|
|
51
|
+
withConfig(configModifier) {
|
|
52
|
+
return this.modify("ConfigModifier", configModifier);
|
|
53
|
+
}
|
|
54
|
+
getConfig(config) {
|
|
55
|
+
const modifier = this.get("ConfigModifier", () => (t) => t);
|
|
56
|
+
return modifier(config);
|
|
57
|
+
}
|
|
58
|
+
withNamer(namer) {
|
|
59
|
+
return this.modify("Namer", namer);
|
|
60
|
+
}
|
|
61
|
+
getNamer() {
|
|
62
|
+
return this.get("Namer", () => new Namer_1.Namer("", ""));
|
|
63
|
+
}
|
|
64
|
+
withReporter(reporter) {
|
|
65
|
+
const previousModifier = this.get("ConfigModifier", () => (t) => t);
|
|
66
|
+
const previousModifierWithReporter = (c) => {
|
|
67
|
+
c = previousModifier(c);
|
|
68
|
+
c.reporters = [reporter];
|
|
69
|
+
return c;
|
|
70
|
+
};
|
|
71
|
+
return this.withConfig(previousModifierWithReporter);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.Options = Options;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { Scrubber } from "../Scrubbers/Scrubbers";
|
|
2
|
+
import { Namer } from "../Namer";
|
|
3
|
+
import { Namer as CoreNamer } from "./Namer";
|
|
4
|
+
import { Config } from "../config";
|
|
5
|
+
import { Reporter } from "./Reporter";
|
|
6
|
+
export type ConfigModifier = (t: any) => any;
|
|
7
|
+
|
|
8
|
+
class FileOptions {
|
|
9
|
+
private options: Options;
|
|
10
|
+
|
|
11
|
+
constructor(options: Options) {
|
|
12
|
+
this.options = options;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** @deprecated Use withFileExtension instead. This method has a typo and will be removed in a future version. */
|
|
16
|
+
withFileExtention(extensionWithDot: string): Options {
|
|
17
|
+
return this.withFileExtension(extensionWithDot);
|
|
18
|
+
}
|
|
19
|
+
withFileExtension(extensionWithDot: string): Options {
|
|
20
|
+
return this.options.modify("FileExtension", extensionWithDot);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
getFileExtension(): string {
|
|
24
|
+
return this.options.get("FileExtension", () => ".txt");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class Options {
|
|
29
|
+
protected fields: { [n: string]: any };
|
|
30
|
+
|
|
31
|
+
constructor() {
|
|
32
|
+
this.fields = {};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
modify(key: string, value: any): Options {
|
|
36
|
+
const next = new Options();
|
|
37
|
+
for (const key1 in this.fields) {
|
|
38
|
+
next.fields[key1] = this.fields[key1];
|
|
39
|
+
}
|
|
40
|
+
next.fields[key] = value;
|
|
41
|
+
return next;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
forFile(): FileOptions {
|
|
45
|
+
return new FileOptions(this);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get<Type>(key: string, default1: () => Type): Type {
|
|
49
|
+
if (this.fields[key] == undefined) {
|
|
50
|
+
return default1();
|
|
51
|
+
}
|
|
52
|
+
return this.fields[key];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
withScrubber(scrubber: Scrubber): Options {
|
|
56
|
+
return this.modify("Scrubber", scrubber);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
getScrubber(): Scrubber {
|
|
60
|
+
return this.get("Scrubber", () => (t) => t);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
scrub(text: string): string {
|
|
64
|
+
const scrubber = this.getScrubber();
|
|
65
|
+
return scrubber(text);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
withConfig(configModifier: ConfigModifier): Options {
|
|
69
|
+
return this.modify("ConfigModifier", configModifier);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
getConfig(config: any): Config {
|
|
73
|
+
const modifier = this.get("ConfigModifier", () => (t: any) => t);
|
|
74
|
+
return modifier(config);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
withNamer(namer: CoreNamer): Options {
|
|
78
|
+
return this.modify("Namer", namer);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
getNamer(): CoreNamer {
|
|
82
|
+
return this.get("Namer", () => new Namer("", ""));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
withReporter(reporter: Reporter): Options {
|
|
86
|
+
const previousModifier = this.get("ConfigModifier", () => (t: any) => t);
|
|
87
|
+
const previousModifierWithReporter: ConfigModifier = (c) => {
|
|
88
|
+
c = previousModifier(c);
|
|
89
|
+
c.reporters = [reporter];
|
|
90
|
+
return c;
|
|
91
|
+
};
|
|
92
|
+
return this.withConfig(previousModifierWithReporter);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Config } from "./config";
|
|
2
|
+
import { Namer } from "./Core/Namer";
|
|
3
|
+
import { Writer } from "./Core/Writer";
|
|
4
|
+
import { Reporter } from "./Core/Reporter";
|
|
5
|
+
import { ReporterLoader } from "./Reporting/ReporterFactory";
|
|
6
|
+
export declare class FileApprover {
|
|
7
|
+
static verify(namer: Namer, writer: Writer, reporterFactory: ReporterLoader, options?: Partial<Config>): void;
|
|
8
|
+
}
|
|
9
|
+
export declare function verify(namer: Namer, writer: Writer, reporterFactory: () => Reporter[], options?: Partial<Config>): void;
|