Skip to content
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

feat: add ESM support to jasmine_test() #41

Merged
merged 1 commit into from
Mar 9, 2023
Merged

Conversation

dgp1130
Copy link
Contributor

@dgp1130 dgp1130 commented Mar 1, 2023

Fixes #33.

This introduces a new module option which chooses between CommonJS and ES modules. Based on this input, jasmine_test() will choose the correct format of the JUnit reporter. This does not enforce that actual tests are executed as ES modules, those still need to be configured in Jasmine's config file. See: https://jasmine.github.io/setup/nodejs.html#using-es-modules.

I tried to keep things simple and just duplicated the reporter with native import statements. There's a bit of unnecessary duplication there, but I think the file is simple enough that it doesn't really matter too much. Fortunately the JUnit reporter is the only thing which needs to be changed to be compatible with ESM. As long as you name the test files .mjs, Jasmine will load them via dynamic import.

dgp1130 added a commit to dgp1130/rules_prerender that referenced this pull request Mar 1, 2023
dgp1130 added a commit to dgp1130/rules_prerender that referenced this pull request Mar 1, 2023
dgp1130 added a commit to dgp1130/rules_prerender that referenced this pull request Mar 1, 2023
dgp1130 added a commit to dgp1130/rules_prerender that referenced this pull request Mar 1, 2023
dgp1130 added a commit to dgp1130/rules_prerender that referenced this pull request Mar 1, 2023
dgp1130 added a commit to dgp1130/rules_prerender that referenced this pull request Mar 1, 2023
This fixes aspect-build/rules_jasmine#33 and adds ESM support. I've sent a PR in aspect-build/rules_jasmine#41 to apply the fix upstream, but for now a patch will have to do.
dgp1130 added a commit to dgp1130/rules_prerender that referenced this pull request Mar 4, 2023
This fixes aspect-build/rules_jasmine#33 and adds ESM support. I've sent a PR in aspect-build/rules_jasmine#41 to apply the fix upstream, but for now a patch will have to do.
dgp1130 added a commit to dgp1130/rules_prerender that referenced this pull request Mar 4, 2023
This fixes aspect-build/rules_jasmine#33 and adds ESM support. I've sent a PR in aspect-build/rules_jasmine#41 to apply the fix upstream, but for now a patch will have to do.
dgp1130 added a commit to dgp1130/rules_prerender that referenced this pull request Mar 4, 2023
This fixes aspect-build/rules_jasmine#33 and adds ESM support. I've sent a PR in aspect-build/rules_jasmine#41 to apply the fix upstream, but for now a patch will have to do.
dgp1130 added a commit to dgp1130/rules_prerender that referenced this pull request Mar 4, 2023
Refs #33.

This fixes aspect-build/rules_jasmine#33 and adds ESM support. I've sent a PR in aspect-build/rules_jasmine#41 to apply the fix upstream, but for now a patch will have to do.
@gonzojive
Copy link

Worked for me. Thanks for this!

@jbedard
Copy link
Member

jbedard commented Mar 7, 2023

Do you have a test that can be added? Something that fails without this...

@dgp1130 dgp1130 force-pushed the esm branch 2 times, most recently from de21ccc to 84e9aad Compare March 8, 2023 02:42
@dgp1130
Copy link
Contributor Author

dgp1130 commented Mar 8, 2023

Do you have a test that can be added? Something that fails without this...

Thanks for bringing this up, totally forgot about that. Actually looking at existing test cases, it seems they all already use ESM. I was a bit surprised about that and started digging a bit more into the failure I was seeing. Turns out I was experiencing a bug where junit_reporter.js incorrectly uses the "type": "module" from the root package.json file of the user's workspace. The reasoning for this is because files are laid out like so at runtime:

.../execroot/${my_wksp}/bazel-out/${cfg}/bin/external/jasmine/junit_reporter.js
.../execroot/${my_wksp}/bazel-out/${cfg}/bin/path/to/pkg/index.js
.../execroot/${my_wksp}/bazel-out/${cfg}/bin/package.json

The package.json file comes from the user's workspace at //:package.json and is intended to apply to only their code. However, Node walks up external/jasmine/junit_reporter.js and finds the same package.json, applying its configuration to the JUnit reporter. If the user puts "type": "module" in their package.json, it also applies to the JUnit reporter, which fails because it is authored in CommonJS.

There are a few potential solutions to this, but I think the best is to actually ship an empty package.json file at external/jasmine/package.json. That way we're protected from any configuration in there such as exports or imports which might cause similar problems.

Ironically, //e2e/jasmine_test/... had an exactly correct test case, but wasn't being run on CI and was failing at HEAD. I enabled that test and fixed it up to serve as a regression test. I also noticed that all the existing tests actually use ESM, so I made a CommonJS test leveraging the default behavior to make sure that wasn't broken as well.

We could probably migrate junit_reporter.js and runner.js to ESM or rename the files to .cjs. runner.js is a little more complicated so I didn't want to migrate that since it wasn't strictly necessary. I also didn't bother renaming the files since we'd want the package.json anyways to avoid future configuration issues. I ultimately opted not to migrate junit_reporter.js to ESM either for consistency with runner.js, but if we really want to migrate the source content for each, I can take a pass at that.

I suspect this same bug of having the user's package.json configuration apply to external workspaces could be causing other issues in other rulesets. I recently tried adding exports to my package.json the other day and encountered some very strange behavior which I haven't been able to narrow down to a specific bug I could file just yet. I wonder if that might have a similar root cause and wonder if we'll want to make this same change to all the other Aspect rulesets?

@jbedard
Copy link
Member

jbedard commented Mar 8, 2023

Great 👍

@jbedard
Copy link
Member

jbedard commented Mar 8, 2023

Looks like the e2e test is failing atm though?

At runtime, the JUnit reporter exists at `.../execroot/${wksp}/bazel-out/${cfg}/bin/external/jasmine/junit_reporter.js` with no included `package.json`. This means Node walks up that tree looking for a `package.json` file and typically won't find one. However, if users include a `//:package.json` file in their own workspace, it will be visible at `.../execroot/${wksp}/bazel-out/${cfg}/bin/package.json`, meaning it will be picked up as owning `junit_reporter.js`. This means that any user-specified `type: "module"` applies to `junit_reporter.js`, and caused it to be loaded as ESM, even when it was authored in CommonJS.

The solution here is to copy over a `package.json` we own to `external/jasmine/package.json`. This way any JavaScript loaded will have a stable `package.json` which uses CommonJS.

Ironically, `//e2e/jasmine_test/` actually already exercised this exact test case, but wasn't running on CI. So this commit also re-enables that test and fixes it. Actually all the test cases use ESM, so I also added `//examples/commonjs/...` and left the "default" behavior there to ensure that doesn't break in the future.
@dgp1130
Copy link
Contributor Author

dgp1130 commented Mar 9, 2023

Ah didn't realize the CI workflow expected the example to have a .bazelrc file. Added.

@jbedard
Copy link
Member

jbedard commented Mar 9, 2023

@gonzojive does this latest change work for you?

gonzojive added a commit to gonzojive/rules_ts_proto that referenced this pull request Mar 9, 2023
@gonzojive
Copy link

@gonzojive does this latest change work for you?

It seems to work for me.

$ git clone https://github.com/gonzojive/rules_ts_proto
$ cd rules_ts_proto/e2e/typescript_proto_usage/
$ bazel test //...

That might not work for others due to local repositories in the workspace.

@jbedard
Copy link
Member

jbedard commented Mar 9, 2023

Great, thanks for checking! 👍

@jbedard jbedard merged commit 5cbd285 into aspect-build:main Mar 9, 2023
@dgp1130 dgp1130 deleted the esm branch March 13, 2023 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[Bug]: jasmine_test doesn't work if type = "module" in package.json
3 participants