fix(git): limit need for Intl to specific method#2172
Conversation
Codecov Report
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
|
|
@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;
});
}); |
|
@matthewmayer So this is not working? https://vitest.dev/guide/mocking.html#globals |
not really sure how that would work. seems to be for mocking globals you DON'T have, not removing globals you do have? |
|
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. |
|
@matthewmayer It took me a fraction of a second to just do this in
(ignore the TS error, we are talking about raw JavaScript here) Then I logged just
in Now you just need to move the check-logic to runtime, so you don't check I hope this helps to bring you forward |
|
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 😂 |
There was a problem hiding this comment.
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



#2171