2019-07-27 00:41:45 +02:00

87 lines
2.3 KiB
TypeScript

import addons, { Parameters, DecoratorFunction, StoryFn } from '@storybook/addons';
import deprecate from 'util-deprecate';
import { normalize, sep } from 'upath';
import { ADD_TESTS } from './shared';
interface AddonParameters extends Parameters {
jest?: string | string[] | { disable: true };
}
const findTestResults = (
testFiles: string[],
jestTestResults: { testResults: { name: string }[] },
jestTestFilesExt: string
) =>
Object.values(testFiles).map(name => {
const fileName = `${sep}${name}${jestTestFilesExt}`;
if (jestTestResults && jestTestResults.testResults) {
const fileNamePattern = new RegExp(fileName);
return {
fileName,
name,
result: jestTestResults.testResults.find(test =>
Boolean(normalize(test.name).match(fileNamePattern))
),
};
}
return { fileName, name };
});
interface EmitAddTestsArg {
kind: string;
story: () => void;
testFiles: string[];
options: {
results: { testResults: { name: string }[] };
filesExt: string;
};
}
const emitAddTests = ({ kind, story, testFiles, options }: EmitAddTestsArg) => {
addons.getChannel().emit(ADD_TESTS, {
kind,
storyName: story,
tests: findTestResults(testFiles, options.results, options.filesExt),
});
};
export const withTests = (userOptions: {
results: any;
filesExt?: string;
}): DecoratorFunction<unknown> => {
const defaultOptions = {
filesExt: '((\\.specs?)|(\\.tests?))?(\\.[jt]sx?)?$',
};
const options = { ...defaultOptions, ...userOptions };
return (...args) => {
if (typeof args[0] === 'string') {
return deprecate((storyFn: StoryFn<unknown>, { kind }: Parameters) => {
emitAddTests({ kind, story: storyFn, testFiles: (args as any) as string[], options });
return storyFn();
}, 'Passing component filenames to the `@storybook/addon-jest` via `withTests` is deprecated. Instead, use the `jest` story parameter');
}
const [storyFn, { kind, parameters = {} }] = args;
let { jest: testFiles } = parameters;
if (typeof testFiles === 'string') {
testFiles = [testFiles];
}
if (testFiles && Array.isArray(testFiles)) {
emitAddTests({ kind, story: storyFn, testFiles, options });
}
return storyFn();
};
};
if (module && module.hot && module.hot.decline) {
module.hot.decline();
}