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(git): limit need for Intl to specific method #2172

Merged
merged 8 commits into from
May 27, 2023

Conversation

matthewmayer
Copy link
Contributor

@codecov
Copy link

codecov bot commented May 23, 2023

Codecov Report

Merging #2172 (e5a96ff) into next (012ba2c) will increase coverage by 0.00%.
The diff coverage is 93.93%.

Additional details and impacted files
@@           Coverage Diff           @@
##             next    #2172   +/-   ##
=======================================
  Coverage   99.60%   99.60%           
=======================================
  Files        2607     2607           
  Lines      245013   245029   +16     
  Branches     1151     1155    +4     
=======================================
+ Hits       244041   244061   +20     
+ Misses        945      941    -4     
  Partials       27       27           
Impacted Files Coverage Δ
src/modules/git/index.ts 99.25% <93.93%> (-0.75%) ⬇️

... and 1 file with indirect coverage changes

@Shinigami92 Shinigami92 added c: bug Something isn't working m: git Something is referring to the git module labels May 23, 2023
@Shinigami92 Shinigami92 added the p: 1-normal Nothing urgent label May 23, 2023
@Shinigami92 Shinigami92 linked an issue May 23, 2023 that may be closed by this pull request
10 tasks
@Shinigami92 Shinigami92 added the needs test More tests are needed label May 23, 2023
@Shinigami92
Copy link
Member

@matthewmayer you already found out how to fake that Intl does not exists, or alternatively you can also just mock it with vitest

Please add tests for this

@matthewmayer
Copy link
Contributor Author

matthewmayer commented May 23, 2023

@matthewmayer you already found out how to fake that Intl does not exists, or alternatively you can also just mock it with vitest

Please add tests for this

Hmm, the problem is that Intl is checked only once at import time when the library is loaded. So you can't just do something like this:

describe('commitDate', () => {
        it('should return a random commitDate', () => {
          const commitDate = faker.git.commitDate();

          expect(commitDate).toBeTruthy();
          expect(commitDate).toBeTypeOf('string');

          const parts = commitDate.split(' ');
          expect(parts.length).toBe(6);
        });
        it('should throw if Intl is unavailable', () => {
          const oldIntl = Intl;
          (global as any).Intl = undefined;

          expect(() => {
            faker.git.commitDate();
          }).toThrow(
            new FakerError(
              'This method requires an environment which supports Intl.NumberFormat and Intl.DateTimeFormat'
            )
          );

          (global as any).Intl = oldIntl;
        });
      });

@Shinigami92
Copy link
Member

Shinigami92 commented May 23, 2023

@matthewmayer So this is not working? https://vitest.dev/guide/mocking.html#globals
If so, please report to Vitest

@matthewmayer
Copy link
Contributor Author

@matthewmayer So this is not working? https://vitest.dev/guide/mocking.html#globals If so, please report to Vitest

not really sure how that would work. seems to be for mocking globals you DON'T have, not removing globals you do have?

@Shinigami92
Copy link
Member

get creative, try a bit around 🙂

@matthewmayer
Copy link
Contributor Author

Can't get it to work. I can't seem to get code to run in the test before faker is imported.

This is a way beyond my capabilities so if anyone else is available to add tests I'd appreciate it.

@Shinigami92
Copy link
Member

Shinigami92 commented May 23, 2023

@matthewmayer It took me a fraction of a second to just do this in git.spec.ts

image

(ignore the TS error, we are talking about raw JavaScript here)

Then I logged just

image

in commitDate function

Now you just need to move the check-logic to runtime, so you don't check GIT_DATE_FORMAT_BASE == null but Intl.DateTimeFormat == null instead of the console.log I did

I hope this helps to bring you forward

@matthewmayer
Copy link
Contributor Author

Ah ok moving the check logic to run time indeed makes it easy.

Glad I only wasted a fraction of a second of your time 😂

Shinigami92
Shinigami92 previously approved these changes May 23, 2023
Copy link
Member

@Shinigami92 Shinigami92 left a comment

Choose a reason for hiding this comment

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

IMO it is okay for now to do it the dirty way and use globalThis.Intl.DateTimeFormat = backup
It could result in broken further test if the test does not run successfully, but common would this occur 🤷


Edit: dammit, somehow the ts script check failed
I will try to fix this later... sry for that

Shinigami92
Shinigami92 previously approved these changes May 23, 2023
import-brain
import-brain previously approved these changes May 24, 2023
@import-brain import-brain removed the needs test More tests are needed label May 24, 2023
import-brain
import-brain previously approved these changes May 25, 2023
Shinigami92
Shinigami92 previously approved these changes May 26, 2023
Copy link
Member

@ST-DDT ST-DDT left a comment

Choose a reason for hiding this comment

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

Missing @throws jsdocs.

@matthewmayer matthewmayer dismissed stale reviews from Shinigami92 and import-brain via 704fb07 May 27, 2023 00:51
@matthewmayer
Copy link
Contributor Author

Missing @throws jsdocs.

added @throws

@matthewmayer matthewmayer requested a review from ST-DDT May 27, 2023 04:43
@ST-DDT ST-DDT merged commit a5e73f8 into faker-js:next May 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: bug Something isn't working m: git Something is referring to the git module p: 1-normal Nothing urgent
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Using Faker in react-native: Can't find variable: Intl error on android
4 participants