next-page-tester
Version:
Enable DOM integration testing on Next.js pages
35 lines (34 loc) • 1.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadFile = void 0;
const path_1 = __importDefault(require("path"));
const _error_1 = require("./_error");
// Jest seems to throw errors which are not Error Instances.
// To provide better information
// we need to include the original message in the thrown error.
// See https://github.com/toomuchdesign/next-page-tester/issues/269
function seemsJestError(e) {
return Boolean(e && typeof e === 'object' && 'message' in e);
}
function loadFile({ absolutePath, }) {
try {
return require(absolutePath);
}
catch (e) {
const baseName = path_1.default.basename(absolutePath);
// @NOTE There are tests covering it but tests coverage seems to not see it
/* istanbul ignore next */
if (e instanceof Error || seemsJestError(e)) {
const error = e;
const internalError = new _error_1.InternalError(`Failed to load "${baseName}" file due to ${error.name}: ${error.message}`);
internalError.stack = error.stack;
throw internalError;
}
/* istanbul ignore next */
throw new _error_1.InternalError(`Failed to load "${baseName}"`);
}
}
exports.loadFile = loadFile;