-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Newrelic 3123/esm test loader #141
Newrelic 3123/esm test loader #141
Conversation
Signed-off-by: mrickard <[email protected]>
…dules Signed-off-by: mrickard <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great start!
…efined in process.env.loader Signed-off-by: mrickard <[email protected]>
lib/versioned/runner.js
Outdated
if (process.env.packageType && process.env.packageType === 'module') { | ||
// The default loader is in the agent root, so this uses process.cwd() instead of __directory. | ||
const cwd = process.cwd().slice(0, process.cwd().indexOf('/test/')) | ||
let loaderPath = path.resolve(`${cwd}/`, 'esm-loader.mjs') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure there's a time we'd use the agent loader. At the very least it'd be a test loader. In my branch i called it test/lib/test-loader.mjs
. I was going to explore using testdouble to mock the agent API but regardless you'd need to call a loader and if we used testdouble we wouldn't be able to call our loader with that so we'd have to chain them which is fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid having to pass in the test loader I see we have a few options here:
- set the default to the agent's
test/lib/test-loader.mjs
- move the test loader into the versioned repo and reference that as the default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I verified passing in env var works:
loader=./test/lib/test-loader.mjs npm run versioned express-esm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense to use the test loader. Default to test/lib/test-loader.mjs
is fine, I'd think.
lib/versioned/runner.js
Outdated
// The default loader is in the agent root, so this uses process.cwd() instead of __directory. | ||
const cwd = process.cwd().slice(0, process.cwd().indexOf('/test/')) | ||
let loaderPath = path.resolve(`${cwd}/`, 'esm-loader.mjs') | ||
if (process.env.loader) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe make this something that could be less of a conflict because technically this is opened source.
NR_LOADER
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we prefer the more-terse NR_
or something like NEW_RELIC_LOADER
? (Personally, I'd prefer terse.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good to me
Signed-off-by: mrickard <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just thinking through test coverage now and put a few thoughts down
lib/versioned/test.js
Outdated
@@ -80,6 +80,7 @@ function Test(directory, pkgVersions, opts = {}) { | |||
this.duration = 0 | |||
this.allPkgs = !!allPkgs | |||
this.strict = !!opts.strict | |||
this.type = pkg.type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have unit test for this file, might be good to add some assertions around type.
lib/versioned/runner.js
Outdated
@@ -97,7 +98,19 @@ function installPackages(cb) { | |||
function runTests(cb) { | |||
// TODO: Add tap arguments, such as color. | |||
process.send({ status: 'running' }) | |||
spawn('node', [testFile], function testHandler(err) { | |||
let args = [testFile] | |||
if (process.env.packageType && process.env.packageType === 'module') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thinking more of this we could just default the type in lib/versioned/test.js to commonjs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially did do that--it's easy enough to restore!
lib/versioned/runner.js
Outdated
let args = [testFile] | ||
if (process.env.packageType && process.env.packageType === 'module') { | ||
// The test loader is defined in the agent, so this uses process.cwd() instead of __directory. | ||
const cwd = process.cwd().slice(0, process.cwd().indexOf('/test/')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's unfortunate we've designed a runner that is very hard to unit test. but i confirmed this works manually. but thinking more of this if we shift all this logic to the test we could test it and hten just pass the loader
in the spawn, thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking this through...this code is only necessary here to get the directory of the agent running it. It's kind of awkward, and I agree that would be better handled by the agent.
If we added it in the agent code as an environment variable passed to the spawn there, I'd think we could just rely on the loader path env var.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand what you're saying. I was just thinking of moving this logic from lib/versioned/runner
to lib/versioned/test
so we could test the logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was reading that as a recommendation to move this code entirely out of runner and into the node agent. I can agree with adding it to this repos tests to test the logic...though the runner is still going to need to know where to find a loader when this runner is kicking off versioned tests, so some version of pathway detection will still need to be here. (Though if we're bundling a test loader in this repo, we could use __dirname
instead of the path manipulation here.)
@@ -99,7 +99,7 @@ function runTests(cb) { | |||
// TODO: Add tap arguments, such as color. | |||
process.send({ status: 'running' }) | |||
let args = [testFile] | |||
if (process.env.NR_LOADER) { | |||
if (process.env.nr_esm_loader) { | |||
const loaderPath = path.resolve(process.env.NR_LOADER) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the wrong env var now. should we unset this env var after every run so subsequent suites will not inherit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd agree that we should unset if we want to be sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd think this should be failing--there's a disjuncture between the env vars on 102 and 103.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is out of date. it's been updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verified on full runs of versioned tests that have both esm and cjs tests. Great work! Can you start preparing a minor release after the CI on main passes so we can get this updated in main agent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry i broke the build. committed the missing file
Proposed Release Notes
Added the capacity of running the ESM loader ESM module tests using this project's test runner.
Links
https://issues.newrelic.com/browse/NEWRELIC-3123
Details
This change requires the default ESM loader from newrelic/node-newrelic#1348, or an equivalent ESM loader defined as a path from the agent project root as the value of
process.env.NR_LOADER
.