Skip to content

Commit

Permalink
Make react-native-codegen tests run in OSS (#34594)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Riccardo Cipolleschi authored and facebook-github-bot committed Sep 6, 2022
1 parent ce50c43 commit 00458c9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ package-lock.json
# react-native-codegen
/React/FBReactNativeSpec/FBReactNativeSpec
/packages/react-native-codegen/lib
/packages/react-native-codegen/tmp/
/ReactCommon/react/renderer/components/rncore/
/packages/rn-tester/NativeModuleExample/ScreenshotManagerSpec*


# Additional SDKs
/sdks/download
/sdks/hermes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,38 @@

const rnCodegen = require('../RNCodegen.js');
const fixture = require('../__test_fixtures__/fixtures.js');
const path = require('path');

const outputDirectory = 'tmp/out/';
const packageName = 'na';

describe('RNCodegen.generate', () => {
beforeEach(() => {
jest.resetModules();
});
it('when type `all`, with default paths', () => {
const componentsOutputDir = 'react/renderer/components/library';
const modulesOutputDir = 'library';

const expectedPaths = {
'library.h': modulesOutputDir,
'library-generated.mm': modulesOutputDir,
'ShadowNodes.h': componentsOutputDir,
'ShadowNodes.cpp': componentsOutputDir,
'Props.h': componentsOutputDir,
'Props.cpp': componentsOutputDir,
'RCTComponentViewHelpers.h': componentsOutputDir,
'EventEmitters.h': componentsOutputDir,
'EventEmitters.cpp': componentsOutputDir,
'ComponentDescriptors.h': componentsOutputDir,
};

jest.mock('fs', () => ({
existsSync: location => {
return true;
},
writeFileSync: (location, content) => {
// Jest in the OSS does not allow to capture variables in closures.
// Therefore, we have to bring the variables inside the closure.
// see: https://github.com/facebook/jest/issues/2567
const path = require('path');
const outputDirectory = 'tmp/out/';
const componentsOutputDir = 'react/renderer/components/library';
const modulesOutputDir = 'library';
const expectedPaths = {
'library.h': modulesOutputDir,
'library-generated.mm': modulesOutputDir,
'ShadowNodes.h': componentsOutputDir,
'ShadowNodes.cpp': componentsOutputDir,
'Props.h': componentsOutputDir,
'Props.cpp': componentsOutputDir,
'RCTComponentViewHelpers.h': componentsOutputDir,
'EventEmitters.h': componentsOutputDir,
'EventEmitters.cpp': componentsOutputDir,
'ComponentDescriptors.h': componentsOutputDir,
};

let receivedDir = path.dirname(location);
let receivedBasename = path.basename(location);

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

const outputDirectory = 'tmp/out/';
const res = rnCodegen.generate(
{
libraryName: 'library',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ const FlowParser = require('../../index.js');
const fixtures = require('../__test_fixtures__/fixtures.js');
const failureFixtures = require('../__test_fixtures__/failures.js');
jest.mock('fs', () => ({
readFileSync: filename => fixtures[filename] || failureFixtures[filename],
readFileSync: filename => {
// Jest in the OSS does not allow to capture variables in closures.
// Therefore, we have to bring the variables inside the closure.
// see: https://github.com/facebook/jest/issues/2567
const readFileFixtures = require('../__test_fixtures__/fixtures.js');
const readFileFailureFixtures = require('../__test_fixtures__/failures.js');
return readFileFixtures[filename] || readFileFailureFixtures[filename];
},
}));

describe('RN Codegen Flow Parser', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@
'use strict';

const FlowParser = require('../../index.js');

const fixtures = require('../__test_fixtures__/fixtures.js');
const failureFixtures = require('../__test_fixtures__/failures.js');

jest.mock('fs', () => ({
readFileSync: filename => fixtures[filename] || failureFixtures[filename],
readFileSync: filename => {
// Jest in the OSS does not allow to capture variables in closures.
// Therefore, we have to bring the variables inside the closure.
// see: https://github.com/facebook/jest/issues/2567
const readFileFixtures = require('../__test_fixtures__/fixtures.js');
const readFileFailureFixtures = require('../__test_fixtures__/failures.js');
return readFileFixtures[filename] || readFileFailureFixtures[filename];
},
}));

describe('RN Codegen Flow Parser', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ const TypeScriptParser = require('../../index.js');
const fixtures = require('../__test_fixtures__/fixtures.js');
const failureFixtures = require('../__test_fixtures__/failures.js');
jest.mock('fs', () => ({
readFileSync: filename => fixtures[filename] || failureFixtures[filename],
readFileSync: filename => {
// Jest in the OSS does not allow to capture variables in closures.
// Therefore, we have to bring the variables inside the closure.
// see: https://github.com/facebook/jest/issues/2567
const readFileFixtures = require('../__test_fixtures__/fixtures.js');
const readFileFailureFixtures = require('../__test_fixtures__/failures.js');
return readFileFixtures[filename] || readFileFailureFixtures[filename];
},
}));

describe('RN Codegen TypeScript Parser', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@
const TypeScriptParser = require('../../index.js');
const fixtures = require('../__test_fixtures__/fixtures.js');
const failureFixtures = require('../__test_fixtures__/failures.js');

jest.mock('fs', () => ({
readFileSync: filename => fixtures[filename] || failureFixtures[filename],
readFileSync: filename => {
// Jest in the OSS does not allow to capture variables in closures.
// Therefore, we have to bring the variables inside the closure.
// see: https://github.com/facebook/jest/issues/2567
const readFileFixtures = require('../__test_fixtures__/fixtures.js');
const readFileFailureFixtures = require('../__test_fixtures__/failures.js');
return readFileFixtures[filename] || readFileFailureFixtures[filename];
},
}));

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

0 comments on commit 00458c9

Please sign in to comment.