Created
February 14, 2023 15:18
-
-
Save sod/07078bb93a292f0a9ebf5a246b34828d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getContentsRelativeFromOrigin(filename: string | undefined, depth: number) { | |
let path = '.'; | |
if (!filename?.startsWith('/')) { | |
const stack = new Error().stack!.split('\n'); | |
const stack3 = stack[depth].split('('); | |
path = stack3.pop()?.replace?.(/(:\d+)*\).*$/, '') ?? '.'; | |
} | |
return () => { | |
// @ts-ignore | |
const {dirname, resolve} = require('path'); | |
const file = path ? resolve(dirname(path), filename) : filename; | |
// @ts-ignore | |
return require(file); | |
}; | |
} | |
jest.mock('@angular/core', () => { | |
const originalModule = jest.requireActual('@angular/core'); | |
const mocked = new WeakMap<object, object>(); | |
type ComponentType = {templateUrl?: string; styleUrls?: string[]; styles?: string[]; template?: string}; | |
const Component = Object.assign(function (definition: ComponentType, ...rest: unknown[]) { | |
let mockedDefinition: ComponentType | undefined = mocked.get(definition); | |
if (!mockedDefinition) { | |
mockedDefinition = {...definition}; | |
if (mockedDefinition.templateUrl) { | |
Object.defineProperty(mockedDefinition, 'template', { | |
get: getContentsRelativeFromOrigin(mockedDefinition.templateUrl, 3), | |
}); | |
delete mockedDefinition.templateUrl; | |
} | |
if (mockedDefinition.styleUrls) { | |
const styles = mockedDefinition.styleUrls.map((url) => getContentsRelativeFromOrigin(url, 5)); | |
Object.defineProperty(mockedDefinition, 'styles', { | |
get: () => styles.map((fn) => fn()), | |
}); | |
delete mockedDefinition.styleUrls; | |
} | |
mocked.set(definition, mockedDefinition); | |
} | |
return originalModule.Component(mockedDefinition, ...rest); | |
}, originalModule.Component); | |
return { | |
...originalModule, | |
Component, | |
}; | |
}); | |
export const enableJestAngularComponentSupport = () => {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment