Skip to content

Commit

Permalink
fix(testing): jest unexpected token error for @modern-js/runtime (#4070)
Browse files Browse the repository at this point in the history
  • Loading branch information
targeral committed Jun 28, 2023
1 parent 99a4833 commit dda37bf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/orange-horses-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/plugin-testing': patch
---

fix: jest unexpected token error for @modern-js/runtime
fix: 修复 Jest 对于 @modern-js/runtime 出现 unexpected token 的错误
21 changes: 21 additions & 0 deletions packages/runtime/plugin-testing/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ export const mergeUserJestConfig = (testUtils: TestConfigOperator) => {
}
};

export const getJestTransformEsModulesRegStr = () => {
const esmModulesInPnpm = [
'@modern-js\\+runtime@',
'@modern-js\\+plugin@',
// @modern-js-reduck+store, @modern-js-reduck+effects and so on
'@modern-js-reduck',
'@babel\\+runtime@',
];
// yarn or npm
const esmModules = [
'@modern-js/runtime',
'@modern-js/plugin',
'@modern-js-reduck',
'@babel/runtime',
];
return `node_modules/(?!(\\.pnpm/(${esmModulesInPnpm.join(
'|',
)}))|(${esmModules.join('|')}))`;
};

export const testingPlugin = (): CliPlugin<{
hooks: Hooks;
userConfig: UserConfig;
Expand Down Expand Up @@ -128,6 +148,7 @@ export const testingPlugin = (): CliPlugin<{
`<rootDir>/src/**/*.test.[jt]s?(x)`,
`<rootDir>/tests/**/*.test.[jt]s?(x)`,
],
transformIgnorePatterns: [getJestTransformEsModulesRegStr()],
});

mergeUserJestConfig(utils);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getJestTransformEsModulesRegStr } from '../src/cli';

describe('Jest transform esModules RegExp String ', () => {
const r = new RegExp(getJestTransformEsModulesRegStr());
it('In pnpm project', () => {
expect(r.test('node_modules/.pnpm/@modern-js+runtime@0.0.0')).toBeFalsy();
expect(r.test('node_modules/.pnpm/@modern-js+plugin@0.0.0')).toBeFalsy();
expect(
r.test('node_modules/.pnpm/@modern-js-reduck+store@0.0.0'),
).toBeFalsy();
expect(
r.test('node_modules/.pnpm/@modern-js-reduck+plugin-effects@0.0.0'),
).toBeFalsy();
expect(r.test('node_modules/apnpm/@modern-js+runtime@0.0.0')).toBeTruthy();
expect(r.test('node_modules/.pnpm/webpack')).toBeTruthy();
});

it('In npm or yarn@1 project', () => {
expect(r.test('node_modules/@modern-js/runtime')).toBeFalsy();
expect(r.test('node_modules/@modern-js/plugin')).toBeFalsy();
expect(r.test('node_modules/@modern-js-reduck/store')).toBeFalsy();
expect(r.test('node_modules/@modern-js-reduck/plugin-effects')).toBeFalsy();
expect(r.test('node_modules/webpack')).toBeTruthy();
});
});

0 comments on commit dda37bf

Please sign in to comment.