bitttttten / jest-transformer-mdx

A jest transformer for MDX with frontMatter support
12 stars 6 forks source link

"Cannot read property 'length' of undefined" in Jest 27 #22

Closed EvHaus closed 3 years ago

EvHaus commented 3 years ago

After upgrading to jest@27 and babel-jest@27 I'm now getting the following errors with this transformer:

    TypeError: Cannot read property 'length' of undefined

      at Object.createTransformer [as process] (node_modules/jest-transformer-mdx/index.js:22:42)
      at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:612:31)
      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:758:40)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:815:19)
      at Object.<anonymous> (pages/chrome/index.tsx:6:1)
bitttttten commented 3 years ago

Okay this library was last tested with jest 24 (and the last update was when jest 26 was the latest version, so it seems jest 26 could also be the last stable version). What does your jest.config.js file look like?

bitttttten commented 3 years ago

Yeah, we have to change the way we are getting the transformer's options. I will try and get this out soon :)

If you're familiar with patch-package, you should be able to patch this module's index.js file.

function createTransformer(src, filename, config) {
  const withFrontMatter = parseFrontMatter(src, config.transformerConfig);
  const jsx = mdx.sync(withFrontMatter);
  const toTransform = `import {mdx} from '@mdx-js/react';${jsx}`;

  return process(toTransform, filename, config).code;
}

Although if you're using babel-jest@27 you might even have to update the babel js import (const { process } = require("babel-jest").default;), just a guess!

bitttttten commented 3 years ago

@EvHaus can you try jest-transformer-mdx@3.0.0-beta.0 ? It might work :)

EvHaus commented 3 years ago

Nice. jest-transformer-mdx@3.0.0-beta.0 worked for me. Thanks for the super fast turnaround!

bitttttten commented 3 years ago

Released in 3.1.0 :)

bitttttten commented 3 years ago

I get errors when running the tests on CI :(

So if you hit them, let me know. I'm working on a fix but it's complicated 🙃

EvHaus commented 3 years ago

Yeah, I'm getting this on CI with 3.3.0:


Jest: synchronous transformer /myproject/node_modules/jest-transformer-mdx/index.js must export a "process" function.
--
&nbsp; | &nbsp;
&nbsp; | at invariant (node_modules/@jest/transform/build/ScriptTransformer.js:1091:11)
&nbsp; | at assertSyncTransformer (node_modules/@jest/transform/build/ScriptTransformer.js:1097:3)
&nbsp; | at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:611:7)
&nbsp; | at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:758:40)
&nbsp; | at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:815:19)

But 3.0.0-beta.0 works fine though.

bitttttten commented 3 years ago

Nice! If you can stay on the beta that would be great haha. I'll be pushing a new version soon but I need to support configuring the mdx options and that's why you get this error above (noted in https://github.com/bitttttten/jest-transformer-mdx/issues/25).