Fix up require-context tests

This commit is contained in:
Tom Coleman 2021-10-04 15:39:18 +11:00
parent 0bbac66b6b
commit 98c11346cf
2 changed files with 24 additions and 3 deletions

View File

@ -1,5 +1,6 @@
import path from 'path';
import { toRequireContext } from '../to-require-context';
import { normalizeStoriesEntry } from '../normalize-stories';
const testCases = [
{
@ -207,6 +208,22 @@ const testCases = [
'../src/stories/components/Icon/Icon.mdx',
],
},
{
glob: '../components/*/*/*.stories.js',
recursive: true,
validPaths: ['../components/basics/icon/Icon.stories.js'],
invalidPaths: [
'../components/icon/node_modules/icon/Icon.stories.js',
'./stories.js',
'./src/stories/Icon.stories.js',
'./Icon.stories.js',
'../src/Icon.stories.mdx',
'../components/icon/Icon.stories.js',
'../components/basics/simple/icon/Icon.stories.js',
'../src/stories/components/Icon/Icon.stories.ts',
'../src/stories/components/Icon/Icon.mdx',
],
},
{
glob: '../components/*/stories/*.js',
recursive: true,
@ -226,7 +243,9 @@ const testCases = [
describe('toRequireContext', () => {
testCases.forEach(({ glob, recursive, validPaths, invalidPaths }) => {
it(`matches only suitable paths - ${glob}`, () => {
const { path: base, recursive: willRecurse, match } = toRequireContext(glob);
const { path: base, recursive: willRecurse, match } = toRequireContext(
normalizeStoriesEntry(glob, { configDir: '/path', workingDir: '/path' })
);
const regex = new RegExp(match);
@ -236,6 +255,8 @@ describe('toRequireContext', () => {
const baseIncluded = filePath.includes(base);
const matched = regex.test(relativePath);
// console.log(filePath, relativePath, regex, matched);
return baseIncluded && matched;
}

View File

@ -1,7 +1,7 @@
import { NormalizedStoriesSpecifier } from '../types';
import { globToRegex } from './glob-to-regexp';
const toRequireContext = (specifier: NormalizedStoriesSpecifier) => {
export const toRequireContext = (specifier: NormalizedStoriesSpecifier) => {
const { directory, files } = specifier;
// The importPathMatcher is a `./`-prefixed matcher that includes the directory
@ -9,7 +9,7 @@ const toRequireContext = (specifier: NormalizedStoriesSpecifier) => {
const match = globToRegex(`./${files}`);
return {
path: directory,
recursive: files.startsWith('**'),
recursive: !!files.match(/^\*{1,2}\//),
match,
};
};