import { OpenMode, PathLike } from 'fs' import { FileHandle } from 'fs/promises' import { Abortable } from 'events' import { ERROR_ON_DEPRECATED_PROPS, parseNowConfigFromDirectory, parseNowConfigFromDirectoryAsync } from '../now-config' jest.mock('../fs', () => { return { FileSystem: { existsSync: function () { return true }, copySync: function () { return void 0 }, Stats: { isDirectory: () => { return false }, }, }, } }) /* eslint-disable @typescript-eslint/no-unused-vars */ describe('Now config tests', () => { const logger = { ...console, successful(...data: any[]) { console.log(data) }, } afterAll(() => { jest.resetModules() jest.clearAllMocks() }) test('parseNowConfigFromDirectoryAsync valid config test', async () => { const config = await parseNowConfigFromDirectoryAsync( 'parse-async', { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore async readFile( _path: PathLike | FileHandle, _options?: | ({ encoding?: BufferEncoding | null flag?: OpenMode | undefined } & Abortable) | BufferEncoding ): Promise { return JSON.stringify({ scope: 'x_1234_br', scopeId: 'fc1b5713c3db3110d6489a038a40dd85', fluentDir: 'src/entities', serverModulesDir: 'src/businessRules', metadataDir: 'metadata', modulePaths: { 'src/businessRules/*.ts': 'dist/modules/*.js', }, }) }, // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore async exists(_path: PathLike): Promise { return true }, }, logger ) expect(config.scope).toBe('x_1234_br') }) test('parseNowConfigAsync invalid config test', async () => { async function foo() { await parseNowConfigFromDirectoryAsync( 'parse-async-invalid', { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore async readFile( _path: PathLike | FileHandle, _options?: | ({ encoding?: BufferEncoding | null flag?: OpenMode | undefined } & Abortable) | BufferEncoding ): Promise { return JSON.stringify({}) }, // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore async exists(_path: PathLike): Promise { return true }, }, logger ) } try { await foo() } catch (error: any) { expect(error.message).toBe( 'now.config.json - error: requires property "scope", requires property "scopeId"' ) } }) test('parseNowConfig invalid transpilation config test', async () => { async function foo() { await parseNowConfigFromDirectory( 'parse-sync-invalid', { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore readFileSync( _path: PathLike | FileHandle, _options?: | ({ encoding?: BufferEncoding | null flag?: OpenMode | undefined } & Abortable) | BufferEncoding ): string { return JSON.stringify({ scope: 'x_1234_br', scopeId: 'fc1b5713c3db3110d6489a038a40dd85', fluentDir: 'src/entities', serverModulesDir: 'src/businessRules', tsconfigPath: './tsconfig.json', modulePaths: { 'src/businessRules/*.ts': 'dist/modules/*.js', }, }) }, // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore async exists(_path: PathLike): Promise { return true }, }, logger ) } try { await foo() } catch (error: any) { expect(error.message).toBe( `now.config.json - error: Cannot specify both 'modulePaths' and 'tsconfigPath'. Use 'tsconfigPath' to supply a custom tsconfig.json file to use for internal transpilation during build. Use 'modulePaths' to map paths from 'serverModulesDir' to output directory if using a separate transpilation step` ) } }) test('parseNowConfig test', async () => { const config = await parseNowConfigFromDirectory( 'parse-sync', { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore readFileSync( _path: PathLike | FileHandle, _options?: | ({ encoding?: BufferEncoding | null flag?: OpenMode | undefined } & Abortable) | BufferEncoding ): string { return JSON.stringify({ scope: 'x_1234_br', scopeId: 'fc1b5713c3db3110d6489a038a40dd87', fluentDir: 'src/entities', serverModulesDir: 'src/businessRules', metadataDir: 'metadata', modulePaths: { 'src/businessRules/*.ts': 'dist/modules/*.js', }, }) }, // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore existsSync(_path: PathLike): boolean { return true }, }, logger ) expect(config.scope).toBe('x_1234_br') }) test('Error on transpiledSourceDir', async () => { async function foo() { await parseNowConfigFromDirectory( 'parse-sync', { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore readFileSync( _path: PathLike | FileHandle, _options?: | ({ encoding?: BufferEncoding | null flag?: OpenMode | undefined } & Abortable) | BufferEncoding ): string { return JSON.stringify({ scope: 'x_1234_br', scopeId: 'fc1b5713c3db3110d6489a038a40dd87', fluentDir: 'src/entities', serverModulesDir: 'src/businessRules', transpiledSourceDir: 'dist/modules', metadataDir: 'metadata', modulePaths: { 'src/businessRules/*.ts': 'dist/modules/*.js', }, }) }, // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore existsSync(_path: PathLike): boolean { return true }, }, logger ) } try { await foo() } catch (error: any) { expect(error.message).toBe(ERROR_ON_DEPRECATED_PROPS.transpiledSourceDir) } }) })