Skip to content

Commit

Permalink
- Updating defineInlineTest, runInlineTest, defineSnapshotTest, and r…
Browse files Browse the repository at this point in the history
…unSnapshotTest to accept a module or Transform (DefinitelyTyped#59724)

- Updating tests
  • Loading branch information
JohnDaly authored Apr 26, 2022
1 parent 960c1d2 commit 03de8be
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 42 deletions.
40 changes: 35 additions & 5 deletions types/jscodeshift/src/testUtils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ export interface TestOptions {
}

export function applyTransform(
module: { default: Transform; parser: TestOptions['parser'] } | Transform,
module:
| {
default: Transform;
parser: TestOptions['parser'];
}
| Transform,
options: Options | null | undefined,
input: {
path?: string;
Expand All @@ -31,15 +36,25 @@ export function runTest(
): string;

export function defineInlineTest(
module: Transform,
module:
| {
default: Transform;
parser: TestOptions['parser'];
}
| Transform,
options: Options,
inputSource: string,
expectedOutputSource: string,
testName?: string,
): void;

export function runInlineTest(
module: Transform,
module:
| {
default: Transform;
parser: TestOptions['parser'];
}
| Transform,
options: Options,
input: {
path?: string;
Expand All @@ -49,10 +64,25 @@ export function runInlineTest(
testOptions?: TestOptions,
): string;

export function defineSnapshotTest(module: Transform, options: Options, input: string, testName?: string): void;
export function defineSnapshotTest(
module:
| {
default: Transform;
parser: TestOptions['parser'];
}
| Transform,
options: Options,
input: string,
testName?: string,
): void;

export function runSnapshotTest(
module: Transform,
module:
| {
default: Transform;
parser: TestOptions['parser'];
}
| Transform,
options: Options,
input: {
path?: string;
Expand Down
106 changes: 69 additions & 37 deletions types/jscodeshift/test/jscodeshift-tests.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import {
ASTNode,
FileInfo,
API,
Transform,
Parser,
JSCodeshift,
Collection,
ImportDeclaration,
} from 'jscodeshift';
import { ASTNode, FileInfo, API, Transform, Parser, JSCodeshift, Collection, ImportDeclaration } from 'jscodeshift';
import * as testUtils from 'jscodeshift/src/testUtils';

// Can define transform with `function`.
Expand Down Expand Up @@ -55,27 +46,26 @@ const transformWithRecastParseOptions: Transform = (file, { j }) => {
};

const transformImportSpecifier: Transform = (file, { j }) => {
return j(file.source).find(j.ImportDeclaration, {
source: { value: 'specific-library' },
specifiers: specifiers =>
specifiers?.some(
specifier => specifier.type === 'ImportSpecifier' && specifier.imported.name === 'import-to-remove',
) || false,
})
.replaceWith((path) => {
const specifiersExceptImportToRemove =
path.node.specifiers?.filter(
(specifier) => !(specifier.type === 'ImportSpecifier' && specifier.imported.name === 'import-to-remove')
) ?? [];

return specifiersExceptImportToRemove.length > 0
? j.importDeclaration(
specifiersExceptImportToRemove,
path.node.source,
path.node.importKind
)
: null;
}).toSource();
return j(file.source)
.find(j.ImportDeclaration, {
source: { value: 'specific-library' },
specifiers: specifiers =>
specifiers?.some(
specifier => specifier.type === 'ImportSpecifier' && specifier.imported.name === 'import-to-remove',
) || false,
})
.replaceWith(path => {
const specifiersExceptImportToRemove =
path.node.specifiers?.filter(
specifier =>
!(specifier.type === 'ImportSpecifier' && specifier.imported.name === 'import-to-remove'),
) ?? [];

return specifiersExceptImportToRemove.length > 0
? j.importDeclaration(specifiersExceptImportToRemove, path.node.source, path.node.importKind)
: null;
})
.toSource();
};

const transformExportDefaultArrow: Transform = (file, { j }) => {
Expand Down Expand Up @@ -142,21 +132,45 @@ function getFileDefaultImport(file: FileInfo, j: JSCodeshift): Collection<Import
}

// Can apply a transform passed as module
testUtils.applyTransform({ default: transformImportSpecifier, parser: 'ts' }, null, { source: "import test from 'test';"});
testUtils.applyTransform({ default: transformImportSpecifier, parser: 'ts' }, null, {
source: "import test from 'test';",
});

// Can apply a transform passed as function
testUtils.applyTransform(transformImportSpecifier, {}, { source: "import test from 'test';", path: '/file/path' }, { parser: 'babylon' });
testUtils.applyTransform(
transformImportSpecifier,
{},
{ source: "import test from 'test';", path: '/file/path' },
{ parser: 'babylon' },
);

// Can define a test
testUtils.defineTest('directory', 'transformName', { opt: true }, undefined, { parser: 'tsx' });

// Can run a test
testUtils.runTest('dirname', 'transformName', {});

// Can define an inline test
// Can define an inline test with transform passed as module
testUtils.defineInlineTest(
{ default: () => {}, parser: 'babel' },
{ opt: true },
"import test from 'test';",
"import test from './test';",
);

// Can define an inline test with transform passed as function
testUtils.defineInlineTest(() => {}, { opt: true }, "import test from 'test';", "import test from './test';");

// Can run an inline test
// Can run an inline test with transform passed as module
testUtils.runInlineTest(
{ default: () => {}, parser: 'babel' },
{ opt: true },
{ source: "import test from 'test';" },
"import test from './test';",
{},
);

// Can run an inline test with transform passed as function
testUtils.runInlineTest(
() => {},
{ opt: true },
Expand All @@ -165,7 +179,18 @@ testUtils.runInlineTest(
{},
);

// Can define a snapshot test
// Can define a snapshot test with transform passed as module
testUtils.defineSnapshotTest(
{ default: reverseIdentifiersTransform, parser: 'babel' },
{},
`
var firstWord = 'Hello ';
var secondWord = 'world';
var message = firstWord + secondWord;
`,
);

// Can define a snapshot test with transform passed as function
testUtils.defineSnapshotTest(
reverseIdentifiersTransform,
{},
Expand All @@ -176,5 +201,12 @@ var message = firstWord + secondWord;
`,
);

// Can run a snapshot test
// Can run a snapshot test with transform passed as module
testUtils.runSnapshotTest(
{ default: reverseIdentifiersTransform, parser: 'babel' },
{},
{ source: "var firstWord = 'Hello ';" },
);

// Can run a snapshot test with transform passed as function
testUtils.runSnapshotTest(reverseIdentifiersTransform, {}, { source: "var firstWord = 'Hello ';" });

0 comments on commit 03de8be

Please sign in to comment.