diff --git a/packages/metro/src/DeltaBundler/__tests__/DeltaCalculator-context-test.js b/packages/metro/src/DeltaBundler/__tests__/DeltaCalculator-context-test.js index 03adaf4416..5749b22494 100644 --- a/packages/metro/src/DeltaBundler/__tests__/DeltaCalculator-context-test.js +++ b/packages/metro/src/DeltaBundler/__tests__/DeltaCalculator-context-test.js @@ -17,7 +17,8 @@ import type {Options, TransformResultDependency} from '../types'; import CountingSet from '../../lib/CountingSet'; import DeltaCalculator from '../DeltaCalculator'; import {Graph} from '../Graph'; -import {createEmitChange} from './test-utils'; +import {createEmitChange, createPathNormalizer} from './test-utils'; +import path from 'path'; const {EventEmitter} = require('events'); @@ -38,6 +39,7 @@ describe('DeltaCalculator + require.context', () => { let deltaCalculator; let fileWatcher; let emitChange; + const p = createPathNormalizer(); const options: Options<> = { unstable_allowRequireContext: true, @@ -64,15 +66,15 @@ describe('DeltaCalculator + require.context', () => { beforeEach(async () => { fileWatcher = new EventEmitter(); - emitChange = createEmitChange(fileWatcher, '/'); + emitChange = createEmitChange(fileWatcher, p('/'), path.sep); markModifiedContextModules.mockImplementation(function ( this: Graph, filePath, modifiedContexts, ) { - if (filePath.startsWith('/ctx/')) { - modifiedContexts.add('/ctx?ctx=xxx'); + if (filePath.startsWith(p('/ctx/'))) { + modifiedContexts.add(p('/ctx?ctx=xxx')); } }); @@ -86,12 +88,12 @@ describe('DeltaCalculator + require.context', () => { this: Graph, options: Options, ): Promise> { - this.dependencies.set('/bundle', { + this.dependencies.set(p('/bundle'), { dependencies: new Map([ [ 'ctx', { - absolutePath: '/ctx?ctx=xxx', + absolutePath: p('/ctx?ctx=xxx'), data: { name: 'ctx', data: { @@ -106,15 +108,15 @@ describe('DeltaCalculator + require.context', () => { ]), inverseDependencies: new CountingSet([]), output: [], - path: '/bundle', + path: p('/bundle'), getSource: () => Buffer.of(), }); - this.dependencies.set('/ctx?ctx=xxx', { + this.dependencies.set(p('/ctx?ctx=xxx'), { dependencies: new Map([ [ 'foo', { - absolutePath: '/ctx/foo', + absolutePath: p('/ctx/foo'), data: { name: 'foo', data: { @@ -127,16 +129,16 @@ describe('DeltaCalculator + require.context', () => { }, ], ]), - inverseDependencies: new CountingSet(['/bundle']), + inverseDependencies: new CountingSet([p('/bundle')]), output: [], - path: '/ctx?ctx=xxx', + path: p('/ctx?ctx=xxx'), getSource: () => Buffer.of(), }); - this.dependencies.set('/ctx/foo', { + this.dependencies.set(p('/ctx/foo'), { dependencies: new Map(), - inverseDependencies: new CountingSet(['/ctx?ctx=xxx']), + inverseDependencies: new CountingSet([p('/ctx?ctx=xxx')]), output: [], - path: '/ctx/foo', + path: p('/ctx/foo'), getSource: () => Buffer.of(), }); @@ -158,7 +160,7 @@ describe('DeltaCalculator + require.context', () => { // $FlowFixMe[underconstrained-implicit-instantiation] deltaCalculator = new DeltaCalculator( - new Set(['/bundle']), + new Set([p('/bundle')]), fileWatcher, options, ); @@ -184,7 +186,7 @@ describe('DeltaCalculator + require.context', () => { }); expect(traverseDependencies).toBeCalledWith( - ['/ctx?ctx=xxx'], + [p('/ctx?ctx=xxx')], expect.anything(), ); @@ -207,7 +209,7 @@ describe('DeltaCalculator + require.context', () => { }); expect(traverseDependencies).toBeCalledWith( - ['/ctx?ctx=xxx'], + [p('/ctx?ctx=xxx')], expect.anything(), ); @@ -227,7 +229,7 @@ describe('DeltaCalculator + require.context', () => { }); expect(traverseDependencies).toBeCalledWith( - ['/ctx/foo'], + [p('/ctx/foo')], expect.anything(), ); @@ -264,7 +266,7 @@ describe('DeltaCalculator + require.context', () => { }); expect(traverseDependencies).toBeCalledWith( - ['/ctx?ctx=xxx'], + [p('/ctx?ctx=xxx')], expect.anything(), ); @@ -303,7 +305,7 @@ describe('DeltaCalculator + require.context', () => { }); expect(traverseDependencies).toBeCalledWith( - ['/ctx/foo'], + [p('/ctx/foo')], expect.anything(), ); }); @@ -323,7 +325,7 @@ describe('DeltaCalculator + require.context', () => { }); expect(traverseDependencies).toBeCalledWith( - ['/ctx?ctx=xxx'], + [p('/ctx?ctx=xxx')], expect.anything(), ); diff --git a/packages/metro/src/DeltaBundler/__tests__/DeltaCalculator-test.js b/packages/metro/src/DeltaBundler/__tests__/DeltaCalculator-test.js index 633974814a..b985f6c6e5 100644 --- a/packages/metro/src/DeltaBundler/__tests__/DeltaCalculator-test.js +++ b/packages/metro/src/DeltaBundler/__tests__/DeltaCalculator-test.js @@ -18,12 +18,11 @@ import type { } from '../types'; import CountingSet from '../../lib/CountingSet'; -import {createEmitChange} from './test-utils'; -import path from 'path'; +import {createEmitChange, createPathNormalizer} from './test-utils'; jest.mock('../../Bundler'); -describe.each(['linux', 'win32'])('DeltaCalculator (%s)', osPlatform => { +describe.each(['posix', 'win32'])('DeltaCalculator (%s)', osPlatform => { let entryModule: Module<$FlowFixMe>; let fooModule: Module<$FlowFixMe>; let barModule: Module<$FlowFixMe>; @@ -35,6 +34,7 @@ describe.each(['linux', 'win32'])('DeltaCalculator (%s)', osPlatform => { let traverseDependencies; let initialTraverseDependencies; let emitChange; + const p = createPathNormalizer(osPlatform); const options: Options<> = { unstable_allowRequireContext: false, @@ -59,18 +59,6 @@ describe.each(['linux', 'win32'])('DeltaCalculator (%s)', osPlatform => { }, }; - function p(posixPath: string): string { - if (osPlatform === 'win32') { - if (path.posix.isAbsolute(posixPath)) { - return path.win32.join('C:\\', ...posixPath.split('/')); - } else { - return posixPath.replaceAll('/', '\\'); - } - } - - return posixPath; - } - beforeEach(async () => { if (osPlatform === 'win32') { jest.doMock('path', () => jest.requireActual('path/win32')); diff --git a/packages/metro/src/DeltaBundler/__tests__/test-utils.js b/packages/metro/src/DeltaBundler/__tests__/test-utils.js index 5d9450c1da..669153c145 100644 --- a/packages/metro/src/DeltaBundler/__tests__/test-utils.js +++ b/packages/metro/src/DeltaBundler/__tests__/test-utils.js @@ -28,7 +28,7 @@ export type ChangeEventInput = { export function createEmitChange( fileWatcher: EventEmitter, rootDir: string, - pathSeparator: string = '/', + pathSeparator: string, ): (changes: ChangeEventInput) => void { return function emitChange(changes: ChangeEventInput): void { const toEntry = ( @@ -59,3 +59,12 @@ export function createEmitChange( }); }; } + +export const createPathNormalizer = ( + platform: 'win32' | 'posix' = process.platform === 'win32' + ? 'win32' + : 'posix', +): (string => string) => + platform === 'win32' + ? posixPath => posixPath.replace(/^\//, 'C:\\').replaceAll('/', '\\') + : posixPath => posixPath; diff --git a/scripts/__tests__/ts-defs-sync-test.js b/scripts/__tests__/ts-defs-sync-test.js index 23ee7ad145..599f99fe86 100644 --- a/scripts/__tests__/ts-defs-sync-test.js +++ b/scripts/__tests__/ts-defs-sync-test.js @@ -28,4 +28,4 @@ test('TypeScript defs are in sync (yarn run build-ts-defs produces no changes)', expect(error.errors).toEqual([]); } expect(error).toBeUndefined(); -}, 30000); +}, 60000);