diff --git a/packages/typescript-to-proptypes/test/index.test.ts b/packages/typescript-to-proptypes/test/index.test.ts deleted file mode 100644 index 50caf1d17386a7..00000000000000 --- a/packages/typescript-to-proptypes/test/index.test.ts +++ /dev/null @@ -1,80 +0,0 @@ -import path from 'path'; -import fs from 'fs'; -import { expect } from 'chai'; -import glob from 'fast-glob'; -import prettier from 'prettier'; -import * as ttp from '../src'; -import { TestOptions } from './types'; - -const prettierConfig = prettier.resolveConfig.sync(path.join(__dirname, '../.prettierrc')); - -const testCases = glob.sync('**/input.{d.ts,ts,tsx}', { absolute: true, cwd: __dirname }); - -// Create program for all files to speed up tests -const program = ttp.createTSProgram( - testCases, - ttp.loadConfig(path.resolve(__dirname, '../tsconfig.json')), -); - -// eslint-disable-next-line no-restricted-syntax -for (const testCase of testCases) { - const dirname = path.dirname(testCase); - const testName = dirname.substr(__dirname.length + 1); - const outputPath = path.join(dirname, 'output.js'); - const optionsPath = path.join(dirname, 'options.ts'); - const inputJS = path.join(dirname, 'input.js'); - - it(testName, () => { - // eslint-disable-next-line import/no-dynamic-require, global-require -- TODO - const options: TestOptions = fs.existsSync(optionsPath) ? require(optionsPath).default : {}; - - const ast = ttp.parseFromProgram(testCase, program, options.parser); - - let inputSource = null; - if (testCase.endsWith('.d.ts')) { - try { - inputSource = fs.readFileSync(inputJS, 'utf8'); - } catch (error) { - // ignore - } - } else { - inputSource = ttp.ts.transpileModule(fs.readFileSync(testCase, 'utf8'), { - compilerOptions: { - target: ttp.ts.ScriptTarget.ESNext, - jsx: ttp.ts.JsxEmit.Preserve, - }, - }).outputText; - } - - let result = ''; - // For d.ts files we just generate the AST - if (!inputSource) { - result = ast.body - .map((component) => { - return ttp.generate(component, options.generator); - }) - .join('\n'); - } else { - // For .tsx? files we transpile them and inject the proptypesu - const injected = ttp.inject(ast, inputSource, options.injector); - if (!injected) { - throw new Error('Injection failed'); - } - - result = injected; - } - - const propTypes = prettier.format(result, { - ...prettierConfig, - filepath: outputPath, - }); - - if (fs.existsSync(outputPath)) { - expect(propTypes.replace(/\r?\n/g, '\n')).to.include( - fs.readFileSync(outputPath, 'utf8').replace(/\r?\n/g, '\n'), - ); - } else { - fs.writeFileSync(outputPath, propTypes); - } - }); -} diff --git a/packages/typescript-to-proptypes/test/typescript-to-proptypes.test.ts b/packages/typescript-to-proptypes/test/typescript-to-proptypes.test.ts new file mode 100644 index 00000000000000..76b2dda20ac7e3 --- /dev/null +++ b/packages/typescript-to-proptypes/test/typescript-to-proptypes.test.ts @@ -0,0 +1,103 @@ +import path from 'path'; +import fs from 'fs'; +import { expect } from 'chai'; +import glob from 'fast-glob'; +import prettier from 'prettier'; +import * as ttp from '../src'; +import { TestOptions } from './types'; + +const testCases = glob + .sync('**/input.{d.ts,ts,tsx}', { absolute: true, cwd: __dirname }) + .map((testPath) => { + const dirname = path.dirname(testPath); + const name = path.dirname(path.relative(__dirname, testPath)); + const outputPath = path.join(dirname, 'output.js'); + const inputJS = path.join(dirname, 'input.js'); + + return { + inputJS, + name, + outputPath, + inputPath: testPath, + }; + }); + +describe('typescript-to-proptypes', () => { + let cachedProgram: ttp.ts.Program; + function getProgram() { + return cachedProgram; + } + + before(() => { + // Create program for all files to speed up tests + cachedProgram = ttp.createTSProgram( + testCases.map((testCase) => testCase.inputPath), + ttp.loadConfig(path.resolve(__dirname, '../tsconfig.json')), + ); + }); + + testCases.forEach((testCase) => { + const { name: testName, inputPath, inputJS, outputPath } = testCase; + + it(testName, async () => { + const program = getProgram(); + let options: TestOptions = {}; + try { + const optionsModule = await import(`./${testName}/options`); + options = optionsModule.default; + } catch (error) { + // Assume "Cannot find module" which means "no options". + } + + const ast = ttp.parseFromProgram(inputPath, program, options.parser); + + let inputSource = null; + if (inputPath.endsWith('.d.ts')) { + try { + inputSource = fs.readFileSync(inputJS, 'utf8'); + } catch (error) { + // ignore + } + } else { + inputSource = ttp.ts.transpileModule(fs.readFileSync(inputPath, 'utf8'), { + compilerOptions: { + target: ttp.ts.ScriptTarget.ESNext, + jsx: ttp.ts.JsxEmit.Preserve, + }, + }).outputText; + } + + let result = ''; + // For d.ts files we just generate the AST + if (!inputSource) { + result = ast.body + .map((component) => { + return ttp.generate(component, options.generator); + }) + .join('\n'); + } else { + // For .tsx? files we transpile them and inject the proptypes + const injected = ttp.inject(ast, inputSource, options.injector); + if (!injected) { + throw new Error('Injection failed'); + } + + result = injected; + } + + const prettierConfig = prettier.resolveConfig.sync(outputPath); + const propTypes = prettier.format(result, { + ...prettierConfig, + filepath: outputPath, + }); + + if (fs.existsSync(outputPath)) { + expect(propTypes.replace(/\r?\n/g, '\n')).to.include( + fs.readFileSync(outputPath, 'utf8').replace(/\r?\n/g, '\n'), + ); + } else { + fs.writeFileSync(outputPath, propTypes); + } + }); + }); +});