@autorest/test-utils
Version:
Set of testing utils that are used across packages
101 lines • 4.11 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
function getAbsolutePathToSnapshot(testPath, snapshotFile) {
return path.isAbsolute(snapshotFile) ? snapshotFile : path.resolve(path.dirname(testPath), snapshotFile);
}
/**
* Helper
*/
function toMatchRawFileSnapshot(received, filename) {
if (typeof received !== "string") {
throw new Error("toMatchRawFileSnapshot is only supported with raw text");
}
if (this.isNot) {
return {
pass: true,
message: () => `.${this.utils.BOLD_WEIGHT("not")} cannot be used with snapshot matchers`,
};
}
if (!this.testPath) {
throw new Error("Unexpected matcher state, testPath is undefined");
}
const filepath = getAbsolutePathToSnapshot(this.testPath, filename);
const content = received;
const updateSnapshot = this.snapshotState._updateSnapshot;
const coloredFilename = this.utils.DIM_COLOR(filename);
const errorColor = this.utils.RECEIVED_COLOR;
if (updateSnapshot === "none" && !fs.existsSync(filepath)) {
// We're probably running in CI environment
this.snapshotState.unmatched++;
return {
pass: false,
message: () => `New output file ${coloredFilename} was ${errorColor("not written")}.\n\n` +
"The update flag must be explicitly passed to write a new snapshot.\n\n",
};
}
if (fs.existsSync(filepath)) {
const output = fs.readFileSync(filepath, "utf8").replace(/\r\n/g, "\n");
// The matcher is being used with `.not`
if (output === content) {
this.snapshotState.matched++;
return { pass: true, message: () => "" };
}
else {
if (updateSnapshot === "all") {
fs.mkdirSync(path.dirname(filepath), { recursive: true });
fs.writeFileSync(filepath, content);
this.snapshotState.updated++;
return { pass: true, message: () => "" };
}
else {
this.snapshotState.unmatched++;
return {
pass: false,
message: () => `Received content ${errorColor("doesn't match")} the file ${coloredFilename}.\n\n${this.utils.diff(output, content)}`,
};
}
}
}
else {
if (updateSnapshot === "new" || updateSnapshot === "all") {
fs.mkdirSync(path.dirname(filepath), { recursive: true });
fs.writeFileSync(filepath, content);
this.snapshotState.added++;
return { pass: true, message: () => "" };
}
else {
this.snapshotState.unmatched++;
return {
pass: true,
message: () => `The output file ${coloredFilename} ${errorColor("doesn't exist")}.`,
};
}
}
}
expect.extend({ toMatchRawFileSnapshot });
//# sourceMappingURL=file-snapshot.js.map