Skip to content

Jest attempts to transform my custom transform leading in "TypeError: Jest: a transform must export a process function" #7710

@kentcdodds

Description

@kentcdodds

Sorry, I'll add more details later. Just getting this down now so I don't forget what I've learned so far before I wrap up for the weekend.

🐛 Bug Report

I have a custom transform that uses babel-jest to create a transformer based on a certain babel config that does not exist in the typical place (this is for a toolkit where the config resides inside a package, not in the project).

To Reproduce

Steps to reproduce the behavior:

I'll create a reproduction repo next week.

Expected behavior

In jest-runtime/build/ScriptTransformer.js in _getTransformer I'm getting:

TypeError: Jest: a transform must export a `process` function

Adding some console logs around shows me that it goes through that function twice with the same file (my custom transform). Here's an outline of where the code gets to:

    if (transformPath) {
      const transformer = this._transformCache.get(transformPath);

      if (transformer != null) {
        return transformer;
      } // $FlowFixMe
      transform = require(transformPath); // 1️⃣ gets to this line the first time
      // following the code path here actually creates a module in the require.cache
      // but in the process of actually loading the module, this function is
      // called and run again!
      // which means that the second time when we try to require `transformPath`
      // a second time it comes back with a module that has a module.exports of `{}`

      if (typeof transform.createTransformer === 'function') {
        transform = transform.createTransformer();
      }
      if (typeof transform.process !== 'function') {
        // 2️⃣ gets to this line the second time
        throw new TypeError(
          `Jest: a transform must export a \`process\` function`
        );
      }
      this._transformCache.set(transformPath, transform);
    }

This is pretty unusual I think. It's not entirely clear to me why the _getTransform function is getting called twice, but my assumption is that Jest is attempting to transform the transform file. Efforts to configure jest to ignore the transform file failed so I'm missing something else.

Link to repl or repo (highly encouraged)

I'll try to add something next week.

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS 10.14.3
    CPU: (8) x64 Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz
  Binaries:
    Node: 8.15.0 - ~/code/paypal/paypal-scripts/node_modules/.bin/node
    Yarn: 1.12.3 - /usr/local/bin/yarn
    npm: 6.5.0 - ~/n/bin/npm
  npmPackages:
    jest: 24.0.0 => 24.0.0

Thanks! Looking forward to upgrading!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions