本文整理汇总了TypeScript中jest-resolve.getMockModule函数的典型用法代码示例。如果您正苦于以下问题:TypeScript getMockModule函数的具体用法?TypeScript getMockModule怎么用?TypeScript getMockModule使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了getMockModule函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: _requireResolve
private _requireResolve(
from: Config.Path,
moduleName?: string,
options: ResolveOptions = {},
) {
if (moduleName == null) {
throw new Error(
'The first argument to require.resolve must be a string. Received null or undefined.',
);
}
const {paths} = options;
if (paths) {
for (const p of paths) {
const absolutePath = path.resolve(from, '..', p);
const module = this._resolver.resolveModuleFromDirIfExists(
absolutePath,
moduleName,
// required to also resolve files without leading './' directly in the path
{paths: [absolutePath]},
);
if (module) {
return module;
}
}
throw new Error(
`Cannot resolve module '${moduleName}' from paths ['${paths.join(
"', '",
)}'] from ${from}`,
);
}
try {
return this._resolveModule(from, moduleName);
} catch (err) {
const module = this._resolver.getMockModule(from, moduleName);
if (module) {
return module;
} else {
throw err;
}
}
}
示例2: catch
return dependencies.reduce<Array<Config.Path>>((acc, dependency) => {
if (this._resolver.isCoreModule(dependency)) {
return acc;
}
let resolvedDependency;
try {
resolvedDependency = this._resolver.resolveModule(
file,
dependency,
options,
);
} catch (e) {
resolvedDependency = this._resolver.getMockModule(file, dependency);
}
if (resolvedDependency) {
acc.push(resolvedDependency);
}
return acc;
}, []);
示例3: _shouldMock
private _shouldMock(from: Config.Path, moduleName: string) {
const mockPath = this._resolver.getModulePath(from, moduleName);
if (mockPath in this._virtualMocks) {
return true;
}
const explicitShouldMock = this._explicitShouldMock;
const moduleID = this._resolver.getModuleID(
this._virtualMocks,
from,
moduleName,
);
const key = from + path.delimiter + moduleID;
if (moduleID in explicitShouldMock) {
return explicitShouldMock[moduleID];
}
if (
!this._shouldAutoMock ||
this._resolver.isCoreModule(moduleName) ||
this._shouldUnmockTransitiveDependenciesCache[key]
) {
return false;
}
if (moduleID in this._shouldMockModuleCache) {
return this._shouldMockModuleCache[moduleID];
}
let modulePath;
try {
modulePath = this._resolveModule(from, moduleName);
} catch (e) {
const manualMock = this._resolver.getMockModule(from, moduleName);
if (manualMock) {
this._shouldMockModuleCache[moduleID] = true;
return true;
}
throw e;
}
if (this._unmockList && this._unmockList.test(modulePath)) {
this._shouldMockModuleCache[moduleID] = false;
return false;
}
// transitive unmocking for package managers that store flat packages (npm3)
const currentModuleID = this._resolver.getModuleID(
this._virtualMocks,
from,
);
if (
this._transitiveShouldMock[currentModuleID] === false ||
(from.includes(NODE_MODULES) &&
modulePath.includes(NODE_MODULES) &&
((this._unmockList && this._unmockList.test(from)) ||
explicitShouldMock[currentModuleID] === false))
) {
this._transitiveShouldMock[moduleID] = false;
this._shouldUnmockTransitiveDependenciesCache[key] = true;
return false;
}
return (this._shouldMockModuleCache[moduleID] = true);
}
示例4: requireMock
requireMock(from: Config.Path, moduleName: string) {
const moduleID = this._resolver.getModuleID(
this._virtualMocks,
from,
moduleName,
);
if (this._isolatedMockRegistry && this._isolatedMockRegistry[moduleID]) {
return this._isolatedMockRegistry[moduleID];
} else if (this._mockRegistry[moduleID]) {
return this._mockRegistry[moduleID];
}
const mockRegistry = this._isolatedMockRegistry || this._mockRegistry;
if (moduleID in this._mockFactories) {
return (mockRegistry[moduleID] = this._mockFactories[moduleID]());
}
const manualMockOrStub = this._resolver.getMockModule(from, moduleName);
let modulePath;
if (manualMockOrStub) {
modulePath = this._resolveModule(from, manualMockOrStub);
} else {
modulePath = this._resolveModule(from, moduleName);
}
let isManualMock =
manualMockOrStub &&
!this._resolver.resolveStubModuleName(from, moduleName);
if (!isManualMock) {
// If the actual module file has a __mocks__ dir sitting immediately next
// to it, look to see if there is a manual mock for this file.
//
// subDir1/my_module.js
// subDir1/__mocks__/my_module.js
// subDir2/my_module.js
// subDir2/__mocks__/my_module.js
//
// Where some other module does a relative require into each of the
// respective subDir{1,2} directories and expects a manual mock
// corresponding to that particular my_module.js file.
const moduleDir = path.dirname(modulePath);
const moduleFileName = path.basename(modulePath);
const potentialManualMock = path.join(
moduleDir,
'__mocks__',
moduleFileName,
);
if (fs.existsSync(potentialManualMock)) {
isManualMock = true;
modulePath = potentialManualMock;
}
}
if (isManualMock) {
const localModule: InitialModule = {
children: [],
exports: {},
filename: modulePath,
id: modulePath,
loaded: false,
};
// Only include the fromPath if a moduleName is given. Else treat as root.
const fromPath = moduleName ? from : null;
this._execModule(localModule, undefined, mockRegistry, fromPath);
mockRegistry[moduleID] = localModule.exports;
localModule.loaded = true;
} else {
// Look for a real module to generate an automock from
mockRegistry[moduleID] = this._generateMock(from, moduleName);
}
return mockRegistry[moduleID];
}
示例5: requireModule
requireModule(
from: Config.Path,
moduleName?: string,
options?: InternalModuleOptions,
isRequireActual?: boolean | null,
) {
const moduleID = this._resolver.getModuleID(
this._virtualMocks,
from,
moduleName,
);
let modulePath;
// Some old tests rely on this mocking behavior. Ideally we'll change this
// to be more explicit.
const moduleResource = moduleName && this._resolver.getModule(moduleName);
const manualMock =
moduleName && this._resolver.getMockModule(from, moduleName);
if (
(!options || !options.isInternalModule) &&
!isRequireActual &&
!moduleResource &&
manualMock &&
manualMock !== this._isCurrentlyExecutingManualMock &&
this._explicitShouldMock[moduleID] !== false
) {
modulePath = manualMock;
}
if (moduleName && this._resolver.isCoreModule(moduleName)) {
return this._requireCoreModule(moduleName);
}
if (!modulePath) {
modulePath = this._resolveModule(from, moduleName);
}
let moduleRegistry;
if (!options || !options.isInternalModule) {
if (this._moduleRegistry[modulePath] || !this._isolatedModuleRegistry) {
moduleRegistry = this._moduleRegistry;
} else {
moduleRegistry = this._isolatedModuleRegistry;
}
} else {
moduleRegistry = this._internalModuleRegistry;
}
if (!moduleRegistry[modulePath]) {
// We must register the pre-allocated module object first so that any
// circular dependencies that may arise while evaluating the module can
// be satisfied.
const localModule: InitialModule = {
children: [],
exports: {},
filename: modulePath,
id: modulePath,
loaded: false,
};
moduleRegistry[modulePath] = localModule;
if (path.extname(modulePath) === '.json') {
localModule.exports = this._environment.global.JSON.parse(
stripBOM(fs.readFileSync(modulePath, 'utf8')),
);
} else if (path.extname(modulePath) === '.node') {
localModule.exports = require(modulePath);
} else {
// Only include the fromPath if a moduleName is given. Else treat as root.
const fromPath = moduleName ? from : null;
this._execModule(localModule, options, moduleRegistry, fromPath);
}
localModule.loaded = true;
}
return moduleRegistry[modulePath].exports;
}
示例6: requireModule
requireModule(
from: Config.Path,
moduleName?: string,
options?: InternalModuleOptions,
isRequireActual?: boolean | null,
) {
const moduleID = this._resolver.getModuleID(
this._virtualMocks,
from,
moduleName,
);
let modulePath: string | undefined;
// Some old tests rely on this mocking behavior. Ideally we'll change this
// to be more explicit.
const moduleResource = moduleName && this._resolver.getModule(moduleName);
const manualMock =
moduleName && this._resolver.getMockModule(from, moduleName);
if (
(!options || !options.isInternalModule) &&
!isRequireActual &&
!moduleResource &&
manualMock &&
manualMock !== this._isCurrentlyExecutingManualMock &&
this._explicitShouldMock[moduleID] !== false
) {
modulePath = manualMock;
}
if (moduleName && this._resolver.isCoreModule(moduleName)) {
return this._requireCoreModule(moduleName);
}
if (!modulePath) {
modulePath = this._resolveModule(from, moduleName);
}
let moduleRegistry;
if (!options || !options.isInternalModule) {
if (
this._moduleRegistry.get(modulePath) ||
!this._isolatedModuleRegistry
) {
moduleRegistry = this._moduleRegistry;
} else {
moduleRegistry = this._isolatedModuleRegistry;
}
} else {
moduleRegistry = this._internalModuleRegistry;
}
const module = moduleRegistry.get(modulePath);
if (module) {
return module.exports;
}
// We must register the pre-allocated module object first so that any
// circular dependencies that may arise while evaluating the module can
// be satisfied.
const localModule: InitialModule = {
children: [],
exports: {},
filename: modulePath,
id: modulePath,
loaded: false,
};
moduleRegistry.set(modulePath, localModule);
this._loadModule(
localModule,
from,
moduleName,
modulePath,
options,
moduleRegistry,
);
return localModule.exports;
}