本文整理汇总了TypeScript中jest-resolve.getModuleID函数的典型用法代码示例。如果您正苦于以下问题:TypeScript getModuleID函数的具体用法?TypeScript getModuleID怎么用?TypeScript getModuleID使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了getModuleID函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1:
config.setupFiles.forEach(filePath => {
if (filePath && filePath.includes(NODE_MODULES)) {
const moduleID = this._resolver.getModuleID(
this._virtualMocks,
filePath,
);
this._transitiveShouldMock[moduleID] = false;
}
});
示例2: setMockFactory
const mock: Jest['mock'] = (moduleName, mockFactory, options) => {
if (mockFactory !== undefined) {
return setMockFactory(moduleName, mockFactory, options);
}
const moduleID = this._resolver.getModuleID(
this._virtualMocks,
from,
moduleName,
);
this._explicitShouldMock[moduleID] = true;
return jestObject;
};
示例3: setMock
setMock(
from: string,
moduleName: string,
mockFactory: () => unknown,
options?: {virtual?: boolean},
) {
if (options && options.virtual) {
const mockPath = this._resolver.getModulePath(from, moduleName);
this._virtualMocks[mockPath] = true;
}
const moduleID = this._resolver.getModuleID(
this._virtualMocks,
from,
moduleName,
);
this._explicitShouldMock[moduleID] = true;
this._mockFactories[moduleID] = mockFactory;
}
示例4: _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);
}
示例5: 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];
}
示例6: 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;
}
示例7: 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;
}