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

[Jest] Migrate src/ui/control/attribution_control.test.ts #543

Merged
merged 16 commits into from Oct 21, 2021
Merged

[Jest] Migrate src/ui/control/attribution_control.test.ts #543

merged 16 commits into from Oct 21, 2021

Conversation

astridx
Copy link
Contributor

@astridx astridx commented Oct 20, 2021

Special things here:

  1. I need to make changes in one file in the folder test/util/index.js. As this file is used also with tape I did not want to change it. I decide to copy it. cp test/util/index.js src/util/test/util.ts.
  2. I created the setup file setup.js instead of the stubloader. (https://jestjs.io/docs/configuration#setupfilesafterenv-array). Then I copied the needed stuff form stubloader,.
  3. I changed one source file. https://stackoverflow.com/a/62281432/4260616
  4. I didn't change much in the test file itself.

If saw this error

    ReferenceError: setImmediate is not defined

      61 |
      62 |     postMessage(data: any) {
    > 63 |         setImmediate(() => {
         |         ^
      64 |             try {
      65 |                 for (const listener of this.postListeners) {
      66 |                     listener({data, target: this.target});

      at MessageBus.postMessage (src/util/web_worker.ts:63:9)
      at Actor.send (src/util/actor.ts:75:21)
      at src/util/dispatcher.ts:47:19

@github-actions
Copy link
Contributor

github-actions bot commented Oct 20, 2021

Bundle size report:

Size Change: 0 B
Total Size Before: 194 kB
Total Size After: 194 kB

Output file Before After Change
maplibre-gl.js 185 kB 185 kB 0 B
maplibre-gl.css 9.49 kB 9.49 kB 0 B
ℹ️ View Details No major changes

@HarelM
Copy link
Member

HarelM commented Oct 20, 2021

Please add the removal of the "old" file to #458, also setTimeout should be with 0 parameter, shouldn't it?

@astridx
Copy link
Contributor Author

astridx commented Oct 20, 2021

Please add the removal of the "old" file to #458 ...

It is already in #458. It is Point 5.

@astridx
Copy link
Contributor Author

astridx commented Oct 20, 2021

..., also setTimeout should be with 0 parameter, shouldn't it?

The delay is an optional parameter and is set 0 by default (https://developer.mozilla.org/en-US/docs/Web/API/setTimeout). But you are right. I added the paramater, so it is more correct.

jest.config.cjs Outdated
@@ -15,4 +15,5 @@ module.exports = {
transformIgnorePatterns: [
],
setupFiles: ["jest-canvas-mock"],
setupFilesAfterEnv: ["./setup.js"],
Copy link
Member

Choose a reason for hiding this comment

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

jest_setup? mock_setup? test_setup? I think setup.is too general...

Copy link
Member

Choose a reason for hiding this comment

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

Thinking about it more, why not wrap the functionality in this file and move it to util.ts file and simply call it in beforeEach in the relevant tests? wouldn't it be more clear when looking at the test what the test is doing? It will cause a bit more code duplication, but I think it's OK...

@astridx
Copy link
Contributor Author

astridx commented Oct 21, 2021

I have now removed setup.js and moved the implementation to util.js and beforeEach() as suggested.

I am just trying to understand this more clearly. I thought the line window.performance.mark = jest.fn(); is necessary because of sinon. But I still don't understand exactly why in this test?

@HarelM
Copy link
Member

HarelM commented Oct 21, 2021

I don't think it is related to sinon, I think it is related to node and the fact that the code runs in jsdom.
I'm guessing that somewhere in the code that the tests in this file go through they call window.performance.mark, and so you have to mock it for the tests to not fail on this.
I'm see the same with fetch so I'm doing global.fetch = null to solve this, which is very similar...

@HarelM HarelM merged commit f3ef9e7 into maplibre:main Oct 21, 2021
@astridx astridx deleted the 20211020__attributioncontrol branch October 28, 2021 10:30
@wipfli wipfli mentioned this pull request Oct 31, 2021
@astridx astridx restored the 20211020__attributioncontrol branch November 4, 2021 17:38
@astridx astridx deleted the 20211020__attributioncontrol branch November 4, 2021 19:18
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

3 participants