1// Reset proxy 2beforeEach(() => { 3 delete process.env.https_proxy; 4}); 5 6it(`returns false when registry.yarnpkg.com can be reached`, async () => { 7 // Mock DNS to fail at finding the URL 8 jest.mock('dns', () => { 9 return { 10 lookup(url, callback) { 11 callback(null); 12 }, 13 }; 14 }); 15 16 const { isYarnOfflineAsync } = require('../yarn'); 17 expect(await isYarnOfflineAsync()).toBe(false); 18}); 19it(`allows an npm proxy`, async () => { 20 // Mock DNS to fail at finding the URL 21 jest.mock('dns', () => { 22 return { 23 lookup(url, callback) { 24 if (url === 'registry.yarnpkg.com') { 25 callback(new Error()); 26 } 27 callback(null); 28 }, 29 }; 30 }); 31 // Mock npm to return a null 32 jest.mock('child_process', () => { 33 return { 34 execSync() { 35 return 'https://expo.dev'; 36 }, 37 }; 38 }); 39 40 const { isYarnOfflineAsync } = require('../yarn'); 41 expect(await isYarnOfflineAsync()).toBe(false); 42}); 43 44describe('getNpmProxy', () => { 45 beforeAll(() => { 46 jest.mock('child_process', () => { 47 return { 48 execSync: () => { 49 if (process.env.YARN_OFFLINE_TEST_VALUE_SHOULD_THROW) { 50 throw new Error('failed'); 51 } 52 return 'something'; 53 }, 54 }; 55 }); 56 }); 57 beforeEach(() => { 58 delete process.env.YARN_OFFLINE_TEST_VALUE_SHOULD_THROW; 59 }); 60 it(`uses the env variable https_proxy for the proxy`, async () => { 61 process.env.https_proxy = 'mock-value'; 62 const { getNpmProxy } = require('../yarn'); 63 expect(getNpmProxy()).toBe('mock-value'); 64 }); 65 it(`returns null when npm cli has an error`, async () => { 66 process.env.YARN_OFFLINE_TEST_VALUE_SHOULD_THROW = 'true'; 67 const { getNpmProxy } = require('../yarn'); 68 expect(getNpmProxy()).toBe(null); 69 }); 70 71 it(`fetches the proxy from npm CLI`, async () => { 72 const { getNpmProxy } = require('../yarn'); 73 expect(getNpmProxy()).toBe('something'); 74 }); 75}); 76