From a683c042c08d7853e301b93d959c6e8dec9fb9be Mon Sep 17 00:00:00 2001 From: atanasster Date: Fri, 6 Mar 2020 19:43:12 -0500 Subject: [PATCH] feat: load test files from examples --- .../instrument/test/extract-component.test.ts | 41 ++++++++----------- core/instrument/test/loadTestFiles.ts | 17 ++++++++ 2 files changed, 33 insertions(+), 25 deletions(-) create mode 100644 core/instrument/test/loadTestFiles.ts diff --git a/core/instrument/test/extract-component.test.ts b/core/instrument/test/extract-component.test.ts index 821e94e6c..ea65bf24d 100644 --- a/core/instrument/test/extract-component.test.ts +++ b/core/instrument/test/extract-component.test.ts @@ -1,31 +1,22 @@ -import * as path from 'path'; -import * as fs from 'fs'; +import { loadTestFiles } from './loadTestFiles'; import { defaultParserOptions, defaultResolveOptions } from '../src/index'; import { extractComponent } from '../src/babel/extract-component'; describe('extract-component', () => { - const folderName = path.join(__dirname, 'examples', 'extract-component'); - const fileNames = fs.readdirSync(folderName); - // .filter(fn => fn === 'node-modules-source.js'); - fileNames.forEach(file => { - const fileName = path.join(folderName, file); - it(file, async () => { - expect( - await extractComponent('Button', fileName, undefined, { - parser: defaultParserOptions, - resolve: defaultResolveOptions, - component: { - resolveFile: (componentName: string, filePath: string) => { - if (filePath.includes('/theme-ui/dist')) { - return `${ - filePath.split('/theme-ui/dist')[0] - }/@theme-ui/components/src/${componentName}.js`; - } - return filePath; - }, - }, - }), - ).toMatchSnapshot(); + loadTestFiles(async (fileName: string) => { + return await extractComponent('Button', fileName, undefined, { + parser: defaultParserOptions, + resolve: defaultResolveOptions, + component: { + resolveFile: (componentName: string, filePath: string) => { + if (filePath.includes('/theme-ui/dist')) { + return `${ + filePath.split('/theme-ui/dist')[0] + }/@theme-ui/components/src/${componentName}.js`; + } + return filePath; + }, + }, }); - }); + }, 'extract-component'); }); diff --git a/core/instrument/test/loadTestFiles.ts b/core/instrument/test/loadTestFiles.ts new file mode 100644 index 000000000..a8f0c5a7e --- /dev/null +++ b/core/instrument/test/loadTestFiles.ts @@ -0,0 +1,17 @@ +import * as path from 'path'; +import * as fs from 'fs'; + +export type LoadTestCallbackFn = (fileName: string) => any; + +export const loadTestFiles = (callback: LoadTestCallbackFn, ...rest) => { + const folderName = path.join(__dirname, 'examples', ...rest); + const fileNames = fs.readdirSync(folderName); + + // .filter(fn => fn === 'node-modules-source.js'); + fileNames.forEach(file => { + const fileName = path.join(folderName, file); + it(file, async () => { + expect(await callback(fileName)).toMatchSnapshot(); + }); + }); +};