diff --git a/e2e/utils/runHaul.ts b/e2e/utils/runHaul.ts index 7a7b19c3..4a47f11b 100644 --- a/e2e/utils/runHaul.ts +++ b/e2e/utils/runHaul.ts @@ -22,6 +22,14 @@ type RunHaulOptions = { skipPkgJsonCheck?: boolean; // don't complain if can't find package.json }; +function getEnv(options: RunHaulOptions) { + return { + ...process.env, + ...(options.nodePath ? { NODE_PATH: options.nodePath } : {}), + NODE_ENV: 'development', + }; +} + export function runHaulSync( dir: string, args?: Array, @@ -34,13 +42,9 @@ export function runHaulSync( cwd = path.resolve(__dirname, cwd); } - const env = options.nodePath - ? Object.assign({}, process.env, { NODE_PATH: options.nodePath }) - : process.env; - const result = spawnSync(NYC_BIN, [...NYC_ARGS, BIN_PATH, ...(args || [])], { cwd, - env, + env: getEnv(options), }); return { @@ -62,9 +66,8 @@ export function runHaul( cwd = path.resolve(__dirname, cwd); } - const env = options.nodePath - ? Object.assign({}, process.env, { NODE_PATH: options.nodePath }) - : process.env; - - return spawn(NYC_BIN, [...NYC_ARGS, BIN_PATH, ...(args || [])], { cwd, env }); + return spawn(NYC_BIN, [...NYC_ARGS, BIN_PATH, ...(args || [])], { + cwd, + env: getEnv(options), + }); } diff --git a/fixtures/react_native_windows_e2e_app/babel.config.js b/fixtures/react_native_windows_e2e_app/babel.config.js index d470cfc5..b316e233 100755 --- a/fixtures/react_native_windows_e2e_app/babel.config.js +++ b/fixtures/react_native_windows_e2e_app/babel.config.js @@ -1,9 +1,3 @@ module.exports = { presets: [['module:../../packages/haul-babel-preset-react-native', { chakra: true }]], - overrides: [ - { - test: /__tests__/, - plugins: [['@babel/plugin-transform-modules-commonjs', { allowTopLevelThis: true }]] - } - ] }; diff --git a/packages/haul-babel-preset-react-native/src/index.ts b/packages/haul-babel-preset-react-native/src/index.ts index c4b58f90..4d0bf53b 100644 --- a/packages/haul-babel-preset-react-native/src/index.ts +++ b/packages/haul-babel-preset-react-native/src/index.ts @@ -27,6 +27,11 @@ const chakraPlugins = [ [require('@babel/plugin-proposal-object-rest-spread')], ]; +const commonJsPlugin = [ + require('@babel/plugin-transform-modules-commonjs'), + { allowTopLevelThis: true }, +]; + function isTypeScriptSource(fileName: string) { return !!fileName && fileName.endsWith('.ts'); } @@ -45,6 +50,12 @@ export default function getHaulBabelPreset( ) { return { compact: false, + env: { + // Add CommonJS transform when running in NODE_ENV === test, for example when testing. + test: { + plugins: [commonJsPlugin], + }, + }, overrides: [ // The flow strip types plugin must go BEFORE class properties! { @@ -67,12 +78,7 @@ export default function getHaulBabelPreset( }, { test: isReactNative, - plugins: [ - [ - require('@babel/plugin-transform-modules-commonjs'), - { allowTopLevelThis: true }, - ], - ], + plugins: [commonJsPlugin], }, { test: isTypeScriptSource, diff --git a/packages/haul-ram-bundle-webpack-plugin/src/__tests__/integration.test.ts b/packages/haul-ram-bundle-webpack-plugin/src/__tests__/integration.test.ts index d379b30f..3f3464d3 100644 --- a/packages/haul-ram-bundle-webpack-plugin/src/__tests__/integration.test.ts +++ b/packages/haul-ram-bundle-webpack-plugin/src/__tests__/integration.test.ts @@ -42,6 +42,7 @@ function build({ options: { babelrc: false, configFile: false, + envName: 'development', presets: ['module:@haul-bundler/babel-preset-react-native'], }, },