Skip to content

Commit

Permalink
Test module mock regex
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman committed Jul 19, 2024
1 parent 42dc1ab commit 6229b7f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 53 deletions.
102 changes: 51 additions & 51 deletions code/core/src/csf-tools/CsfFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { dedent } from 'ts-dedent';
import { describe, it, expect, vi } from 'vitest';
import yaml from 'js-yaml';
import { loadCsf } from './CsfFile';
import { loadCsf, isModuleMock } from './CsfFile';

expect.addSnapshotSerializer({
print: (val: any) => yaml.dump(val).trimEnd(),
Expand Down Expand Up @@ -1477,6 +1477,36 @@ describe('CsfFile', () => {
`);
});

it('mount meta', () => {
expect(
parse(
dedent`
export default {
title: 'foo/bar',
play: ({ context, mount: mountRenamed }) => {},
};
export const A = {};
`
)
).toMatchInlineSnapshot(`
meta:
title: foo/bar
tags:
- play-fn
stories:
- id: foo-bar--a
name: A
__stats:
play: true
render: false
loaders: false
beforeEach: false
storyFn: false
mount: true
moduleMock: false
`);
});

it('meta csf2', () => {
expect(
parse(
Expand Down Expand Up @@ -1933,55 +1963,25 @@ describe('CsfFile', () => {
moduleMock: true
`);
});
it('absolute', () => {
expect(
parse(
dedent`
import foo from '/path/to/bar.mock';
export default { title: 'foo/bar' };
export const A = {};
`
)
).toMatchInlineSnapshot(`
meta:
title: foo/bar
stories:
- id: foo-bar--a
name: A
__stats:
play: false
render: false
loaders: false
beforeEach: false
storyFn: false
mount: false
moduleMock: true
`);
});
it('package', () => {
expect(
parse(
dedent`
import foo from 'smart-mock';
export default { title: 'foo/bar' };
export const A = {};
`
)
).toMatchInlineSnapshot(`
meta:
title: foo/bar
stories:
- id: foo-bar--a
name: A
__stats:
play: false
render: false
loaders: false
beforeEach: false
storyFn: false
mount: false
moduleMock: false
`);
});
});
});

describe('isModuleMock', () => {
it('prefix', () => {
expect(isModuleMock('#foo.mock')).toBe(true);
expect(isModuleMock('./foo.mock')).toBe(true);
expect(isModuleMock('../foo.mock')).toBe(true);
expect(isModuleMock('/foo.mock')).toBe(true);

expect(isModuleMock('foo.mock')).toBe(false);
expect(isModuleMock('@/foo.mock')).toBe(false);
});
it('sufixes', () => {
expect(isModuleMock('#foo.mock.js')).toBe(true);
expect(isModuleMock('#foo.mock.mjs')).toBe(true);
expect(isModuleMock('#foo.mock.vue')).toBe(true);

expect(isModuleMock('#foo.mocktail')).toBe(false);
expect(isModuleMock('#foo.mock.test.ts')).toBe(false);
});
});
6 changes: 4 additions & 2 deletions code/core/src/csf-tools/CsfFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ const formatLocation = (node: t.Node, fileName?: string) => {
return `${fileName || ''} (line ${line}, col ${column})`.trim();
};

export const isModuleMock = (importPath: string) => MODULE_MOCK_REGEX.test(importPath);

const isArgsStory = (init: t.Node, parent: t.Node, csf: CsfFile) => {
let storyFn: t.Node = init;
// export const Foo = Bar.bind({})
Expand Down Expand Up @@ -133,7 +135,7 @@ const hasMount = (play: t.Node | undefined) => {
return false;
};

const MODULE_MOCK_REGEX = /^[.\/#].*\.mock(\.)?[^.]*$/i;
const MODULE_MOCK_REGEX = /^[.\/#].*\.mock($|\.[^.]*$)/i;

export interface CsfOptions {
fileName?: string;
Expand Down Expand Up @@ -573,7 +575,7 @@ export class CsfFile {
t.isArrowFunctionExpression(storyExport) || t.isFunctionDeclaration(storyExport)
);
stats.mount = hasMount(storyAnnotations.play ?? self._metaAnnotations.play);
stats.moduleMock = !!self.imports.find((fname) => MODULE_MOCK_REGEX.test(fname));
stats.moduleMock = !!self.imports.find((fname) => isModuleMock(fname));

return acc;
},
Expand Down

0 comments on commit 6229b7f

Please sign in to comment.