jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: `jest.unstable_mockModule` does not work with custom dynamic import wrapper

Open JounQin opened this issue 1 year ago • 3 comments

Version

29.7.0

Steps to reproduce

https://github.com/conventional-changelog/commitlint/pull/3850

yarn test-ci

Expected behavior

No error

Actual behavior

 RUNS  @commitlint/load/src/utils/load-plugin.test.ts
/Users/JounQin/Workspaces/GitHub/commitlint/@commitlint/load/src/utils/load-plugin.ts:35
                throw new MissingPluginError(pluginName, message, {
                      ^

MissingPluginError: Failed to load plugin example: Cannot find module 'commitlint-plugin-example' from '@commitlint/load/src/utils/load-plugin.ts'
    at loadPlugin (/Users/JounQin/Workspaces/GitHub/commitlint/@commitlint/load/src/utils/load-plugin.ts:35:23) {
  ['__proto__']: [Function: Error] {
    stackTraceLimit: 100,
    prepareStackTrace: [Function: prepareStackTrace]
  },
  messageTemplate: 'plugin-missing',
  messageData: {
    pluginName: 'commitlint-plugin-example',
    commitlintPath: '/Users/JounQin/Workspaces/GitHub/commitlint/@commitlint/load'
  }
}

Additional context

I've also tried 30.0.0-alpha.2 locally but still failed.

Environment

System:
    OS: macOS 14.2.1
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 18.19.0 - ~/Library/Caches/fnm_multishells/43242_1705330700273/bin/node
    Yarn: 1.22.19 - ~/Library/Caches/fnm_multishells/43242_1705330700273/bin/yarn
    npm: 10.2.3 - ~/Library/Caches/fnm_multishells/43242_1705330700273/bin/npm
  npmPackages:
    jest: ^29.7.0 => 29.7.0

JounQin avatar Jan 15 '24 15:01 JounQin

Related codes:

const dynamicImport = async <T,>(id: string): Promise<T> => {
  const imported = await import(
    path.isAbsolute(id) ? pathToFileURL(id).toString() : id
  );
  return ("default" in imported && imported.default) || imported;
};

try {
  plugin = await dynamicImport<Plugin>(longName);
} catch (pluginLoadErr) {}

JounQin avatar Jan 15 '24 15:01 JounQin

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Feb 14 '24 16:02 github-actions[bot]

ping

JounQin avatar Feb 15 '24 16:02 JounQin

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Mar 16 '24 18:03 github-actions[bot]

Can you put together a minimal reproduction?

SimenB avatar Mar 17 '24 14:03 SimenB