node.jstypescriptjestjs

uuid is not a function while using jest


I have the following setup:

// uuid-wrapper.ts
import { v4 as uuidV4 } from 'uuid';

const uuid: () => string = uuidV4;

export { uuid };
// uuid-wrapper.spec.ts
import { uuid } from './uuid-wrapper';

describe('uuid-wrapper', () => {
    console.log(uuid());
});

This works fine runtime, but it breaks in test. I'm trying to migrate to Jest 29 and I'm getting the following error:

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

From uuid's repo I found a workaround which, after applied and my jest.config.js looks like this:

module.exports = {
    moduleNameMapper: {
        uuid: require.resolve("uuid"),
    },
};

This gives me a different error but I still have no idea what it means:

> jest uuid-wrapper --no-coverage

 FAIL  src/uuid-wrapper/uuid-wrapper.spec.ts
  ● Test suite failed to run

    TypeError: (0 , uuid_wrapper_1.uuid) is not a function

In fact, any function I export from this file (uuid-wrapper.ts) is not resolved. I have other tests that follow a similar pattern of reexporting packages but only this one breaks. I'm using uuid 9.0.0 and jest 29.1.2.

Edit: After a bit more testing, it turns out that anything I import into the test is "not a function".


Solution

  • As I suspected, the issue was in the naming. Renaming the files and directory to just wrapper solved the issue.