import { transformSync } from '@babel/core'; import { afterEach, beforeEach, describe, expect, jest, test, } from '@jest/globals'; const mockWriteFileSync = jest.fn(); const mockRenameSync = jest.fn(); const mockRm = jest.fn(); const mockReadFileSync = jest.fn(); const actualFs: typeof import('fs') = jest.requireActual('fs'); const WORKLET_FILE = /[/\\]\.worklets[/\\]\d+\.js$/; function mockWorkletFileRead(read: () => string) { mockReadFileSync.mockImplementation((...args: unknown[]) => WORKLET_FILE.test(String(args[0])) ? read() : (actualFs.readFileSync as (...a: unknown[]) => unknown)(...args) ); } jest.mock('fs', () => { const actual: object = jest.requireActual('fs'); return { ...actual, writeFileSync: (...args: unknown[]) => mockWriteFileSync(...args), renameSync: (...args: unknown[]) => mockRenameSync(...args), rm: (...args: unknown[]) => mockRm(...args), readFileSync: (...args: unknown[]) => mockReadFileSync(...args), }; }); // eslint-disable-next-line import/first import plugin from '../index'; const INPUT = ` function foo() { 'worklet'; return 1; } `; function runPlugin() { return transformSync(INPUT, { filename: 'test.js', babelrc: false, configFile: false, plugins: [[plugin, { disableSourceMaps: true, bundleMode: true }]], }); } describe('worklet file write', () => { beforeEach(() => { process.env.WORKLETS_JEST_SHOULD_MOCK_VERSION = '1'; mockWriteFileSync.mockReset(); mockRenameSync.mockReset(); mockRm.mockReset(); mockReadFileSync.mockReset(); mockWorkletFileRead(() => { throw Object.assign(new Error('missing'), { code: 'ENOENT' }); }); }); test('writes to a temporary file and renames it over the worklet file', () => { runPlugin(); expect(mockWriteFileSync).toHaveBeenCalledTimes(1); expect(mockRenameSync).toHaveBeenCalledTimes(1); const [tempPath, content] = mockWriteFileSync.mock.calls[0] as [ string, string, ]; const [renamedFrom, renamedTo] = mockRenameSync.mock.calls[0] as [ string, string, ]; expect(renamedTo).toMatch(/[/\\]\.worklets[/\\]\d+\.js$/); expect(renamedFrom).toBe(tempPath); expect(tempPath.startsWith(`${renamedTo}.`)).toBe(true); expect(tempPath).toMatch(/\.tmp$/); expect(content).toContain('export default'); expect(mockRm).not.toHaveBeenCalled(); }); test('uses a different temporary file for each write', () => { runPlugin(); runPlugin(); const [first, second] = mockWriteFileSync.mock.calls.map( ([path]) => path as string ); expect(first).not.toBe(second); }); test('removes the temporary file when the rename fails', () => { mockRenameSync.mockImplementation(() => { throw new Error('rename failed'); }); expect(runPlugin).toThrow('rename failed'); const [tempPath] = mockWriteFileSync.mock.calls[0] as [string]; expect(mockRm).toHaveBeenCalledWith( tempPath, { force: true }, expect.any(Function) ); }); test('ignores a failed cleanup and rethrows the write error', () => { mockRenameSync.mockImplementation(() => { throw new Error('rename failed'); }); mockRm.mockImplementation((...args: unknown[]) => { (args[2] as (error: Error) => void)(new Error('cleanup failed')); }); expect(runPlugin).toThrow('rename failed'); }); describe.each([ { platform: 'win32', code: 'EPERM', target: 'same', throws: false }, { platform: 'win32', code: 'EACCES', target: 'same', throws: false }, { platform: 'win32', code: 'EBUSY', target: 'same', throws: false }, { platform: 'win32', code: 'EPERM', target: 'different', throws: true }, { platform: 'win32', code: 'EPERM', target: 'missing', throws: true }, { platform: 'win32', code: 'ENOSPC', target: 'same', throws: true }, { platform: 'darwin', code: 'EPERM', target: 'same', throws: true }, { platform: 'linux', code: 'EACCES', target: 'same', throws: true }, ])( 'rename fails with $code on $platform, target: $target', ({ platform, code, target, throws }) => { const originalPlatform = process.platform; beforeEach(() => { Object.defineProperty(process, 'platform', { value: platform }); mockRenameSync.mockImplementation(() => { throw Object.assign(new Error(`rename failed: ${code}`), { code }); }); mockWorkletFileRead(() => { if (target === 'missing') { throw Object.assign(new Error('missing'), { code: 'ENOENT' }); } if (target === 'different') { return 'stale content'; } return (mockWriteFileSync.mock.calls[0] as [string, string])[1]; }); }); afterEach(() => { Object.defineProperty(process, 'platform', { value: originalPlatform, }); }); test(throws ? 'rethrows the error' : 'keeps the existing file', () => { if (throws) { expect(runPlugin).toThrow(`rename failed: ${code}`); } else { expect(runPlugin).not.toThrow(); } const [tempPath] = mockWriteFileSync.mock.calls[0] as [string]; expect(mockRm).toHaveBeenCalledWith( tempPath, { force: true }, expect.any(Function) ); }); } ); test('rethrows a failed temporary file write on Windows even when the target exists', () => { const originalPlatform = process.platform; Object.defineProperty(process, 'platform', { value: 'win32' }); mockWorkletFileRead( () => (mockWriteFileSync.mock.calls[0] as [string, string])[1] ); mockWriteFileSync.mockImplementation(() => { throw Object.assign(new Error('write failed: EPERM'), { code: 'EPERM' }); }); try { expect(runPlugin).toThrow('write failed: EPERM'); expect(mockRenameSync).not.toHaveBeenCalled(); } finally { Object.defineProperty(process, 'platform', { value: originalPlatform }); } }); });