Skip to content

Commit d05ad75

Browse files
committed
Fix the rest of tests, correctly stub process.exit
1 parent 51f736f commit d05ad75

1 file changed

Lines changed: 44 additions & 29 deletions

File tree

tests/unit/generateTranslationsTest.ts

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import dedent from '@libs/StringUtils/dedent';
99
import generateTranslations, {GENERATED_FILE_PREFIX} from '@scripts/generateTranslations';
1010
import Translator from '@scripts/utils/Translator/Translator';
1111

12+
let mockExit: jest.SpyInstance;
13+
1214
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment
1315
let mockEn: any = jest.requireActual('@src/languages/en');
1416
jest.mock('@src/languages/en', () => ({
@@ -31,6 +33,8 @@ describe('generateTranslations', () => {
3133
const ORIGINAL_ARGV = process.argv;
3234

3335
beforeEach(() => {
36+
mockExit = jest.spyOn(process, 'exit').mockImplementation(() => undefined as never);
37+
3438
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'translations-test-'));
3539
LANGUAGES_DIR = path.join(tempDir, 'src/languages');
3640
EN_PATH = path.join(LANGUAGES_DIR, 'en.ts');
@@ -52,6 +56,7 @@ describe('generateTranslations', () => {
5256

5357
afterAll(() => {
5458
process.argv = ORIGINAL_ARGV;
59+
jest.restoreAllMocks();
5560
});
5661

5762
it('translates nested structures', async () => {
@@ -621,22 +626,26 @@ describe('generateTranslations', () => {
621626
});
622627

623628
it('translates nested paths when parent path is specified', async () => {
629+
const strings = {
630+
greeting: 'Hello',
631+
common: {
632+
save: 'Save',
633+
cancel: 'Cancel',
634+
nested: {
635+
deep: 'Deep value',
636+
},
637+
},
638+
errors: {
639+
generic: 'An error occurred',
640+
},
641+
};
642+
643+
mockEn = strings;
644+
624645
fs.writeFileSync(
625646
EN_PATH,
626647
dedent(`
627-
const strings = {
628-
greeting: 'Hello',
629-
common: {
630-
save: 'Save',
631-
cancel: 'Cancel',
632-
nested: {
633-
deep: 'Deep value',
634-
},
635-
},
636-
errors: {
637-
generic: 'An error occurred',
638-
},
639-
};
648+
const strings = ${JSON.stringify(strings)};
640649
export default strings;
641650
`),
642651
'utf8',
@@ -666,15 +675,18 @@ describe('generateTranslations', () => {
666675
});
667676

668677
it('ignores --compare-ref when --paths is provided', async () => {
678+
const strings = {
679+
greeting: 'Hello',
680+
common: {
681+
save: 'Save',
682+
},
683+
};
684+
mockEn = strings;
685+
669686
fs.writeFileSync(
670687
EN_PATH,
671688
dedent(`
672-
const strings = {
673-
greeting: 'Hello',
674-
common: {
675-
save: 'Save',
676-
},
677-
};
689+
const strings = ${JSON.stringify(strings)};
678690
export default strings;
679691
`),
680692
'utf8',
@@ -719,22 +731,25 @@ describe('generateTranslations', () => {
719731

720732
// Expect the script to throw an error during CLI parsing
721733
await expect(generateTranslations()).rejects.toThrow('Translation path not found: nonexistent.path');
734+
expect(mockExit).toHaveBeenCalledWith(1);
722735
});
723736

724737
it('validates paths against actual translation structure', async () => {
738+
const strings = {
739+
greeting: 'Hello',
740+
common: {
741+
save: 'Save',
742+
},
743+
errors: {
744+
generic: 'An error occurred',
745+
},
746+
};
747+
mockEn = strings;
748+
725749
fs.writeFileSync(
726750
EN_PATH,
727751
dedent(`
728-
const strings = {
729-
greeting: 'Hello',
730-
common: {
731-
save: 'Save',
732-
cancel: 'Cancel',
733-
},
734-
errors: {
735-
generic: 'An error occurred',
736-
},
737-
};
752+
const strings = ${JSON.stringify(strings)};
738753
export default strings;
739754
`),
740755
'utf8',

0 commit comments

Comments
 (0)