Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions code/core/src/actions/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ const logActionsWhenMockCalled: LoaderFunction = (context) => {
onMockCall((mock, args) => {
const name = mock.getMockName();

// Default name provided by vi.fn(), which we don't want to log.
if (name === 'spy') {
return;
}

// Escape hatch allowing users to disable logging for specific spies.
// This is what Vitest sets when you pass `mockName('')` to a spy.
if (name === 'vi.fn()') {
return;
}

// TODO: Make this a configurable API in 8.2
if (
!/^next\/.*::/.test(name) ||
Expand Down
309 changes: 309 additions & 0 deletions code/core/src/csf-tools/ConfigFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,315 @@ describe('ConfigFile', () => {
});
});

describe('removeImport', () => {
it(`removes a default require import`, () => {
const source = dedent`
const path = require('path');
const config: StorybookConfig = { };
export default config;
`;

const config = loadConfig(source).parse();
config.removeImport('path', 'path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(`
const config: StorybookConfig = { };
export default config;
`);
});

it(`removes a default ES6 import`, () => {
const source = dedent`
import path from 'path';
const config: StorybookConfig = { };
export default config;
`;

const config = loadConfig(source).parse();
config.removeImport('path', 'path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(`
const config: StorybookConfig = { };
export default config;
`);
});

it(`removes a named require import`, () => {
const source = dedent`
const { dirname, basename } = require('path');
const config: StorybookConfig = { };
export default config;
`;

const config = loadConfig(source).parse();
config.removeImport(['dirname'], 'path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(`
const {
basename,
} = require('path');
const config: StorybookConfig = { };
export default config;
`);
});

it(`removes a named ES6 import`, () => {
const source = dedent`
import { dirname, basename } from 'path';
const config: StorybookConfig = { };
export default config;
`;

const config = loadConfig(source).parse();
config.removeImport(['dirname'], 'path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(`
import { basename } from 'path';
const config: StorybookConfig = { };
export default config;
`);
});

it(`removes multiple named require imports`, () => {
const source = dedent`
const { dirname, basename, join } = require('path');
const config: StorybookConfig = { };
export default config;
`;

const config = loadConfig(source).parse();
config.removeImport(['dirname', 'basename'], 'path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(`
const {
join,
} = require('path');
const config: StorybookConfig = { };
export default config;
`);
});

it(`removes multiple named ES6 imports`, () => {
const source = dedent`
import { dirname, basename, join } from 'path';
const config: StorybookConfig = { };
export default config;
`;

const config = loadConfig(source).parse();
config.removeImport(['dirname', 'basename'], 'path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(`
import { join } from 'path';
const config: StorybookConfig = { };
export default config;
`);
});

it(`removes a namespace ES6 import`, () => {
const source = dedent`
import * as path from 'path';
const config: StorybookConfig = { };
export default config;
`;
const config = loadConfig(source).parse();
config.removeImport({ namespace: 'path' }, 'path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(`
const config: StorybookConfig = { };
export default config;
`);
});

it(`preserves default import when removing a namespace ES6 import`, () => {
const source = dedent`
import path, * as alsoPath from 'path';
const config: StorybookConfig = { };
export default config;
`;
const config = loadConfig(source).parse();
config.removeImport({ namespace: 'alsoPath' }, 'path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(`
import path from 'path';
const config: StorybookConfig = { };
export default config;
`);
});

it(`removes entire require declaration when all named imports are removed`, () => {
const source = dedent`
const { dirname, basename } = require('path');
const config: StorybookConfig = { };
export default config;
`;

const config = loadConfig(source).parse();
config.removeImport(['dirname', 'basename'], 'path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(`
const config: StorybookConfig = { };
export default config;
`);
});

it(`removes entire ES6 declaration when all named imports are removed`, () => {
const source = dedent`
import { dirname, basename } from 'path';
const config: StorybookConfig = { };
export default config;
`;

const config = loadConfig(source).parse();
config.removeImport(['dirname', 'basename'], 'path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(`
const config: StorybookConfig = { };
export default config;
`);
});

it(`handles node: prefix in require imports`, () => {
const source = dedent`
const { dirname } = require('path');
const config: StorybookConfig = { };
export default config;
`;

const config = loadConfig(source).parse();
config.removeImport(['dirname'], 'node:path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(`
const config: StorybookConfig = { };
export default config;
`);
});

it(`handles node: prefix in ES6 imports`, () => {
const source = dedent`
import { dirname } from 'path';
const config: StorybookConfig = { };
export default config;
`;

const config = loadConfig(source).parse();
config.removeImport(['dirname'], 'node:path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(`
const config: StorybookConfig = { };
export default config;
`);
});

it(`does nothing when require import does not exist`, () => {
const source = dedent`
const config: StorybookConfig = { };
export default config;
`;

const config = loadConfig(source).parse();
config.removeImport('path', 'path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(source);
});

it(`does nothing when trying to remove non-existent named require import`, () => {
const source = dedent`
const { dirname } = require('path');
const config: StorybookConfig = { };
export default config;
`;

const config = loadConfig(source).parse();
config.removeImport(['nonexistent'], 'path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(source);
});

it(`does nothing when trying to remove non-existent named ES6 import`, () => {
const source = dedent`
import { dirname } from 'path';
const config: StorybookConfig = { };
export default config;
`;

const config = loadConfig(source).parse();
config.removeImport(['nonexistent'], 'path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(source);
});

it(`removes ES6 import while preserving require import`, () => {
const source = dedent`
import { readFile } from 'fs';
const { dirname } = require('path');
const config: StorybookConfig = { };
export default config;
`;

const config = loadConfig(source).parse();
config.removeImport(['readFile'], 'fs');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(`
const { dirname } = require('path');
const config: StorybookConfig = { };
export default config;
`);
});

it(`removes require import while preserving ES6 import`, () => {
const source = dedent`
import { readFile } from 'fs';
const { dirname } = require('path');
const config: StorybookConfig = { };
export default config;
`;

const config = loadConfig(source).parse();
config.removeImport(['dirname'], 'path');

const parsed = babelPrint(config._ast);

expect(parsed).toMatchInlineSnapshot(`
import { readFile } from 'fs';
const config: StorybookConfig = { };
export default config;
`);
});
});

describe('removeEntryFromArray', () => {
it('removes a string literal entry', () => {
const source = dedent`
Expand Down
Loading