Skip to content

Commit 45fdaf6

Browse files
committed
feat: share test cases between jest and e2e
1 parent d318776 commit 45fdaf6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+13460
-1361
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ umd
55
tslib
66
node_modules
77
npm-debug.log
8-
test-browser/fixtures/es-modern-bundle.js
9-
test-browser/fixtures/es-legacy-bundle.js
8+
e2e/fixtures/scripts
9+
e2e/test-results
1010
coverage/
11-
test-results
1211
run
1312
.DS_Store
1413
.cache

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
### Features
1212

13-
- 🔥 add `Enum.install` method which introduces a new plugin system. The plugin system allows you to add new methods or properties to all enums. While similar to `Enum.extends`, the plugin system enables features to be packaged as `shareable` plugins, making it more flexible and widely applicable.
13+
- 🔥 Introduces the new plugin system. Plugins are registered using `Enum.install`. The plugin system allows you to add new methods or properties to all enums. While similar to `Enum.extends`, the plugin system enables features to be packaged as `shareable` plugins, making it more flexible and widely applicable.
1414
- 🔥 introduce an official plugin `enum-plus-plugin` that provides a collection of highly shareable plugins.
1515
- These plugins are designed to work in all scenarios and environments (including browser and Node.js), without introducing dependencies on third-party frameworks, component libraries, or platforms.
1616
- If you have a good idea that might be specific to a certain framework or platform rather than universally applicable, we recommend creating a new plugin project (e.g., `enum-plus-plugin-xxx`). We are happy to see that, and hope that you submit a PR to `enum-plus-plugin` to link your project back.

Testing.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1. jest和playwright复用一套代码
2+
2. 所以在单元测试编写时还是有点区别
3+
1. test分为evaluate和assert两个部分
4+
2. 两个方法的入参和返回值都需要可序列化
5+
3. 尽量不使用闭包,如果必须要使用的话,请添加到evaluate的返回值中
6+
4. 用到的外部数据(例如enum-plus、config、data)等,需要考虑到浏览器端,需要编译到e2e目录中

browser-global.d.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

e2e-global.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type * as EnumPlus from '@enum-plus';
2+
import type * as WeekConfig from './test/data/week-config';
3+
import type * as WeekData from './test/data/week-data';
4+
import type * as SerializeJavascript from './test/utils/serialize-javascript';
5+
6+
declare global {
7+
interface Window {
8+
EnumPlus: typeof EnumPlus;
9+
WeekConfig: typeof WeekConfig;
10+
WeekData: typeof WeekData;
11+
SerializeJavascript: typeof SerializeJavascript;
12+
}
13+
}
14+
export {};

e2e/fixtures/EnumLegacyTest.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { test as base } from '@playwright/test';
2+
import EnumPage from './EnumPage';
3+
4+
export { expect } from '@playwright/test';
5+
6+
export const legacyTest = base.extend<{
7+
enumPage: EnumPage;
8+
}>({
9+
enumPage: async ({ page }, use) => {
10+
const enumPage = new EnumPage(page);
11+
await enumPage.gotoLegacy();
12+
13+
await use(enumPage);
14+
},
15+
});
16+
17+
legacyTest.beforeEach(async ({ enumPage }) => {
18+
await enumPage.setupLang();
19+
});
20+
legacyTest.afterEach(async ({ enumPage }) => {
21+
await enumPage.resetLang();
22+
});

e2e/fixtures/EnumPage.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { Page } from '@playwright/test';
2+
3+
export default class EnumPage {
4+
constructor(public readonly page: Page) {}
5+
6+
async gotoModern() {
7+
await this.page.goto('/modern.html');
8+
}
9+
async gotoLegacy() {
10+
await this.page.goto('/legacy.html');
11+
}
12+
async setupLang() {
13+
await this.page.evaluate(() => {
14+
const {
15+
EnumPlus: { Enum, defaultLocalize },
16+
WeekConfig: { setLang, getLocales },
17+
} = window;
18+
19+
setLang('en-US', Enum, getLocales, defaultLocalize);
20+
});
21+
}
22+
async resetLang() {
23+
await this.page.evaluate(() => {
24+
const {
25+
EnumPlus: { Enum },
26+
WeekConfig: { clearLang },
27+
} = window;
28+
clearLang(Enum);
29+
});
30+
}
31+
}

e2e/fixtures/EnumTest.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { test as base } from '@playwright/test';
2+
import EnumPage from './EnumPage';
3+
4+
export { expect } from '@playwright/test';
5+
6+
export const test = base.extend<{
7+
enumPage: EnumPage;
8+
}>({
9+
enumPage: async ({ page }, use) => {
10+
const enumPage = new EnumPage(page);
11+
await enumPage.gotoModern();
12+
13+
await use(enumPage);
14+
},
15+
});
16+
17+
test.beforeEach(async ({ enumPage }) => {
18+
await enumPage.setupLang();
19+
});
20+
test.afterEach(async ({ enumPage }) => {
21+
await enumPage.resetLang();
22+
});

e2e/fixtures/legacy.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Enum Plus Legacy Test</title>
7+
<script src="scripts/serialize-javascript-bundle.js"></script>
8+
<script src="scripts/es-legacy-bundle.js"></script>
9+
<script src="scripts/week-config-bundle.js"></script>
10+
<script src="scripts/week-data-bundle.js"></script>
11+
</head>
12+
13+
<body>
14+
<div id="test-results"></div>
15+
</body>
16+
17+
</html>

e2e/fixtures/modern.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Enum Plus Modern Test</title>
7+
<script src="scripts/serialize-javascript-bundle.js"></script>
8+
<script src="scripts/es-modern-bundle.js"></script>
9+
<script src="scripts/week-config-bundle.js"></script>
10+
<script src="scripts/week-data-bundle.js"></script>
11+
</head>
12+
13+
<body>
14+
<div id="test-results"></div>
15+
</body>
16+
17+
</html>

0 commit comments

Comments
 (0)