diff --git a/lib/render-html.js b/lib/render-html.js index c5065f80..ca25d9a1 100644 --- a/lib/render-html.js +++ b/lib/render-html.js @@ -1,6 +1,8 @@ -const tryResolve = (...args) => { +import { join } from 'path'; + +const tryRequire = (...args) => { try { - return require.resolve(...args); + return require(...args); } catch (err) { return false; } @@ -8,25 +10,20 @@ const tryResolve = (...args) => { export default async ({ resume, themePath }) => { const cwd = process.cwd(); - let path; + let theme; if (themePath[0] === '.') { - path = tryResolve(path.join(cwd, themePath), { paths: [cwd] }); - throw new Error( - `Theme ${themePath} could not be resolved relative to ${cwd}`, - ); - } - if (!path) { - path = tryResolve(themePath, { paths: [cwd] }); + theme = tryRequire(join(cwd, themePath)); + } else { + theme = tryRequire(themePath); } - if (!path && /^[a-z0-9]/i.test(path)) { - path = tryResolve(`jsonresume-theme-${themePath}`, { paths: [cwd] }); + if (!theme && /^[a-z0-9-]+$/i.test(themePath)) { + theme = tryRequire(`jsonresume-theme-${themePath}`); } - if (!path) { + if (!theme) { throw new Error( - `theme path ${themePath} could not be resolved from current working directory`, + `theme path ${themePath} could not be resolved. cwd is ${cwd}`, ); } - const theme = require(path); if (typeof theme?.render !== 'function') { throw new Error('theme.render is not a function'); } diff --git a/lib/render-html.test.js b/lib/render-html.test.js index 78038ad9..616ff724 100644 --- a/lib/render-html.test.js +++ b/lib/render-html.test.js @@ -1,21 +1,27 @@ import renderHTML from './render-html'; -describe('renderHTML', () => { - beforeAll(() => { - const originalRequireResolve = require.resolve; - const mockThemePath = 'mock/path/to/jsonresume-theme-even'; - require.resolve = (...args) => { - if (args[0] === 'jsonresume-theme-even') { - return mockThemePath; - } - if (args[0] === 'jsonresume-theme-even') { - return mockThemePath; - } - return originalRequireResolve.apply(require, ...args); +jest.mock( + 'jsonresume-theme-even', + () => { + return { + render: () => 'hello from theme even', }; - require.cache[mockThemePath] = { - render: () => 'here-is-your-mocked-theme', + }, + { virtual: true }, +); +jest.mock( + '/the/mocked/cwd/some-local-theme', + () => { + return { + render: () => 'hello from the local mocked theme', }; + }, + { virtual: true }, +); +describe('renderHTML', () => { + beforeAll(() => { + const localPath = '/the/mocked/cwd/'; + process.cwd = jest.fn().mockReturnValue(localPath); }); const resume = { basics: { @@ -31,17 +37,22 @@ describe('renderHTML', () => { ).rejects.toBeTruthy(); }); - describe('should render html when theme is availlable', () => { - it('with long theme name', async () => { + describe('when theme is availlable', () => { + it('should resolve from cwd when themePath starts with a period', async () => { + expect( + await renderHTML({ resume, themePath: './some-local-theme' }), + ).toMatchInlineSnapshot(`"hello from the local mocked theme"`); + }); + it('should render html with long theme name', async () => { expect( await renderHTML({ resume, themePath: 'jsonresume-theme-even' }), - ).toStartWith(''); + ).toMatchInlineSnapshot(`"hello from theme even"`); }); - it('with short theme name', async () => { - expect(await renderHTML({ resume, themePath: 'even' })).toStartWith( - '', - ); + it('should render html with short theme name', async () => { + expect( + await renderHTML({ resume, themePath: 'even' }), + ).toMatchInlineSnapshot(`"hello from theme even"`); }); }); });