|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +import statusMiddleware from '../statusMiddleware'; |
| 12 | +import connect from 'connect'; |
| 13 | +import http from 'node:http'; |
| 14 | + |
| 15 | +jest.useRealTimers(); |
| 16 | + |
| 17 | +async function fetchStatus(projectRoot: string): Promise<Response> { |
| 18 | + const app = connect().use('/status', statusMiddleware({projectRoot})); |
| 19 | + const server = http.createServer(app); |
| 20 | + await new Promise(resolve => server.listen(0, 'localhost', resolve)); |
| 21 | + try { |
| 22 | + const address = server.address(); |
| 23 | + if (address == null || typeof address === 'string') { |
| 24 | + throw new Error('Expected the server to listen on a TCP port'); |
| 25 | + } |
| 26 | + return await fetch(`http://localhost:${address.port}/status`); |
| 27 | + } finally { |
| 28 | + server.closeAllConnections(); |
| 29 | + await new Promise(resolve => server.close(() => resolve())); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +describe('/status', () => { |
| 34 | + // React Native apps and `isDevServerRunning` depend on this exact response. |
| 35 | + test('reports that the packager is running for the project root', async () => { |
| 36 | + const resp = await fetchStatus('/mocked/path'); |
| 37 | + |
| 38 | + expect(resp.status).toBe(200); |
| 39 | + expect(await resp.text()).toBe('packager-status:running'); |
| 40 | + expect(resp.headers.get('X-React-Native-Project-Root')).toBe( |
| 41 | + '/mocked/path', |
| 42 | + ); |
| 43 | + expect(resp.headers.get('Cache-Control')).toBe('no-store'); |
| 44 | + }); |
| 45 | +}); |
0 commit comments