Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defineInlineTest doesn't support async transforms #454

Open
robatwilliams opened this issue Sep 24, 2021 · 2 comments
Open

defineInlineTest doesn't support async transforms #454

robatwilliams opened this issue Sep 24, 2021 · 2 comments
Labels

Comments

@robatwilliams
Copy link

Since #237, transformers can be async.

However testing such transformers using defineInlineTest doesn't appear to be supported, and from the code also looks like it won't be suppored by any type of unit test.

Error: TypeError: (output || "").trim is not a function

@MonicaOlejniczak
Copy link

I have the same problem :(

@danieldelcore
Copy link

Hey all, in case anyone finds it useful here's an alternate testing util that supports async transforms. Feel free to either copy-paste or pull the library as a dep.

The API is different to defineInlineTest because it allows for you to still have access to jest library features like it.skip, it.only etc.

applyTransform

Example:

import * as transformer from '../transform';
import { applyTransform } from '@codeshift/test-utils';

it('should wrap avatar in a tooltip if name is defined', async () => {
  const result = await applyTransform(transformer, `const foo = 'foo';`);

  expect(result).toMatchInlineSnapshot(`"const oof = 'oof';"`);
});

here's the implementation in case you would rather copy-paste:

import jscodeshift from 'jscodeshift';

type Parser = 'babel' | 'babylon' | 'flow' | 'ts' | 'tsx';

interface Options {
  parser?: Parser;
}

export default async function applyTransform(
  transform: any,
  input: string,
  options: Options = {
    parser: 'babel',
  },
) {
  // Handle ES6 modules using default export for the transform
  const transformer = transform.default ? transform.default : transform;
  const output = await transformer(
    { source: input },
    {
      jscodeshift: jscodeshift.withParser(options.parser as string),
      stats: () => {},
    },
    options || {},
  );

  return (output || '').trim();
}

@ElonVolo ElonVolo added the bug label May 6, 2022
trivikr added a commit to aws/aws-sdk-js-codemod that referenced this issue Dec 29, 2022
Refs: facebook/jscodeshift#454

TypeError: Cannot read properties of undefined (reading 'endsWith')
      1 | export const isTypeScriptFile = (filePath: string): boolean =>
    > 2 |   filePath.endsWith(".ts") || filePath.endsWith(".tsx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants