|
1 | 1 | import type { EventEmitter } from 'node:events'; |
2 | 2 | import { Worker } from 'node:worker_threads'; |
| 3 | +import { fs, vol } from 'memfs'; |
3 | 4 | import type { Reporter } from '../../../logging/types.js'; |
4 | 5 | import { Compiler } from '../Compiler.js'; |
5 | 6 |
|
| 7 | +jest.mock('node:fs', () => jest.requireActual('memfs').fs); |
| 8 | + |
6 | 9 | jest.mock('node:worker_threads', () => { |
7 | 10 | const { EventEmitter } = |
8 | 11 | jest.requireActual<typeof import('node:events')>('node:events'); |
@@ -70,3 +73,42 @@ test('terminates active workers when closed', async () => { |
70 | 73 |
|
71 | 74 | expect(worker.terminate).toHaveBeenCalledTimes(1); |
72 | 75 | }); |
| 76 | + |
| 77 | +describe('getSource', () => { |
| 78 | + const reporter: Reporter = { |
| 79 | + process: jest.fn(), |
| 80 | + flush: jest.fn(), |
| 81 | + stop: jest.fn(), |
| 82 | + }; |
| 83 | + |
| 84 | + const createCompiler = () => |
| 85 | + new Compiler(['ios'], { host: '' }, reporter, '/project', '/react-native'); |
| 86 | + |
| 87 | + beforeEach(() => { |
| 88 | + vol.reset(); |
| 89 | + }); |
| 90 | + |
| 91 | + afterEach(() => { |
| 92 | + jest.restoreAllMocks(); |
| 93 | + }); |
| 94 | + |
| 95 | + test('reads an absolute filename as-is', async () => { |
| 96 | + vol.fromJSON({ '/outside/project/file.js': 'absolute source' }); |
| 97 | + const readFile = jest.spyOn(fs.promises, 'readFile'); |
| 98 | + |
| 99 | + await expect( |
| 100 | + createCompiler().getSource('/outside/project/file.js', 'ios') |
| 101 | + ).resolves.toBe('absolute source'); |
| 102 | + expect(readFile).toHaveBeenCalledWith('/outside/project/file.js', 'utf8'); |
| 103 | + }); |
| 104 | + |
| 105 | + test('resolves a relative filename against the project root', async () => { |
| 106 | + vol.fromJSON({ '/project/src/index.js': 'source under the project root' }); |
| 107 | + const readFile = jest.spyOn(fs.promises, 'readFile'); |
| 108 | + |
| 109 | + await expect( |
| 110 | + createCompiler().getSource('src/index.js', 'ios') |
| 111 | + ).resolves.toBe('source under the project root'); |
| 112 | + expect(readFile).toHaveBeenCalledWith('/project/src/index.js', 'utf8'); |
| 113 | + }); |
| 114 | +}); |
0 commit comments