Skip to content

Commit fabce63

Browse files
committed
fix mysql2 test failure resulting from attempting to mix dynamic-import, require, and coping with on-the-fly .ts->.js translation for the test run
1 parent 9a2f06a commit fabce63

File tree

1 file changed

+18
-2
lines changed
  • plugins/node/opentelemetry-instrumentation-mysql2/test

1 file changed

+18
-2
lines changed

plugins/node/opentelemetry-instrumentation-mysql2/test/mysql.test.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,12 +1240,28 @@ describe('mysql2', () => {
12401240
}
12411241
});
12421242

1243-
const { MySQL2Instrumentation } = await import('../src/index.js');
1243+
// Here we want to dynamically load the instrumentation.
1244+
// - `await import('../src')` does not work with tsconfig `moduleResolution: "node16"`
1245+
// because relative imports must use a suffix
1246+
// - `await import('../src/index.js')` does not work because when running
1247+
// the test files from "./test/", instead of from "./build/test/", there
1248+
// *isn't* a "index.js" file at that relative path.
1249+
// - `await import('../build/src/index.js')` does not work because that
1250+
// is a different module, hence mismatched `MySQL2Instrumentation` types.
1251+
// We fallback to using `require`. This is what the emitted JS used when
1252+
// tsconfig was target=ES2017,module=commonjs. It also matches the
1253+
// `require.cache` deletions above.
1254+
//
1255+
// (IMO, a better solution for a clean test of `mysql2/promise` would
1256+
// be to use out-of-process testing as provided by `runTestFixture` in
1257+
// contrib-test-utils.)
1258+
const { MySQL2Instrumentation } = require('../src');
12441259
instrumentation = new MySQL2Instrumentation();
12451260
instrumentation.enable();
12461261
instrumentation.disable();
12471262

1248-
createConnection = (await import('mysql2/promise')).createConnection;
1263+
// createConnection = (await import('mysql2/promise')).createConnection;
1264+
createConnection = require('mysql2/promise').createConnection;
12491265

12501266
if (!shouldTest) {
12511267
// this.skip() workaround

0 commit comments

Comments
 (0)