Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: include CJS transform when testing in Babel preset #662

Merged
merged 2 commits into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions e2e/utils/runHaul.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>,
Expand All @@ -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 {
Expand All @@ -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),
});
}
6 changes: 0 additions & 6 deletions fixtures/react_native_windows_e2e_app/babel.config.js
Original file line number Diff line number Diff line change
@@ -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 }]]
}
]
};
18 changes: 12 additions & 6 deletions packages/haul-babel-preset-react-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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!
{
Expand All @@ -67,12 +78,7 @@ export default function getHaulBabelPreset(
},
{
test: isReactNative,
plugins: [
[
require('@babel/plugin-transform-modules-commonjs'),
{ allowTopLevelThis: true },
],
],
plugins: [commonJsPlugin],
},
{
test: isTypeScriptSource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function build({
options: {
babelrc: false,
configFile: false,
envName: 'development',
presets: ['module:@haul-bundler/babel-preset-react-native'],
},
},
Expand Down