Skip to content

Commit 2e164e1

Browse files
authoredApr 30, 2025
test(node): Add utility to test esm & cjs instrumentation (#16159)
Today, we do not have a lot of esm-specific node integration tests. Our tests make it possible to test ESM, but we only use it very rarely, sticking to cjs tests across the suite mostly. This means that we have a bunch of gaps around our ESM support. This PR introduces a new test utility to make it easier to test stuff in ESM and CJS: ```js createEsmAndCjsTests( __dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { test('it works when importing the http module', async () => { const runner = createRunner(); // normal test as before }); }); ``` This has a few limitations based on how this works - we can make this more robust in the future, but for now it should be "OK": 1. It requires a .mjs based instrument file as well as an .mjs based scenario 2. No relative imports are supported (all content must be in these two files) 3. It simply regex replaces the esm imports with require for the CJS tests. not perfect, but it kind of works For tests that are known to fail on e.g. esm or cjs, you can configure `failsOnEsm: true`. In this case, it will fail if the test _does not fail_ (by using `test.fails()` to ensure test failure). This way we can ensure we find out if stuff starts to fail etc. To make this work, I had to re-write the test runner code a bit, because it had issues with vitest unhandled rejection handling. Due to the way we handled test completion with a promise, `test.fails` was not working as expected, because the test indeed succeeded when it failed, but the overall test run still failed because an unhandled rejection bubbled up. Now, this should work as expected... I re-wrote a few tests already to show how it works, plus added a new test that shows an ESM test failure when importing http module (😭 )
1 parent 2130d35 commit 2e164e1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+818
-703
lines changed
 

‎dev-packages/node-integration-tests/.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ module.exports = {
1212
},
1313
},
1414
{
15-
files: ['suites/**/*.ts'],
15+
files: ['suites/**/*.ts', 'suites/**/*.mjs'],
1616
parserOptions: {
1717
project: ['tsconfig.test.json'],
1818
sourceType: 'module',
19+
ecmaVersion: 'latest',
1920
},
2021
rules: {
2122
'@typescript-eslint/typedef': 'off',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
suites/**/tmp_*

0 commit comments

Comments
 (0)
Failed to load comments.