Skip to content

Commit 642d9b7

Browse files
committed
feat: add support to load and use process.env in the test app
1 parent 4d3feeb commit 642d9b7

File tree

10 files changed

+129
-31
lines changed

10 files changed

+129
-31
lines changed

app/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const filterRNLogs = () => {
1616
const originalFn = console[level];
1717

1818
console[level] = (...args) => {
19-
if (args?.[0]?.match(filterRegExp)) {
19+
if (args?.[0]?.match?.(filterRegExp)) {
2020
return;
2121
}
2222
originalFn.apply(console, args);

babel.config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22

33
module.exports = {
44
presets: ['module:metro-react-native-babel-preset'],
5-
plugins: ['@babel/plugin-proposal-async-generator-functions'],
5+
plugins: [
6+
'@babel/plugin-proposal-async-generator-functions',
7+
'transform-inline-environment-variables',
8+
],
69
};

cli/test-runners/index.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
'use strict';
22

3+
const path = require('path');
4+
const fs = require('fs');
5+
const tempy = require('tempy');
36
const createSignal = require('pico-signals');
4-
const { expandGlobs, writeTestSuiteEntryModule, getCommonParentDirectories } = require('../utils');
7+
const { expandGlobs, getCommonParentDirectories } = require('../utils');
58
const OraBundleProgress = require('../bundle-progress/ora');
69

710
const createTestReporter = (testRunner) => {
@@ -95,7 +98,25 @@ const createTestReporter = (testRunner) => {
9598
});
9699
};
97100

98-
module.exports = ({ cwd, runner, require: preloadModulePath }, testFileGlobs = []) => {
101+
const writeTestSuiteEntryModule = (cwd, testFilePaths, { preloadModulePath } = {}) => {
102+
const tempFile = tempy.file({
103+
name: 'test-suite.js',
104+
});
105+
const testSuite = [preloadModulePath, ...testFilePaths]
106+
.filter(Boolean)
107+
.map((file) => `require('${path.resolve(cwd, file)}');`)
108+
.join('\n');
109+
110+
fs.writeFileSync(tempFile, testSuite);
111+
112+
return tempFile;
113+
};
114+
115+
module.exports = ({
116+
cwd,
117+
runner,
118+
require: preloadModulePath,
119+
}, testFileGlobs = []) => {
99120
const DEFAULT_TEST_FILE_GLOB = '**/test?(s)/**/?(*.)+(spec|test).js';
100121

101122
const testFilePaths = expandGlobs(cwd, testFileGlobs.length === 0 ? DEFAULT_TEST_FILE_GLOB : testFileGlobs);

cli/utils.js

-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
'use strict';
22

33
const path = require('path');
4-
const fs = require('fs-extra');
54
const globby = require('globby');
6-
const tempy = require('tempy');
75
const findUp = require('find-up');
86

97
const expandGlobs = (cwd, globPatterns) => {
@@ -25,20 +23,6 @@ const expandGlobs = (cwd, globPatterns) => {
2523
return filePaths;
2624
};
2725

28-
const writeTestSuiteEntryModule = (cwd, testFilePaths, { preloadModulePath } = {}) => {
29-
const tempFile = tempy.file({
30-
name: 'test-suite.js',
31-
});
32-
const testSuite = [preloadModulePath, ...testFilePaths]
33-
.filter(Boolean)
34-
.map((file) => `require('${path.resolve(cwd, file)}');`)
35-
.join('\n');
36-
37-
fs.writeFileSync(tempFile, testSuite);
38-
39-
return tempFile;
40-
};
41-
4226
// Cwd: .
4327
// test-suite-1/test.js
4428
// test-suite-1/foo/test.js
@@ -97,7 +81,6 @@ const findMonoRepoRoot = (cwd) => {
9781

9882
module.exports = {
9983
expandGlobs,
100-
writeTestSuiteEntryModule,
10184
getCommonParentDirectories,
10285
findMonoRepoRoot,
10386
};

fixtures/mocha/env/test.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
const { expect } = require('chai');
4+
5+
it('environment variables loading works', () => {
6+
expect(process.env.HELLO).to.equal('hello');
7+
expect(process.env.WORLD).to.equal('world');
8+
});
9+

fixtures/zora/env/test.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
const { test } = require('zora');
4+
5+
console.log({ env: process.env });
6+
7+
test('environment variables loading works', (t) => {
8+
t.eq(process.env.FOO, 'foo');
9+
t.eq(process.env.BAR, 'bar');
10+
});

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
},
3939
"dependencies": {
4040
"@babel/plugin-proposal-async-generator-functions": "^7.12.12",
41+
"babel-plugin-transform-inline-environment-variables": "^0.4.3",
4142
"chai": "^4.2.0",
4243
"execa": "^4.1.0",
4344
"find-up": "^5.0.0",

test/android.test.js

+39
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,25 @@ describe('zora', () => {
334334
expect(testResults.ok).toBe(true);
335335
expect(testResults.pass).toBe(2);
336336
});
337+
338+
test('load environment variables', async () => {
339+
const process = await execa('./cli/index.js', [
340+
'--platform',
341+
'android',
342+
'--emulator',
343+
ANDROID_EMULATOR,
344+
'--runner',
345+
'zora',
346+
'fixtures/zora/env/test.js',
347+
], {
348+
env: {
349+
FOO: 'foo',
350+
BAR: 'bar',
351+
},
352+
});
353+
354+
expect(process.exitCode).toBe(0);
355+
});
337356
});
338357

339358
describe('mocha', () => {
@@ -537,4 +556,24 @@ describe('mocha', () => {
537556
);
538557
expect(process.stdout).toEqual(expect.stringContaining('2 passing'));
539558
});
559+
560+
test('load environment variables', async () => {
561+
const process = await execa('./cli/index.js', [
562+
'--platform',
563+
'android',
564+
'--emulator',
565+
ANDROID_EMULATOR,
566+
'--runner',
567+
'mocha',
568+
'fixtures/mocha/env/test.js',
569+
], {
570+
env: {
571+
HELLO: 'hello',
572+
WORLD: 'world',
573+
},
574+
});
575+
576+
expect(process.exitCode).toBe(0);
577+
expect(process.stdout).toEqual(expect.stringContaining('environment variables loading works'));
578+
});
540579
});

test/ios.test.js

+37-10
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,6 @@ describe('zora', () => {
288288
});
289289

290290
expect(process.exitCode).toBe(0);
291-
292-
const tapCompleteEvent = parseTap(process.stdout).pop();
293-
const testResults = tapCompleteEvent[1];
294-
295-
expect(testResults.ok).toBe(true);
296-
expect(testResults.pass).toBe(1);
297291
});
298292

299293
test('patch test app dependencies', async () => {
@@ -315,12 +309,25 @@ describe('zora', () => {
315309
});
316310

317311
expect(process.exitCode).toBe(0);
312+
});
318313

319-
const tapCompleteEvent = parseTap(process.stdout).pop();
320-
const testResults = tapCompleteEvent[1];
314+
test('load environment variables', async () => {
315+
const process = await execa('./cli/index.js', [
316+
'--platform',
317+
'ios',
318+
'--simulator',
319+
IOS_SIMULATOR,
320+
'--runner',
321+
'zora',
322+
'fixtures/zora/env/test.js',
323+
], {
324+
env: {
325+
FOO: 'foo',
326+
BAR: 'bar',
327+
},
328+
});
321329

322-
expect(testResults.ok).toBe(true);
323-
expect(testResults.pass).toBe(2);
330+
expect(process.exitCode).toBe(0);
324331
});
325332
});
326333

@@ -501,4 +508,24 @@ describe('mocha', () => {
501508
expect(process.stdout).toEqual(expect.stringContaining('FileReader.readAsArrayBuffer patch works'));
502509
expect(process.stdout).toEqual(expect.stringContaining('2 passing'));
503510
});
511+
512+
test('load environment variables', async () => {
513+
const process = await execa('./cli/index.js', [
514+
'--platform',
515+
'ios',
516+
'--simulator',
517+
IOS_SIMULATOR,
518+
'--runner',
519+
'mocha',
520+
'fixtures/mocha/env/test.js',
521+
], {
522+
env: {
523+
HELLO: 'hello',
524+
WORLD: 'world',
525+
},
526+
});
527+
528+
expect(process.exitCode).toBe(0);
529+
expect(process.stdout).toEqual(expect.stringContaining('environment variables loading works'));
530+
});
504531
});

0 commit comments

Comments
 (0)