Skip to content

Commit 6c8f4bf

Browse files
rubennortefacebook-github-bot
authored andcommitted
Automatically inject debugger statements in tests in preparation for debug mode (#53205)
Summary: Pull Request resolved: #53205 Changelog: [internal] This injects a custom Babel transform for Fantom tests that automatically injects `debugger` statements in the generated code. This simplifies debugging by providing a default interruption point in the test setup for the test author to decide what to debug. This has no effect unless the debugger is opened, which isn't happening yet. Reviewed By: rshest Differential Revision: D79996000
1 parent 11d3f4d commit 6c8f4bf

3 files changed

Lines changed: 85 additions & 1 deletion

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @noflow
8+
* @format
9+
*/
10+
11+
/**
12+
* This transform injects a single `debugger;` statement at the top of every
13+
* Fantom test, so we can automatically stop on them when debugging.
14+
*/
15+
module.exports = function ({types: t}) {
16+
return {
17+
name: 'inject-debugger-statements-in-tests',
18+
visitor: {
19+
Program(path, state) {
20+
const filename = state.filename || '';
21+
if (
22+
(filename.endsWith('-itest.js') ||
23+
filename.endsWith('-itest.fb.js')) &&
24+
!filename.includes('/.out/')
25+
) {
26+
// Check if the first statement is already a debugger statement
27+
const first = path.node.body[0];
28+
if (!first || first.type !== 'DebuggerStatement') {
29+
path.unshiftContainer('body', t.debuggerStatement());
30+
}
31+
}
32+
},
33+
},
34+
};
35+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
import type {
12+
BabelTransformer,
13+
BabelTransformerArgs,
14+
} from 'metro-babel-transformer';
15+
16+
import MetroBabelTransformer from '@react-native/metro-babel-transformer';
17+
import crypto from 'crypto';
18+
import fs from 'fs';
19+
20+
const transform: BabelTransformer['transform'] = (
21+
args: BabelTransformerArgs,
22+
) => {
23+
const processedArgs = {
24+
...args,
25+
plugins: [
26+
...(args.plugins ?? []),
27+
// $FlowExpectedError[untyped-import]
28+
require('./babel-plugins/inject-debugger-statements-in-tests'),
29+
],
30+
};
31+
return MetroBabelTransformer.transform(processedArgs);
32+
};
33+
34+
module.exports = {
35+
...MetroBabelTransformer,
36+
transform,
37+
getCacheKey(): string {
38+
const key = crypto.createHash('md5');
39+
const cacheKeyParts = [
40+
MetroBabelTransformer.getCacheKey?.() ?? '',
41+
fs.readFileSync(__filename),
42+
fs.readFileSync(
43+
require.resolve('./babel-plugins/inject-debugger-statements-in-tests'),
44+
),
45+
];
46+
cacheKeyParts.forEach(part => key.update(part));
47+
return key.digest('hex');
48+
},
49+
};

private/react-native-fantom/config/metro-babel-transformer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
'use strict';
1212

1313
require('../../../scripts/shared/babelRegister').registerForMonorepo();
14-
module.exports = require('@react-native/metro-babel-transformer');
14+
module.exports = require('./metro-babel-transformer.flow');

0 commit comments

Comments
 (0)