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

Add tests and automated publishing the package to NPM #96

Merged
merged 11 commits into from
Oct 18, 2023

Conversation

robmosca
Copy link
Contributor

This PR:

  • introduces automated publishing of the package using the changeset action. The setup is identical to the one in single-spa-react
  • introduces Jest tests on the API

For automated publishing to work, it is necessary that:

  • The GITHUB_TOKEN is granted permissions to create and approve pull requests. This can be done in Settings -> Actions -> General -> Workflow permissions -> Allow GitHub Actions to create and approve pull requests
  • The admin of the repo creates an NPM_TOKEN secret with an NPM token with write permissions to the organization (or at least the corresponding package, for publishing).

@changeset-bot
Copy link

changeset-bot bot commented Sep 29, 2023

🦋 Changeset detected

Latest commit: fd32330

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
import-map-overrides Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@robmosca robmosca changed the title Add publish automation and jest tests Add tests and automated publishing the package to NPM Sep 29, 2023
Copy link
Member

@joeldenning joeldenning left a comment

Choose a reason for hiding this comment

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

Thanks for your work on this, these tests will help a lot. I've left a few comments and questions

Do I need to add an NPM_TOKEN environment variable to this repo before merging it?

"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": [
"@changesets/changelog-github",
{ "repo": "single-spa/single-spa-react" }
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
{ "repo": "single-spa/single-spa-react" }
{ "repo": "single-spa/import-map-overrides }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about this. Fixed.

"import-map-overrides": patch
---

Add automation to publish package
Copy link
Member

Choose a reason for hiding this comment

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

This seems redundant to automate release flow in the fast-chairs-guess.md changeset. Let's remove this changeset?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course. This PR has been reworked so many times that with iterations (it has been reopened quite a few times) some comments went lost and some inconsistencies crept in. Removing this file.

.github/workflows/build-test-release.yml Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
rollup.config.js Outdated
@@ -70,6 +70,7 @@ export default [
},
plugins: [
babel({
babelHelpers: "bundled",
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed? Are we using generator syntax or something else that requires babel runtime?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without this, when building we get the following warning:

src/import-map-overrides-server.js → dist/import-map-overrides-server.js...
babelHelpers: 'bundled' option was used by default. It is recommended to configure this option explicitly, read more here: https://github.com/rollup/plugins/tree/master/packages/babel#babelhelpers

return promise.then(() =>
includes(imo.invalidExternalMaps, importMapUrl)
return promise.then(
() => !includes(imo.invalidExternalMaps, importMapUrl)
Copy link
Member

Choose a reason for hiding this comment

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

Is this a bug that you fixed? Please link issue if there was one for this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is indeed a bug I fixed. I discovered it when writing the tests.
The function is called isExternalMapValid and should return true when the map IS valid, which means it is NOT included in the imo.invalidExternalMaps array.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There was no issue about this. The power of unit tests 🙂

@@ -367,6 +373,7 @@ function init() {

function fireEvent(type) {
// Set timeout so that event fires after the change has totally finished

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

@@ -0,0 +1,25 @@
// Mocks for the localStorage
Copy link
Member

Choose a reason for hiding this comment

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

Seems redundant to have local-storage-mock.js and localStorageMock.js

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another left over from the many reopenings of this PR. Sorry about this. Removed.

@@ -1,4 +1,4 @@
import cookie from "cookie";
const cookie = require("cookie");
Copy link
Member

Choose a reason for hiding this comment

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

Why the change to CJS?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we use the ES syntax with the new version of cookie library we get the following error when building with rollup:

src/import-map-overrides-server.js → dist/import-map-overrides-server.js...
(!) Unresolved dependencies
https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
cookie (imported by src/server/server-api.js)

Copy link
Member

Choose a reason for hiding this comment

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

I am hesitant about this change because it could impact a lot of things (bundling, tests, linter, etc). All ESM is better.

I read through the rollup documentation linked to in the warning you shared (https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency) and the solution seems to be to add @rollup/plugin-node-resolve rather than to change it to a CJS import. Is there an error even when rollup node resolve is present?

Have you tested that import-map-overrides works server-side after this change?

src/util/includes.test.js Show resolved Hide resolved
Copy link
Contributor Author

@robmosca robmosca left a comment

Choose a reason for hiding this comment

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

Addressed the comments.

"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": [
"@changesets/changelog-github",
{ "repo": "single-spa/single-spa-react" }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about this. Fixed.

"import-map-overrides": patch
---

Add automation to publish package
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course. This PR has been reworked so many times that with iterations (it has been reopened quite a few times) some comments went lost and some inconsistencies crept in. Removing this file.

.gitignore Outdated Show resolved Hide resolved
.github/workflows/build-test-release.yml Outdated Show resolved Hide resolved
rollup.config.js Outdated
@@ -70,6 +70,7 @@ export default [
},
plugins: [
babel({
babelHelpers: "bundled",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without this, when building we get the following warning:

src/import-map-overrides-server.js → dist/import-map-overrides-server.js...
babelHelpers: 'bundled' option was used by default. It is recommended to configure this option explicitly, read more here: https://github.com/rollup/plugins/tree/master/packages/babel#babelhelpers

let parsedContent;
try {
parsedContent = JSON.parse(scriptEl.textContent);
} catch (e) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I introduced this when I wrote a test and I realized that, if we have an empty import map of the form:

<script type="importmap"></script>

this will make the parsing fail. I considered this would be an unwanted behaviour, hence the change. I understand your comment and I have now reverted this change and opened an issue to further discuss this and maybe reintroduce this in a separate PR.
#98

let parsedContent;
try {
parsedContent = JSON.parse(scriptEl.textContent);
} catch (e) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also modified the tests accordingly.

return promise.then(() =>
includes(imo.invalidExternalMaps, importMapUrl)
return promise.then(
() => !includes(imo.invalidExternalMaps, importMapUrl)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is indeed a bug I fixed. I discovered it when writing the tests.
The function is called isExternalMapValid and should return true when the map IS valid, which means it is NOT included in the imo.invalidExternalMaps array.

return promise.then(() =>
includes(imo.invalidExternalMaps, importMapUrl)
return promise.then(
() => !includes(imo.invalidExternalMaps, importMapUrl)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There was no issue about this. The power of unit tests 🙂

@@ -0,0 +1,25 @@
// Mocks for the localStorage
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another left over from the many reopenings of this PR. Sorry about this. Removed.

@robmosca
Copy link
Contributor Author

robmosca commented Oct 12, 2023

Thanks for your work on this, these tests will help a lot. I've left a few comments and questions

Do I need to add an NPM_TOKEN environment variable to this repo before merging it?

For automated publishing to work, @joeldenning, before merging it is necessary that:

  • The GITHUB_TOKEN is granted permissions to create and approve pull requests. This can be done in Settings -> Actions -> General -> Workflow permissions -> Allow GitHub Actions to create and approve pull requests
  • The admin of the repo creates an NPM_TOKEN secret with an NPM token with write permissions to the organization (or at least the corresponding package, for publishing).

Copy link
Member

@joeldenning joeldenning left a comment

Choose a reason for hiding this comment

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

Looks good, one last comment

@@ -1,4 +1,4 @@
import cookie from "cookie";
const cookie = require("cookie");
Copy link
Member

Choose a reason for hiding this comment

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

I am hesitant about this change because it could impact a lot of things (bundling, tests, linter, etc). All ESM is better.

I read through the rollup documentation linked to in the warning you shared (https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency) and the solution seems to be to add @rollup/plugin-node-resolve rather than to change it to a CJS import. Is there an error even when rollup node resolve is present?

Have you tested that import-map-overrides works server-side after this change?

@robmosca
Copy link
Contributor Author

I read through the rollup documentation linked to in the warning you shared (https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency) and the solution seems to be to add @rollup/plugin-node-resolve rather than to change it to a CJS import. Is there an error even when rollup node resolve is present?

Have you tested that import-map-overrides works server-side after this change?

To unblock this PR I have reverted all changes related to the warning I get when building import-map-overrides-server.js.
I tracked the problem in this new issue and I can tackle it in a separate PR.

@joeldenning joeldenning merged commit cd137ce into main Oct 18, 2023
2 checks passed
@joeldenning joeldenning deleted the add-publish-automation-and-jest-tests branch October 18, 2023 23:30
@github-actions github-actions bot mentioned this pull request Oct 18, 2023
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.

2 participants