Skip to content

Commit 00458c9

Browse files
Riccardo Cipolleschifacebook-github-bot
Riccardo Cipolleschi
authored andcommitted
Make react-native-codegen tests run in OSS (#34594)
Summary: During the CoreContributor summit, we discovered that the `react-native-codegen` tests cannot be executed in the OSS due to this [issue with Jest](jestjs/jest#2567). This PR moves the required variables inside the proper closure so that we can run tests in the OSS. ## Changelog [General] [Fixed] - Enable the `react-native-codegen` tests in the OSS. Pull Request resolved: #34594 Test Plan: 1. Run `yarn install` in the `react-native-codegen` folder. 2. Run `yarn jest`, see the test fail. 3. Apply the changes in this diff. 4. Run `yarn jest`, see the test pass. Reviewed By: cortinico Differential Revision: D39259164 Pulled By: cipolleschi fbshipit-source-id: 7c4c0a7baa3c9b5e90a7ef75a37a0ec9d1b89db0
1 parent ce50c43 commit 00458c9

File tree

6 files changed

+58
-23
lines changed

6 files changed

+58
-23
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ package-lock.json
111111
# react-native-codegen
112112
/React/FBReactNativeSpec/FBReactNativeSpec
113113
/packages/react-native-codegen/lib
114+
/packages/react-native-codegen/tmp/
114115
/ReactCommon/react/renderer/components/rncore/
115116
/packages/rn-tester/NativeModuleExample/ScreenshotManagerSpec*
116117

118+
117119
# Additional SDKs
118120
/sdks/download
119121
/sdks/hermes

packages/react-native-codegen/src/generators/__tests__/RNCodegen-test.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,38 @@
1313

1414
const rnCodegen = require('../RNCodegen.js');
1515
const fixture = require('../__test_fixtures__/fixtures.js');
16-
const path = require('path');
17-
18-
const outputDirectory = 'tmp/out/';
1916
const packageName = 'na';
2017

2118
describe('RNCodegen.generate', () => {
2219
beforeEach(() => {
2320
jest.resetModules();
2421
});
2522
it('when type `all`, with default paths', () => {
26-
const componentsOutputDir = 'react/renderer/components/library';
27-
const modulesOutputDir = 'library';
28-
29-
const expectedPaths = {
30-
'library.h': modulesOutputDir,
31-
'library-generated.mm': modulesOutputDir,
32-
'ShadowNodes.h': componentsOutputDir,
33-
'ShadowNodes.cpp': componentsOutputDir,
34-
'Props.h': componentsOutputDir,
35-
'Props.cpp': componentsOutputDir,
36-
'RCTComponentViewHelpers.h': componentsOutputDir,
37-
'EventEmitters.h': componentsOutputDir,
38-
'EventEmitters.cpp': componentsOutputDir,
39-
'ComponentDescriptors.h': componentsOutputDir,
40-
};
41-
4223
jest.mock('fs', () => ({
4324
existsSync: location => {
4425
return true;
4526
},
4627
writeFileSync: (location, content) => {
28+
// Jest in the OSS does not allow to capture variables in closures.
29+
// Therefore, we have to bring the variables inside the closure.
30+
// see: https://github.com/facebook/jest/issues/2567
31+
const path = require('path');
32+
const outputDirectory = 'tmp/out/';
33+
const componentsOutputDir = 'react/renderer/components/library';
34+
const modulesOutputDir = 'library';
35+
const expectedPaths = {
36+
'library.h': modulesOutputDir,
37+
'library-generated.mm': modulesOutputDir,
38+
'ShadowNodes.h': componentsOutputDir,
39+
'ShadowNodes.cpp': componentsOutputDir,
40+
'Props.h': componentsOutputDir,
41+
'Props.cpp': componentsOutputDir,
42+
'RCTComponentViewHelpers.h': componentsOutputDir,
43+
'EventEmitters.h': componentsOutputDir,
44+
'EventEmitters.cpp': componentsOutputDir,
45+
'ComponentDescriptors.h': componentsOutputDir,
46+
};
47+
4748
let receivedDir = path.dirname(location);
4849
let receivedBasename = path.basename(location);
4950

@@ -55,6 +56,7 @@ describe('RNCodegen.generate', () => {
5556
},
5657
}));
5758

59+
const outputDirectory = 'tmp/out/';
5860
const res = rnCodegen.generate(
5961
{
6062
libraryName: 'library',

packages/react-native-codegen/src/parsers/flow/components/__tests__/component-parser-test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ const FlowParser = require('../../index.js');
1515
const fixtures = require('../__test_fixtures__/fixtures.js');
1616
const failureFixtures = require('../__test_fixtures__/failures.js');
1717
jest.mock('fs', () => ({
18-
readFileSync: filename => fixtures[filename] || failureFixtures[filename],
18+
readFileSync: filename => {
19+
// Jest in the OSS does not allow to capture variables in closures.
20+
// Therefore, we have to bring the variables inside the closure.
21+
// see: https://github.com/facebook/jest/issues/2567
22+
const readFileFixtures = require('../__test_fixtures__/fixtures.js');
23+
const readFileFailureFixtures = require('../__test_fixtures__/failures.js');
24+
return readFileFixtures[filename] || readFileFailureFixtures[filename];
25+
},
1926
}));
2027

2128
describe('RN Codegen Flow Parser', () => {

packages/react-native-codegen/src/parsers/flow/modules/__tests__/module-parser-snapshot-test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,19 @@
1212
'use strict';
1313

1414
const FlowParser = require('../../index.js');
15+
1516
const fixtures = require('../__test_fixtures__/fixtures.js');
1617
const failureFixtures = require('../__test_fixtures__/failures.js');
18+
1719
jest.mock('fs', () => ({
18-
readFileSync: filename => fixtures[filename] || failureFixtures[filename],
20+
readFileSync: filename => {
21+
// Jest in the OSS does not allow to capture variables in closures.
22+
// Therefore, we have to bring the variables inside the closure.
23+
// see: https://github.com/facebook/jest/issues/2567
24+
const readFileFixtures = require('../__test_fixtures__/fixtures.js');
25+
const readFileFailureFixtures = require('../__test_fixtures__/failures.js');
26+
return readFileFixtures[filename] || readFileFailureFixtures[filename];
27+
},
1928
}));
2029

2130
describe('RN Codegen Flow Parser', () => {

packages/react-native-codegen/src/parsers/typescript/components/__tests__/typescript-component-parser-test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ const TypeScriptParser = require('../../index.js');
1515
const fixtures = require('../__test_fixtures__/fixtures.js');
1616
const failureFixtures = require('../__test_fixtures__/failures.js');
1717
jest.mock('fs', () => ({
18-
readFileSync: filename => fixtures[filename] || failureFixtures[filename],
18+
readFileSync: filename => {
19+
// Jest in the OSS does not allow to capture variables in closures.
20+
// Therefore, we have to bring the variables inside the closure.
21+
// see: https://github.com/facebook/jest/issues/2567
22+
const readFileFixtures = require('../__test_fixtures__/fixtures.js');
23+
const readFileFailureFixtures = require('../__test_fixtures__/failures.js');
24+
return readFileFixtures[filename] || readFileFailureFixtures[filename];
25+
},
1926
}));
2027

2128
describe('RN Codegen TypeScript Parser', () => {

packages/react-native-codegen/src/parsers/typescript/modules/__tests__/typescript-module-parser-snapshot-test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@
1414
const TypeScriptParser = require('../../index.js');
1515
const fixtures = require('../__test_fixtures__/fixtures.js');
1616
const failureFixtures = require('../__test_fixtures__/failures.js');
17+
1718
jest.mock('fs', () => ({
18-
readFileSync: filename => fixtures[filename] || failureFixtures[filename],
19+
readFileSync: filename => {
20+
// Jest in the OSS does not allow to capture variables in closures.
21+
// Therefore, we have to bring the variables inside the closure.
22+
// see: https://github.com/facebook/jest/issues/2567
23+
const readFileFixtures = require('../__test_fixtures__/fixtures.js');
24+
const readFileFailureFixtures = require('../__test_fixtures__/failures.js');
25+
return readFileFixtures[filename] || readFileFailureFixtures[filename];
26+
},
1927
}));
2028

2129
describe('RN Codegen TypeScript Parser', () => {

0 commit comments

Comments
 (0)