From 1750fc58194b32cd628ad551e3fe9b0e9571a3cf Mon Sep 17 00:00:00 2001
From: Kagami Sascha Rosylight <saschanaz@outlook.com>
Date: Sat, 11 Feb 2023 20:46:33 +0100
Subject: [PATCH] Fix moduleNameMapper to not resolve `.wasm.js` to `.js`

Fixes #9767

Undici [tries to import `./llhttp/llhttp.wasm.js`](https://github.com/nodejs/undici/blob/e155c6db5cec9bc577d548fa7c7378013631c79c/lib/client.js#L342) which is currently broken by the (hacky) module name mapper.
---
 packages/backend/jest-resolver.cjs | 14 --------------
 packages/backend/jest.config.cjs   | 11 +++++++++--
 2 files changed, 9 insertions(+), 16 deletions(-)
 delete mode 100644 packages/backend/jest-resolver.cjs

diff --git a/packages/backend/jest-resolver.cjs b/packages/backend/jest-resolver.cjs
deleted file mode 100644
index 4424b800dc..0000000000
--- a/packages/backend/jest-resolver.cjs
+++ /dev/null
@@ -1,14 +0,0 @@
-// https://github.com/facebook/jest/issues/12270#issuecomment-1194746382
-
-const nativeModule = require('node:module');
-
-function resolver(module, options) {
-  const { basedir, defaultResolver } = options;
-  try {
-    return defaultResolver(module, options);
-  } catch (error) {
-    return nativeModule.createRequire(basedir).resolve(module);
-  }
-}
-
-module.exports = resolver;
diff --git a/packages/backend/jest.config.cjs b/packages/backend/jest.config.cjs
index f0a3dc16c2..2f11f6a3e9 100644
--- a/packages/backend/jest.config.cjs
+++ b/packages/backend/jest.config.cjs
@@ -83,7 +83,14 @@ module.exports = {
 
 	// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
 	moduleNameMapper: {
-		"^@/(.*?).js": "<rootDir>/src/$1.ts",
+		// Do not resolve .wasm.js to .wasm by the rule below
+		'^(.+)\\.wasm\\.js$': '$1.wasm.js',
+		// SWC converts @/foo/bar.js to `../../src/foo/bar.js`, and then this rule
+		// converts it again to `../../src/foo/bar` which then can be resolved to
+		// `.ts` files.
+		// See https://github.com/swc-project/jest/issues/64#issuecomment-1029753225
+		// TODO: Use `--allowImportingTsExtensions` on TypeScript 5.0 so that we can
+		// directly import `.ts` files without this hack.
 		'^(\\.{1,2}/.*)\\.js$': '$1',
 	},
 
@@ -112,7 +119,7 @@ module.exports = {
 	// resetModules: false,
 
 	// A path to a custom resolver
-	resolver: './jest-resolver.cjs',
+	// resolver: './jest-resolver.cjs',
 
 	// Automatically restore mock state between every test
 	restoreMocks: true,