[tests] Fix examples tests with shared util (#9415)

Co-authored-by: Sean Massa <EndangeredMassa@gmail.com>
This commit is contained in:
Steven
2023-02-10 17:32:45 -05:00
committed by GitHub
parent fdcd86d37c
commit 2b483b0fd0
3 changed files with 34 additions and 48 deletions

View File

@@ -1,7 +1,27 @@
import { basename, join } from 'path';
import { testDeployment } from '../../test/lib/deployment/test-deployment.js';
import { lstatSync, readdirSync } from 'fs';
export async function deployExample(filename: string) {
const { testDeployment } = require('../../test/lib/deployment/test-deployment.js');
const example = basename(filename).replace(/\.test\.ts$/, '');
await testDeployment(join(filename, '..', '..', '..', example));
}
export function getExamples() {
const dirname = join(__dirname, '..');
const examples = readdirSync(dirname)
.map(example =>
({
exampleName: example,
examplePath: join(dirname, example),
testPath: join(dirname, '__tests__', 'integration', `${example}.test.ts`),
})
)
.filter(o =>
!o.exampleName.startsWith('.') &&
!o.exampleName.startsWith('_') &&
o.exampleName !== 'node_modules' &&
lstatSync(o.examplePath).isDirectory()
);
return examples;
}

View File

@@ -1,23 +1,5 @@
import { existsSync, lstatSync, readdirSync } from 'fs';
import { join } from 'path';
function getExamples() {
const dirname = join(__dirname, '..', '..');
const examples = readdirSync(dirname)
.map(example =>
({
exampleName: example,
examplePath: join(dirname, example),
testPath: join(dirname, '__tests__', 'integration', `${example}.test.ts`),
})
)
.filter(o =>
!o.exampleName.startsWith('.') &&
!o.exampleName.startsWith('_') &&
lstatSync(o.examplePath).isDirectory()
);
return examples;
}
import { existsSync } from 'fs';
import { getExamples } from '../test-utils';
describe('should have test for each example', () => {
it.each(getExamples())('should exist $exampleName', async ({testPath}) => {

View File

@@ -1,35 +1,19 @@
import frameworkList from '@vercel/frameworks';
import { detectFramework } from '../src';
import { FixtureFilesystem } from './utils/fixture-filesystem';
import { readdirSync, lstatSync } from 'fs';
import { join } from 'path';
function getExamples() {
const root = join(__dirname, '..', '..', '..');
const examplesPath = join(root, 'examples');
const examples = readdirSync(examplesPath);
const exampleDirs = examples.filter(example => {
const examplePath = join(examplesPath, example);
const stat = lstatSync(examplePath);
return stat.isDirectory();
});
return exampleDirs.map(exampleDirName => {
return [exampleDirName, join(examplesPath, exampleDirName)];
});
}
import { getExamples } from '../../../examples/__tests__/test-utils';
describe('examples should be detected', () => {
const examples = getExamples();
it.each(examples)('%s', async (example, examplePath) => {
it.each(getExamples())(
'should detect $exampleName',
async ({ exampleName, examplePath }) => {
const fs = new FixtureFilesystem(examplePath);
const framework = await detectFramework({ fs, frameworkList });
if (!framework) {
throw new Error(`Framework not detected for example "${example}".`);
throw new Error(`Framework not detected for example "${exampleName}".`);
}
expect(framework).toBe(example);
});
expect(framework).toBe(exampleName);
}
);
});