Skip to content

Commit

Permalink
chore(test-utils): check parent directories for .only (#4971)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhsf authored Nov 27, 2024
1 parent f22b9b9 commit 7ceab63
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ async function getTestModuleCode(input) {
return { code, watchFiles };
}

async function existsUp(dir, file) {
while (true) {
if (await exists(path.join(dir, file))) return true;
dir = path.join(dir, '..');
const basename = path.basename(dir);
// We should always hit __tests__, but check for system root as an escape hatch
if (basename === '__tests__' || basename === '') return false;
}
}

function createHCONFIG2JSPreprocessor(config, logger, emitter) {
const { basePath } = config;
const log = logger.create('preprocessor-lwc');
Expand All @@ -193,7 +203,7 @@ function createHCONFIG2JSPreprocessor(config, logger, emitter) {
const { code: componentDef, watchFiles: componentWatchFiles } =
await getCompiledModule(suiteDir);
// You can add an `.only` file alongside an `index.spec.js` file to make it `fdescribe()`
const onlyFileExists = await exists(path.join(suiteDir, '.only'));
const onlyFileExists = await existsUp(suiteDir, '.only');

const ssrOutput = getSsrCode(componentDef, testCode, path.join(suiteDir, 'ssr.js'));

Expand Down
14 changes: 12 additions & 2 deletions scripts/test-utils/test-fixture-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ const { globSync } = glob;

type TestFixtureOutput = { [filename: string]: unknown };

function existsUp(dir: string, file: string): boolean {
while (true) {
if (fs.existsSync(path.join(dir, file))) return true;
dir = path.join(dir, '..');
const basename = path.basename(dir);
// We should always hit __tests__, but check for system root as an escape hatch
if (basename === '__tests__' || basename === '') return false;
}
}

/**
* Facilitates the use of vitest's `test.only`/`test.skip` in fixture files.
* @param dirname fixture directory to check for "directive" files
Expand All @@ -22,8 +32,8 @@ type TestFixtureOutput = { [filename: string]: unknown };
* @example getTestOptions('/fixtures/some-test')
*/
function getTestOptions(dirname: string) {
const isOnly = fs.existsSync(path.join(dirname, '.only'));
const isSkip = fs.existsSync(path.join(dirname, '.skip'));
const isOnly = existsUp(dirname, '.only');
const isSkip = existsUp(dirname, '.skip');
if (isOnly && isSkip) {
const relpath = path.relative(process.cwd(), dirname);
throw new Error(`Cannot have both .only and .skip in ${relpath}`);
Expand Down

0 comments on commit 7ceab63

Please sign in to comment.