Skip to content

Commit

Permalink
fix(plugin-testing): failed to run tests with decorators (#4840)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Oct 24, 2023
1 parent 0b9e483 commit 6e8cb66
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .changeset/dirty-apes-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@modern-js/builder-webpack-provider': patch
'@modern-js/plugin-testing': patch
---

fix(plugin-testing): failed to run tests with decorators

fix(plugin-testing): 修复无法运行带有 decorator 的测试用例的问题
16 changes: 10 additions & 6 deletions packages/builder/builder-webpack-provider/src/plugins/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,23 @@ export const builderPluginBabel = (): BuilderPlugin => ({
},
};

const decoratorConfig = {
version: config.output.enableLatestDecorators
? '2018-09'
: 'legacy',
} as const;

const baseBabelConfig =
isServer || isServiceWorker
? getBabelConfigForNode()
? getBabelConfigForNode({
pluginDecorators: decoratorConfig,
})
: getBabelConfigForWeb({
presetEnv: {
targets: browserslist,
useBuiltIns: getUseBuiltIns(config),
},
pluginDecorators: {
version: config.output.enableLatestDecorators
? '2018-09'
: 'legacy',
},
pluginDecorators: decoratorConfig,
});

applyPluginImport(baseBabelConfig, config.source.transformImport);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ exports[`plugins/babel > should adjust browserslist when target is node 1`] = `
"compact": false,
"configFile": false,
"plugins": [
[
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-decorators/lib/index.js",
{
"version": "legacy",
},
],
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-export-default-from/lib/index.js",
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-partial-application/lib/index.js",
[
Expand Down Expand Up @@ -278,6 +284,12 @@ exports[`plugins/babel > should adjust browserslist when target is node 1`] = `
"compact": false,
"configFile": false,
"plugins": [
[
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-decorators/lib/index.js",
{
"version": "legacy",
},
],
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-export-default-from/lib/index.js",
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-partial-application/lib/index.js",
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2149,6 +2149,12 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"compact": true,
"configFile": false,
"plugins": [
[
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-decorators/lib/index.js",
{
"version": "legacy",
},
],
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-export-default-from/lib/index.js",
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-partial-application/lib/index.js",
[
Expand Down Expand Up @@ -2254,6 +2260,12 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"compact": true,
"configFile": false,
"plugins": [
[
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-decorators/lib/index.js",
{
"version": "legacy",
},
],
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-export-default-from/lib/index.js",
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-partial-application/lib/index.js",
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import babelJest from 'babel-jest';

const babelTransformer = (babelJest.createTransformer as any)?.({
presets: [
require.resolve('@rsbuild/babel-preset/node'),
[
require.resolve('@rsbuild/babel-preset/node'),
{
pluginDecorators: {
version: 'legacy',
},
},
],
require.resolve('@babel/preset-react'),
],
configFile: false,
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/integration/testing-plugin/modern.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { testingPlugin } from '@modern-js/plugin-testing';
import { applyBaseConfig } from '../../utils/applyBaseConfig';

export default applyBaseConfig({
plugins: [testingPlugin()],
});
14 changes: 14 additions & 0 deletions tests/integration/testing-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"private": true,
"name": "@e2e/testing-plugin",
"version": "1.0.0",
"scripts": {
"test": "modern test"
},
"devDependencies": {
"@modern-js/app-tools": "workspace:*",
"@modern-js/plugin-testing": "workspace:*",
"@types/jest": "^29",
"@types/node": "^14"
}
}
10 changes: 10 additions & 0 deletions tests/integration/testing-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function seal(constructor: unknown) {
Object.seal(constructor);
}

@seal
export class DummyClass {
public method() {
// Method implementation
}
}
7 changes: 7 additions & 0 deletions tests/integration/testing-plugin/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { DummyClass } from '../src';

describe('basic test', () => {
test('should compile legacy decorator correctly', () => {
expect(new DummyClass()).toEqual({});
});
});
12 changes: 12 additions & 0 deletions tests/integration/testing-plugin/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "@modern-js/tsconfig/base",
"compilerOptions": {
"declaration": true,
"jsx": "preserve",
"baseUrl": "./",
"emitDeclarationOnly": true,
"isolatedModules": true,
"paths": {},
"types": ["node", "jest"]
}
}
15 changes: 15 additions & 0 deletions tests/integration/testing-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "@modern-js/tsconfig/base",
"compilerOptions": {
"declaration": false,
"jsx": "preserve",
"baseUrl": "./",
"paths": {
"@/*": ["./src/*"]
},
"target": "ES5",
"experimentalDecorators": true,
"types": ["jest"]
},
"include": ["src"]
}

0 comments on commit 6e8cb66

Please sign in to comment.