Skip to content

Commit

Permalink
feat(test): jest support runtime plugin init (#9131)
Browse files Browse the repository at this point in the history
* chore(examples-test): πŸ”§ enable request plugin

* test(example-test): βœ… add requets test case

* chore: πŸ”§ update jest config

* refactor: 🎨 add umi runtime plugin init in jest setup

* feat: ✨ add test-setup for umi

* feat(umi-preset): ✨ generate setup using internal test-setup

* test(example-test): βœ… using internal test-setup.js

* chore: ⬆️ update npnm-lock

* fix(perset-umi): πŸ›  @@ tsc error

* chore: πŸ”₯ remove blank lines

* chore: ⬆️ update npnm-lock

Co-authored-by: pshu <pishu.spf@antfin.com>
  • Loading branch information
stormslowly and stormslowly committed Aug 31, 2022
1 parent 1debeca commit b912bbe
Show file tree
Hide file tree
Showing 10 changed files with 1,218 additions and 1,267 deletions.
3 changes: 2 additions & 1 deletion examples/test-test/.umirc.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
export default {
npmClient: 'pnpm',
plugins: ['@umijs/plugins/dist/dva'],
plugins: ['@umijs/plugins/dist/dva', '@umijs/plugins/dist/request'],
dva: {
immer: {
enableES5: true,
enableAllPlugins: true,
},
},
request: {},
};
8 changes: 6 additions & 2 deletions examples/test-test/importFromUmiSmoke.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// @ts-ignore
import { connect } from 'dva';
import { useAppData } from 'umi';

import { useAppData, request } from 'umi';
test('import from umi works', () => {
expect(useAppData).toBeDefined();
expect(connect).toBeDefined();
});

test('import request from umi works', () => {
expect(request).toBeTruthy();
request(`https://registry.npmjs.com/umi/2.0.0`);
});
1 change: 1 addition & 0 deletions examples/test-test/jest-setup.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import '@testing-library/jest-dom';
import 'umi/test-setup';
21 changes: 13 additions & 8 deletions examples/test-test/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { Config, configUmiAlias, createConfig } from 'umi/test';

const jestConfig = createConfig({
target: 'browser',
jsTransformer: 'esbuild',
jsTransformerOpts: { jsx: 'automatic' },
});

export default async () => {
return (await configUmiAlias({
...jestConfig,
...createConfig({
target: 'browser',
jsTransformer: 'esbuild',
// config opts for esbuild , it will pass to esbuild directly
jsTransformerOpts: { jsx: 'automatic' },
}),
setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'],
collectCoverageFrom: [
'**/*.{ts,tsx,js,jsx}',
'!.umi/**',
'!.umi-test/**',
'!coverage/**',
'!.umi-production/**',
'!.umirc.{js,ts}',
'!.umirc.*.{js,ts}',
'!jest.config.{js,ts}',
'!coverage/**',
'!dist/**',
'!config/**',
'!mock/**',
],
// if you require some es-module npm package, please uncomment below line and insert your package name
// transformIgnorePatterns: ['node_modules/(?!.*(lodash-es|your-es-pkg-name)/)']
coverageThreshold: {
global: {
lines: 1,
Expand Down
4 changes: 2 additions & 2 deletions examples/test-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"devDependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13",
"@types/jest": "^27.5.1",
"@types/jest": "^27",
"@types/testing-library__jest-dom": "^5.14.5",
"jest": "^27",
"ts-node": "^10",
"typescript": "^4.7.2"
"typescript": "^4"
}
}
1 change: 1 addition & 0 deletions packages/max/test-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('umi/test-setup');
24 changes: 12 additions & 12 deletions packages/preset-umi/src/commands/generators/jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default (api: IApi) => {

const hasSrc = api.paths.absSrcPath.endsWith('src');

const importSource = api.appData.umi.importSource;
const basicDeps = {
jest: '^27',
'@types/jest': '^27',
Expand All @@ -53,16 +54,15 @@ export default (api: IApi) => {
h.addDevDeps(packageToInstall);
h.addScript('test', 'jest');

if (res.willUseTLR) {
writeFileSync(
join(api.cwd, 'jest-setup.ts'),
`import '@testing-library/jest-dom';
`.trimLeft(),
);
logger.info('Write jest-setup.ts');
}
const setupImports = res.willUseTLR
? [
`import '@testing-library/jest-dom';`,
`import '${api.appData.umi.importSource}/test-setup'`,
]
: [`import '${api.appData.umi.importSource}/test-setup'`];

const importSource = api.appData.umi.importSource;
writeFileSync(join(api.cwd, 'jest-setup.ts'), setupImports.join('\n'));
logger.info('Write jest-setup.ts');

const collectCoverageFrom = hasSrc
? [
Expand Down Expand Up @@ -99,9 +99,9 @@ export default async () => {
jsTransformerOpts: { jsx: 'automatic' },
}),
${res.willUseTLR ? `setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'],` : ''}
collectCoverageFrom: [${collectCoverageFrom
.map((v) => `'${v}'`)
.join(', ')}],
collectCoverageFrom: [
${collectCoverageFrom.map((v) => ` '${v}'`).join(',\n')}
],
// if you require some es-module npm package, please uncomment below line and insert your package name
// transformIgnorePatterns: ['node_modules/(?!.*(lodash-es|your-es-pkg-name)/)']
})) as Config.InitialOptions;
Expand Down
4 changes: 4 additions & 0 deletions packages/umi/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @ts-ignore
import { createPluginManager } from '@@/core/plugin';

createPluginManager();
1 change: 1 addition & 0 deletions packages/umi/test-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./dist/test-setup');
Loading

0 comments on commit b912bbe

Please sign in to comment.