-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jest): add @react-native-esbuild/jest
- Loading branch information
1 parent
2ca9a8b
commit 53b1874
Showing
29 changed files
with
457 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+3.82 KB
.yarn/cache/@jest-create-cache-key-function-npm-27.5.1-d4c8561229-a6c3a8c769.zip
Binary file not shown.
Binary file added
BIN
+4.83 KB
.yarn/cache/@jest-create-cache-key-function-npm-29.7.0-786396764f-681bc761fa.zip
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+1.72 MB
.yarn/cache/swc-plugin-coverage-instrument-npm-0.0.20-e82119d110-4d182c67b9.zip
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const { resolve } = require('node:path'); | ||
|
||
const project = resolve(__dirname, 'tsconfig.json'); | ||
|
||
/** @type { import('eslint').ESLint.ConfigData } */ | ||
module.exports = { | ||
root: true, | ||
env: { | ||
node: true, | ||
}, | ||
plugins: ['prettier'], | ||
extends: [ | ||
require.resolve('@vercel/style-guide/eslint/node'), | ||
require.resolve('@vercel/style-guide/eslint/typescript'), | ||
], | ||
parserOptions: { | ||
project, | ||
}, | ||
settings: { | ||
'import/resolver': { | ||
typescript: { | ||
project, | ||
}, | ||
}, | ||
}, | ||
overrides: [ | ||
{ | ||
files: ['*.ts?(x)', '*.js?(x)'], | ||
rules: { | ||
semi: ['error', 'always'], | ||
quotes: ['error', 'single'], | ||
'object-curly-spacing': ['error', 'always'], | ||
'array-bracket-spacing': 'off', | ||
'no-console': 'off', | ||
'unicorn/filename-case': 'off', | ||
'prettier/prettier': 'error', | ||
}, | ||
}, | ||
{ | ||
files: ['*.test.ts?(x)', '*.spec.js?(x)'], | ||
rules: { | ||
'import/no-named-as-default-member': 'off', | ||
}, | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,7 @@ yarn-error.log | |
|
||
# @react-native-esbuild | ||
.rne | ||
.swc | ||
|
||
# @react-native-esbuild for web | ||
public/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,11 @@ | ||
/** | ||
* @format | ||
*/ | ||
|
||
import 'react-native'; | ||
import React from 'react'; | ||
import App from '../src/App'; | ||
|
||
// Note: import explicitly to use the types shiped with jest. | ||
import {it} from '@jest/globals'; | ||
|
||
// Note: test renderer must be required after react-native. | ||
import renderer from 'react-test-renderer'; | ||
|
||
it('renders correctly', () => { | ||
renderer.create(<App />); | ||
import { describe, it } from '@jest/globals'; | ||
import renderer, { act } from 'react-test-renderer'; | ||
import { App } from '../src/App'; | ||
|
||
describe('App', () => { | ||
it('renders correctly', async () => { | ||
await act(() => { | ||
renderer.create(<App />); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
module.exports = { | ||
presets: ['module:metro-react-native-babel-preset'], | ||
plugins: ['react-native-reanimated/plugin'], | ||
plugins: [ | ||
['@babel/plugin-transform-private-methods', { loose: true }], | ||
'react-native-reanimated/plugin', | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,30 @@ | ||
const TRANSFORM_PACKAGES = [ | ||
'react-native', | ||
'jest-react-native', | ||
'@react-native', | ||
'@react-native-community', | ||
'@react-navigation', | ||
'@expo/html-elements', | ||
'dripsy', | ||
]; | ||
|
||
/** | ||
* @type {import('jest').Config} | ||
*/ | ||
module.exports = { | ||
preset: 'react-native', | ||
transform: { | ||
'^.+\\.(t|j)sx?$': '@react-native-esbuild/jest', | ||
}, | ||
transformIgnorePatterns: [ | ||
`node_modules/(?!${TRANSFORM_PACKAGES.join('|')})/`, | ||
], | ||
testPathIgnorePatterns: ['dist'], | ||
coveragePathIgnorePatterns: ['node_modules'], | ||
collectCoverageFrom: [ | ||
'src/**/*.{js,jsx,ts,tsx}', | ||
'!**/*.d.ts', | ||
'!**/index.ts', | ||
], | ||
setupFilesAfterEnv: ['./tests/setup.ts'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import 'react-native-gesture-handler/jestSetup'; | ||
import { setUpTests } from 'react-native-reanimated'; | ||
|
||
setUpTests(); | ||
|
||
jest.mock('@react-navigation/devtools', () => ({ | ||
useFlipper: jest.fn(), | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# preset | ||
jest-preset.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# `@react-native-esbuild/jest` | ||
|
||
> react-native preset for jest powered by @react-native-esbuild | ||
## Usage | ||
|
||
```js | ||
exports.default = { | ||
preset: 'react-native', | ||
transform: { | ||
'^.+\\.(t|j)sx?$': '@react-native-esbuild/jest', | ||
}, | ||
}; | ||
|
||
// With transformer options(for enable custom instrument). | ||
exports.default = { | ||
preset: 'react-native', | ||
transform: { | ||
'^.+\\.(t|j)sx?$': ['@react-native-esbuild/jest', /* TransformerConfig */], | ||
}, | ||
}; | ||
``` | ||
|
||
```ts | ||
/** | ||
* @see {@link https://github.com/kwonoj/swc-plugin-coverage-instrument} | ||
*/ | ||
interface TransformerConfig { | ||
experimental?: { | ||
customCoverageInstrumentation?: { | ||
coverageVariable?: string; | ||
compact: boolean; | ||
reportLogic: boolean; | ||
ignoreClassMethods: string[]; | ||
instrumentLog?: { | ||
level: string; | ||
enableTrace: boolean; | ||
}; | ||
}; | ||
}; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const esbuild = require('esbuild'); | ||
const { getEsbuildBaseOptions } = require('../../../shared'); | ||
|
||
const buildOptions = getEsbuildBaseOptions(__dirname); | ||
|
||
esbuild.build(buildOptions).catch((error) => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { TransformerFactory } from '@jest/transform'; | ||
import { createTransformer } from './transformer'; | ||
|
||
const factory: TransformerFactory<ReturnType<typeof createTransformer>> = { | ||
createTransformer, | ||
}; | ||
|
||
export default factory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import type { | ||
Transformer, | ||
TransformOptions, | ||
TransformedSource, | ||
} from '@jest/transform'; | ||
import getCacheKeyFunction from '@jest/create-cache-key-function'; | ||
import md5 from 'md5'; | ||
import { ReactNativeEsbuildBundler } from '@react-native-esbuild/core'; | ||
import { | ||
SyncTransformPipeline, | ||
AsyncTransformPipeline, | ||
swcPresets, | ||
} from '@react-native-esbuild/transformer'; | ||
import { getReactNativeInitializeCore } from '@react-native-esbuild/internal'; | ||
import type { TransformerConfig } from '../types'; | ||
import pkg from '../../package.json'; | ||
|
||
const DUMMY_ENTRY = ''; | ||
const DUMMY_ESBUILD_VALUE = ''; | ||
const ROOT = process.cwd(); | ||
|
||
ReactNativeEsbuildBundler.bootstrap(); | ||
|
||
export const createTransformer = (config: TransformerConfig): Transformer => { | ||
const cacheKeyFunction = getCacheKeyFunction([], [pkg.version]); | ||
const { transformer } = ReactNativeEsbuildBundler.getConfig(); | ||
const instrumentEnabled = Boolean( | ||
config.experimental?.customCoverageInstrumentation, | ||
); | ||
const swcExperimentalOptions = instrumentEnabled | ||
? config.experimental | ||
: undefined; | ||
|
||
const syncTransformPipeline = new SyncTransformPipeline.builder( | ||
ROOT, | ||
DUMMY_ENTRY, | ||
{ | ||
swc: swcPresets.getJestOptions({ | ||
module: 'cjs', | ||
experimental: swcExperimentalOptions, | ||
}), | ||
}, | ||
) | ||
.setInjectScripts([getReactNativeInitializeCore(ROOT)]) | ||
.setFullyTransformPackages(transformer?.fullyTransformPackageNames ?? []) | ||
.setStripFlowPackages(transformer?.stripFlowPackageNames ?? []) | ||
.setAdditionalBabelTransformRules( | ||
transformer?.additionalTransformRules?.babel ?? [], | ||
) | ||
.setAdditionalSwcTransformRules( | ||
transformer?.additionalTransformRules?.swc ?? [], | ||
) | ||
.build(); | ||
|
||
const asyncTransformPipeline = new AsyncTransformPipeline.builder( | ||
ROOT, | ||
DUMMY_ENTRY, | ||
{ | ||
// Async transform is always ESM. | ||
swc: swcPresets.getJestOptions({ | ||
module: 'esm', | ||
experimental: swcExperimentalOptions, | ||
}), | ||
}, | ||
) | ||
.setInjectScripts([getReactNativeInitializeCore(ROOT)]) | ||
.setFullyTransformPackages(transformer?.fullyTransformPackageNames ?? []) | ||
.setStripFlowPackages(transformer?.stripFlowPackageNames ?? []) | ||
.setAdditionalBabelTransformRules( | ||
transformer?.additionalTransformRules?.babel ?? [], | ||
) | ||
.setAdditionalSwcTransformRules( | ||
transformer?.additionalTransformRules?.swc ?? [], | ||
) | ||
.build(); | ||
|
||
return { | ||
canInstrument: instrumentEnabled, | ||
process: ( | ||
code: string, | ||
path: string, | ||
_options: TransformOptions, | ||
): TransformedSource => { | ||
const transformResult = syncTransformPipeline.transform(code, { | ||
path, | ||
pluginData: DUMMY_ESBUILD_VALUE, | ||
namespace: DUMMY_ESBUILD_VALUE, | ||
suffix: DUMMY_ESBUILD_VALUE, | ||
}); | ||
return { code: transformResult.code }; | ||
}, | ||
processAsync: async ( | ||
code: string, | ||
path: string, | ||
_options: TransformOptions, | ||
): Promise<TransformedSource> => { | ||
const transformResult = await asyncTransformPipeline.transform(code, { | ||
path, | ||
pluginData: DUMMY_ESBUILD_VALUE, | ||
namespace: DUMMY_ESBUILD_VALUE, | ||
suffix: DUMMY_ESBUILD_VALUE, | ||
}); | ||
return { code: transformResult.code }; | ||
}, | ||
getCacheKey: ( | ||
code: string, | ||
path: string, | ||
options: TransformOptions, | ||
): string => { | ||
// @ts-expect-error -- `NewGetCacheKeyFunction` | ||
return md5(cacheKeyFunction(code, path, options)); | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './createTransformer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import type { SwcJestPresetOptions } from '@react-native-esbuild/transformer'; | ||
|
||
export type TransformerConfig = Omit<SwcJestPresetOptions, 'module'>; |
Oops, something went wrong.
53b1874
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
react-native-esbuild-web – ./
react-native-esbuild-web-git-master-leegeunhyeok.vercel.app
react-native-esbuild-web-leegeunhyeok.vercel.app
rne-web-demo.vercel.app
53b1874
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report
Test suite run success
83 tests passing in 10 suites.
Report generated by 🧪jest coverage report action from 53b1874