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

Fix ESM issue for Node.js #1221

Closed
wants to merge 7 commits into from
Closed

Conversation

denysoblohin-okta
Copy link
Contributor

@denysoblohin-okta denysoblohin-okta commented May 25, 2022

This PR resolves issue #1188
Issue: okta-auth-js can't be loaded as ES module for Node.js because of incorrect ES bundle of broadcast-channel

Internal ref: OKTA-491223

@denysoblohin-okta denysoblohin-okta changed the base branch from master to 6.5 May 25, 2022 15:22
@denysoblohin-okta denysoblohin-okta force-pushed the od-esm-fix-OKTA-491223 branch 2 times, most recently from 167a705 to 2017052 Compare May 25, 2022 15:31
package.json Outdated
@@ -49,6 +49,7 @@
"test:integration": "jest --config ./jest.integration.js",
"test:bundle:esm:browser": "cross-env BUNDLE_ENV=browser NODE_OPTIONS=--experimental-vm-modules jest --config ./jest.esm.mjs",
"test:bundle:esm:node": "cross-env BUNDLE_ENV=node NODE_OPTIONS=--experimental-vm-modules jest --config ./jest.esm.mjs",
"test:bundle:esm:node:app": "yarn workspace @okta/test.app.node-esm start",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to move this test to unit layer? catch error from a running node app seems like an integration test.

what if you don't map the module in jest config above?

Copy link
Contributor Author

@denysoblohin-okta denysoblohin-okta May 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shuowu

what if you don't map the module in jest config above?

Without module mapping in Jest yarn test:bundle:esm:bowser will fail with
SyntaxError: Unexpected token 'export'

It happens because ES module of broadcast-channel for browser (dist/esbrowser/index.js) have '.js' extension (not '.mjs').

For the same reason Jest uses special ES module of okta-auth-js for browser with .mjs extension - build/bundles-for-validation/esm/esm.browser.mjs instead of build/esm/esm.browser.js.
See

// generate ems bundle for jest test, ".mjs" extension should be used

`<rootDir>/build/bundles-for-validation/esm/esm.browser.mjs` :

Copy link
Contributor Author

@denysoblohin-okta denysoblohin-okta May 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to move this test to unit layer? catch error from a running node app seems like an integration test.

There is already unit test for validating bundles - test:bundle:esm:node

It has a little "hack" currently in master

'^broadcast-channel$': '<rootDir>/node_modules/broadcast-channel/dist/es5node/index.js'

to resolve broadcast-chanel with ES5 version of bundle which doesn't have import/export
I've replaced it with

'^broadcast-channel$': '<rootDir>/node_modules/broadcast-channel/dist/esnode/index.mjs' 

This line can be removed, but then there will be problem with test:bundle:esm:browser , see comment above

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const OktaAuth = process.env.BUNDLE_ENV === 'browser' ? 
  `<rootDir>/build/bundles-for-validation/esm/esm.browser.mjs` : 
  `<rootDir>/build/esm/esm.node.mjs`;

Is it possible to follow logic here to only apply module map for browser env, and leave it to jest to figure out the node dependencies. If possible, we should avoid over testing dependencies of auth-js.

@denysoblohin-okta denysoblohin-okta force-pushed the od-esm-fix-OKTA-491223 branch 2 times, most recently from ea33627 to b464dd5 Compare May 25, 2022 22:29
Copy link
Contributor

@shuowu shuowu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably can also re-organize the bundle related tests to put all of them in one single bacon task, so we can include anything that make sense to simply verify different version of bundles.

The idea above should not be included in a patch version release (no change will be needed).

Verified

This commit was signed with the committer’s verified signature.
Julesssss Jules
added test:bundle:esm:node:app

Verified

This commit was signed with the committer’s verified signature.
Julesssss Jules

Verified

This commit was signed with the committer’s verified signature.
Julesssss Jules
This reverts commit 4e3838f.

Verified

This commit was signed with the committer’s verified signature.
Julesssss Jules

Verified

This commit was signed with the committer’s verified signature.
Julesssss Jules
@denysoblohin-okta denysoblohin-okta force-pushed the od-esm-fix-OKTA-491223 branch from b464dd5 to 9bc0cf3 Compare June 1, 2022 19:02
@denysoblohin-okta denysoblohin-okta changed the base branch from 6.5 to 6.6 June 1, 2022 19:03
@codecov-commenter
Copy link

codecov-commenter commented Jun 1, 2022

Codecov Report

Merging #1221 (d2d5d62) into 6.6 (e79b294) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##              6.6    #1221   +/-   ##
=======================================
  Coverage   93.41%   93.41%           
=======================================
  Files         157      157           
  Lines        4586     4586           
  Branches     1058     1058           
=======================================
  Hits         4284     4284           
  Misses        283      283           
  Partials       19       19           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e79b294...d2d5d62. Read the comment docs.

Verified

This commit was signed with the committer’s verified signature.
Julesssss Jules

Verified

This commit was signed with the committer’s verified signature.
Julesssss Jules
eng-prod-CI-bot-okta pushed a commit that referenced this pull request Jun 1, 2022

Verified

This commit was signed with the committer’s verified signature.
Julesssss Jules
OKTA-491223
<<<Jenkins Check-In of Tested SHA: d2d5d62 for [email protected]>>>
Artifact: okta-auth-js
Files changed count: 5
PR Link: #1221
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants