|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { vol } from 'memfs' |
| 3 | + |
| 4 | +import { precommit, translationTaskNeededMessage } from './precommitFunction.mjs' |
| 5 | + |
| 6 | +const mockExit = vi.spyOn(process, 'exit').mockImplementation(code => { |
| 7 | + throw new Error(`Process exited with code: ${code}`) |
| 8 | +}) |
| 9 | + |
| 10 | +const consoleMock = vi.spyOn(console, 'error').mockImplementation(() => undefined) |
| 11 | + |
| 12 | +const mocks = vi.hoisted(() => { |
| 13 | + return { |
| 14 | + exec: vi.fn(), |
| 15 | + } |
| 16 | +}) |
| 17 | + |
| 18 | +vi.mock('child_process', () => { |
| 19 | + return { |
| 20 | + exec: mocks.exec, |
| 21 | + } |
| 22 | +}) |
| 23 | + |
| 24 | +beforeEach(() => { |
| 25 | + consoleMock.mockClear() |
| 26 | + mockExit.mockClear() |
| 27 | +}) |
| 28 | + |
| 29 | +describe('precommit function execution tests', () => { |
| 30 | + it('should exit with error code 0 when no staged files inside extract_from_pattern path', async () => { |
| 31 | + // simulate 0 staged files |
| 32 | + mocks.exec.mockImplementation((cmd, cb) => { |
| 33 | + if (cmd.includes('wc -l')) { |
| 34 | + return cb(null, { stdout: '0' }) |
| 35 | + } |
| 36 | + return cb(null, { stdout: '' }) |
| 37 | + }) |
| 38 | + |
| 39 | + try { |
| 40 | + await precommit() |
| 41 | + } catch (e) { |
| 42 | + expect(e.message).toBe('Process exited with code: 0') |
| 43 | + } |
| 44 | + mocks.exec.mockRestore() |
| 45 | + }) |
| 46 | + |
| 47 | + it('should exit with error code 1 when there are files to translate with new translations', async () => { |
| 48 | + // simulate staged files |
| 49 | + mocks.exec.mockImplementation((cmd, cb) => { |
| 50 | + if (cmd.includes('wc -l')) { |
| 51 | + return cb(null, { stdout: '1' }) |
| 52 | + } |
| 53 | + return cb(null, { stdout: '' }) |
| 54 | + }) |
| 55 | + // simulate new translations |
| 56 | + vol.fromJSON({ |
| 57 | + '__mocks__/locales/temp.json': JSON.stringify({ c: 'baz' }), |
| 58 | + '__mocks__/locales/en-US.json': JSON.stringify({ a: 'foo', b: 'bar' }), |
| 59 | + }) |
| 60 | + try { |
| 61 | + await precommit() |
| 62 | + } catch (e) { |
| 63 | + expect(consoleMock).toHaveBeenCalledWith(translationTaskNeededMessage) |
| 64 | + expect(e.message).toBe('Process exited with code: 1') |
| 65 | + } |
| 66 | + mocks.exec.mockRestore() |
| 67 | + }) |
| 68 | + |
| 69 | + it('should exit with error code 0 when there are files to translate', async () => { |
| 70 | + // simulate staged files |
| 71 | + mocks.exec.mockImplementation((cmd, cb) => { |
| 72 | + if (cmd.includes('wc -l')) { |
| 73 | + return cb(null, { stdout: '1' }) |
| 74 | + } |
| 75 | + return cb(null, { stdout: '' }) |
| 76 | + }) |
| 77 | + // simulate no new translations |
| 78 | + vol.fromJSON({ |
| 79 | + '__mocks__/locales/temp.json': JSON.stringify({ a: 'foo', b: 'bar' }), |
| 80 | + '__mocks__/locales/en-US.json': JSON.stringify({ a: 'foo', b: 'bar' }), |
| 81 | + }) |
| 82 | + try { |
| 83 | + await precommit() |
| 84 | + } catch (e) { |
| 85 | + expect(e.message).toBe('Process exited with code: 0') |
| 86 | + } |
| 87 | + mocks.exec.mockRestore() |
| 88 | + }) |
| 89 | +}) |
0 commit comments