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

frontend: use vite's mechanism for env variables to fill window.envir… #3268

Merged
merged 11 commits into from
Sep 17, 2023

Conversation

BacLuc
Copy link
Contributor

@BacLuc BacLuc commented Feb 18, 2023

…onment

That we don't have locally cached environment.js files breaking the development setup. Sadly we still use vue-cli to run our tests, so there the vite mechanism does not work and we have to maintain one additional environment.js.

In production, we compose the environment.js anyway in the configmap, thus we don't have to consider the production case in dev-environment.js.

Now we can also remove the check in index.html.
In a production environment, you have to check yourself that you have all the needed configurations set.

closes #3267

EDIT:
Current state: could not get the tests to run... -> converted it to draft again

EDIT:
Now turned the PR around:

Step 1:
replace jest with vitest
See the single commits for some quirky fixes that it works.
Also reduces the coverage by 6% in the frontend, and the overall coverage by 30%

Step 2:
Use the vite env variable mechanism to fill environmnet

I will use review comments in the files that we can discuss the different fixes that were needed.

@BacLuc BacLuc added the deploy! Creates a feature branch deployment for this PR label Feb 18, 2023
@BacLuc BacLuc force-pushed the dev-setup-improve-frontend-env-experiance branch from fac673c to 7a7d916 Compare February 18, 2023 15:10
@github-actions
Copy link

github-actions bot commented Feb 18, 2023

Feature branch deployment currently inactive.

If the PR is still open, you can add the deploy! label to this PR to trigger a feature branch deployment.

Copy link
Member

@usu usu left a comment

Choose a reason for hiding this comment

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

We also have a environment.js file in the .jest folder: https://github.com/ecamp/ecamp3/blob/devel/frontend/.jest/environment.js

This is still needed and properly used, right?

frontend/src/dev-environment.js Outdated Show resolved Hide resolved
frontend/src/main.js Outdated Show resolved Hide resolved
@BacLuc
Copy link
Contributor Author

BacLuc commented Feb 19, 2023

We also have a environment.js file in the .jest folder: https://github.com/ecamp/ecamp3/blob/devel/frontend/.jest/environment.js

This is still needed and properly used, right?

 Sadly we still use vue-cli to run our tests, so there the vite mechanism does not work and we have to maintain one additional environment.js.

@carlobeltrame
Copy link
Member

Migrating from jest / vue-cli to vitest shouldn't be too hard. Maybe I'll try that soon

@BacLuc BacLuc removed the deploy! Creates a feature branch deployment for this PR label Feb 20, 2023
@BacLuc BacLuc force-pushed the dev-setup-improve-frontend-env-experiance branch 3 times, most recently from 7397544 to b9b475d Compare February 20, 2023 17:40
@BacLuc BacLuc added the deploy! Creates a feature branch deployment for this PR label Feb 20, 2023
frontend/src/App.vue Outdated Show resolved Hide resolved
@BacLuc BacLuc marked this pull request as draft February 20, 2023 20:00
@BacLuc
Copy link
Contributor Author

BacLuc commented Feb 20, 2023

Migrating from jest / vue-cli to vitest shouldn't be too hard. Maybe I'll try that soon

I tried it. They kept the api from jest, but there is not e a drop in replacement for everything. https://github.com/BacLuc/ecamp3/tree/try-vitest

@BacLuc BacLuc removed the deploy! Creates a feature branch deployment for this PR label Feb 20, 2023
@carlobeltrame carlobeltrame self-assigned this Feb 27, 2023
@carlobeltrame
Copy link
Member

I've gotten a lot further in the vitest migration, and am still actively working on this. Just FYI, so we don't do too much parallel work.
As far as I can tell so far, vite does have drop-in replacements for most if not all jest mechanisms which we use. Admittedly, some of them aren't documented very clearly in the docs. In other places, you threw out our much needed jest extensions / customizations without replacing them (e.g. the snapshot serializer works out of the box in vitest, and for the canvas mock there is an adapter package). And in yet some other places, we had to wait for some vitest bugfixes.

@BacLuc
Copy link
Contributor Author

BacLuc commented Aug 6, 2023

Should i

  1. continue this PR [👍]
  2. Help @carlobeltrame with migrating to vitest (i just need a link to your branch) [🚀]
  3. close this PR [👎]

@carlobeltrame
Copy link
Member

My work on top of yours is now here: https://github.com/carlobeltrame/ecamp3/tree/try-vitest
Right now I don't remember how far I got or what was the immediate next problem. But a good start would probably be to rebase the branch and then update all touched npm packages to their latest versions. As mentioned, some of the involved packages also had some bugfixing to do.

@BacLuc BacLuc force-pushed the dev-setup-improve-frontend-env-experiance branch 5 times, most recently from af943ea to db0e26d Compare August 7, 2023 09:57
@@ -39,134 +41,133 @@ ruleTester.run('local-rules/matching-translation-keys', ruleInstance, {
name: 'allows correct key in js',
code: '$tc("components.hello.world")',
options: options,
filename: '/app/src/components/hello.js',
filename: '/src/components/hello.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.

I did not get the packageDirectory mock to work, so i used a simple solution.
Else it works in the docker container, but not when run locally with an interpreter.
ok @carlobeltrame ?

Copy link
Member

Choose a reason for hiding this comment

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

As long as the tests work in CI and when run in the way we have it documented in the wiki, fine by me

Comment on lines 220 to 226
"collectCoverageFrom": [
"src/**/*.{js,vue}",
"common/**/*.js",
"!src/components/print/print-react/generatePdf.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.

Maybe we need to use this config also for the v8 coverage.
Should i do this in this PR, or can we do this in another PR?

Copy link
Member

Choose a reason for hiding this comment

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

I would merge this and then see whether we have any coverage problems, and solve those later.

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 did not get these tests to run.

commit message:
frontend: delete RichText tests

Because i could not get them to run with vitest.
The error i could not resolve:
FAIL src/components/form/api/tests/ApiRichtext.spec.js > An ApiRichtext > updates state if value in store is refreshed and has new value
TypeError: tippy is not a function
❯ BubbleMenuView.createTooltip node_modules/@tiptap/extension-bubble-menu/src/bubble-menu-plugin.ts:141:18
❯ BubbleMenuView.updateHandler node_modules/@tiptap/extension-bubble-menu/src/bubble-menu-plugin.ts:200:10
❯ BubbleMenuView.update node_modules/@tiptap/extension-bubble-menu/src/bubble-menu-plugin.ts:170:10
❯ EditorView.updatePluginViews node_modules/prosemirror-view/dist/index.js:5262:32
❯ EditorView.updateStateInner node_modules/prosemirror-view/dist/index.js:5211:14
❯ EditorView.updateState node_modules/prosemirror-view/dist/index.js:5142:14
❯ Editor.dispatchTransaction node_modules/@tiptap/core/src/Editor.ts:344:15
❯ EditorView.dispatch node_modules/prosemirror-view/dist/index.js:5471:33
❯ Object.method [as setContent] node_modules/@tiptap/core/src/CommandManager.ts:42:18
❯ VueComponent.value src/components/form/tiptap/TiptapEditor.vue:174:1
172| // of the input field
173| if (val !== this.html) {
174| this.editor.commands.setContent(val)
| ^
175| }
176| },

Copy link
Member

Choose a reason for hiding this comment

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

Alright, let's write new ones later.

@@ -1,63 +1,45 @@
// Libraries
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here i had problems with the virtualTIme.
It did not work as expected, so i used additional promises and resolvers to control the execution of promises.

@@ -41,4 +42,6 @@ class StorePlugin {
export let apiStore
export let store

export let storePlugin = new StorePlugin()
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 for the auth.spec.js test.
As soon as i started mocking the router, store.replaceState was undefined...?

Copy link
Member

Choose a reason for hiding this comment

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

I think I was able to simplify this: b33f67e

}
}

function createStubs() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

here i had to change the stups to CamelCase

@BacLuc BacLuc marked this pull request as ready for review August 7, 2023 10:24
@@ -39,134 +41,133 @@ ruleTester.run('local-rules/matching-translation-keys', ruleInstance, {
name: 'allows correct key in js',
code: '$tc("components.hello.world")',
options: options,
filename: '/app/src/components/hello.js',
filename: '/src/components/hello.js',
Copy link
Member

Choose a reason for hiding this comment

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

As long as the tests work in CI and when run in the way we have it documented in the wiki, fine by me

Comment on lines 220 to 226
"collectCoverageFrom": [
"src/**/*.{js,vue}",
"common/**/*.js",
"!src/components/print/print-react/generatePdf.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 would merge this and then see whether we have any coverage problems, and solve those later.

Copy link
Member

Choose a reason for hiding this comment

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

Alright, let's write new ones later.

Comment on lines +25 to +26
set: lodash.set,
get: lodash.get,
Copy link
Member

Choose a reason for hiding this comment

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

That shouldn't be necessary after the ...lodash above..?

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 expected that, but it did not work without setting these to explicitly.
Did you test it and it worked?

const lodash = await importOriginal()
return {
...lodash,
debounce: (callback) => new Promise((resolve) => (debounce = resolve)).then(callback),
Copy link
Member

Choose a reason for hiding this comment

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

Looks very funky... This implementation only supports one debounce at a time, because there is only one global let debounce, right? But I guess it is fine for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, but why would you have 2 debounce before a user interaction/web request/response...?

Copy link
Member

Choose a reason for hiding this comment

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

It is possible to write a test where e.g. the user triggers two separate debounced buttons, and check whether everything is executed in the expected order. I think this kind of setup is not supported by this mock.

@@ -41,4 +42,6 @@ class StorePlugin {
export let apiStore
export let store

export let storePlugin = new StorePlugin()
Copy link
Member

Choose a reason for hiding this comment

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

I think I was able to simplify this: b33f67e

Copy link
Member

@manuelmeister manuelmeister left a comment

Choose a reason for hiding this comment

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

Worked on my machine ;)

Not all tests are running
Because vitest cannot resolve the files if there is no extension.
And update snapshots.
Vitest said correctly that this method does not exist.
Let the test helpers create the store, just provide a json.
This fixes Error: [vuex] getters should be function but "getters.getLoggedInUser" is {"_meta":{"self":"/users/17d341a80579"}}.

Write the stubs in camel case

And simplify a little.
Because i could not get them to run with vitest.
The error i could not resolve:
 FAIL  src/components/form/api/__tests__/ApiRichtext.spec.js > An ApiRichtext > updates state if value in store is refreshed and has new value
TypeError: tippy is not a function
 ❯ BubbleMenuView.createTooltip node_modules/@tiptap/extension-bubble-menu/src/bubble-menu-plugin.ts:141:18
 ❯ BubbleMenuView.updateHandler node_modules/@tiptap/extension-bubble-menu/src/bubble-menu-plugin.ts:200:10
 ❯ BubbleMenuView.update node_modules/@tiptap/extension-bubble-menu/src/bubble-menu-plugin.ts:170:10
 ❯ EditorView.updatePluginViews node_modules/prosemirror-view/dist/index.js:5262:32
 ❯ EditorView.updateStateInner node_modules/prosemirror-view/dist/index.js:5211:14
 ❯ EditorView.updateState node_modules/prosemirror-view/dist/index.js:5142:14
 ❯ Editor.dispatchTransaction node_modules/@tiptap/core/src/Editor.ts:344:15
 ❯ EditorView.dispatch node_modules/prosemirror-view/dist/index.js:5471:33
 ❯ Object.method [as setContent] node_modules/@tiptap/core/src/CommandManager.ts:42:18
 ❯ VueComponent.value src/components/form/tiptap/TiptapEditor.vue:174:1
    172|       // of the input field
    173|       if (val !== this.html) {
    174|         this.editor.commands.setContent(val)
       | ^
    175|       }
    176|     },
router.push takes ca 2 seconds, which is too long for a unit tests.
Thus we mock router.push and router.resolve.
Then store.replace does not work anymore, thus we use vi.importActual
to make sure not the somehow mocked store is used.
…t for vitest

Mocking seems a little difficult with vitest.
If we keep the /app, the tests work in the docker container, but not
when running the tests with a local interpreter.
…onment

That we don't have locally cached environment.js files breaking the development setup.
Sadly we still use vue-cli to run our tests, so there the vite mechanism does
not work and we have to maintain one additional environment.js.

In production, we compose the environment.js anyway in the configmap, thus we don't
have to consider the production case in dev-environment.js.

Now we can also remove the check in index.html.
In a production environment, you have to check yourself that you have all the needed configurations
set.

closes ecamp#3267
@BacLuc BacLuc force-pushed the dev-setup-improve-frontend-env-experiance branch from c9193ee to bb570c7 Compare September 17, 2023 09:38
@BacLuc BacLuc added the deploy! Creates a feature branch deployment for this PR label Sep 17, 2023
@BacLuc BacLuc temporarily deployed to feature-branch September 17, 2023 09:45 — with GitHub Actions Inactive
@BacLuc BacLuc added this pull request to the merge queue Sep 17, 2023
Merged via the queue into ecamp:devel with commit 225105f Sep 17, 2023
25 checks passed
@BacLuc BacLuc deleted the dev-setup-improve-frontend-env-experiance branch September 17, 2023 10:27
This was referenced Sep 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deploy! Creates a feature branch deployment for this PR
Projects
Development

Successfully merging this pull request may close these issues.

Avoid changes in environment.docker.dist breaking developer's environments
4 participants