Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcb2515e

Browse files
committed
feat(fronts-test): refactor testing builder
1 parente5582e9 commitcb2515e

File tree

3 files changed

+177
-18
lines changed

3 files changed

+177
-18
lines changed

‎README.md‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,21 @@ console.log(getMeta());
195195
`fronts-test` provides an runner for function step, and any micro frontends IT and E2E can use it for reusable testing. It also provides other APIs, such as`useContext()`, beforeHook and afterHook in`createRunner()`.
196196

197197
```ts
198-
import {run,useContext }from'fronts-test';
198+
import {$,useContext,run,When,Then }from'fronts-test';
199199

200-
const addTodo= (todoText)=> {
200+
const addTodo= ()=> {
201201
const { page }=useContext();
202-
awaitpage.type('.text',todoText);
202+
awaitpage.type('.text','Use Fronts');
203203
awaitpage.click('.add');
204204
};
205205

206-
const entry= ()=> {
207-
awaitrun(addTodo,'Use Fronts');
208-
};
206+
test('base',async ()=> {
207+
awaitrun(
208+
Given('user open the page',entry),
209+
When('user add todo text',addTodo),
210+
Then('user should see that todo list has a new item',checkTodo),
211+
);
212+
});
209213
```
210214

211215
##CLI

‎packages/fronts-test/src/runner.ts‎

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
exportconstcreateRunner=({
1+
typeStep=(...args:any)=>any;
2+
3+
constcreateRunner=({
24
before,
35
after,
46
}:{
@@ -9,7 +11,7 @@ export const createRunner = ({
911
result:any
1012
)=>Promise<void>;
1113
}={})=>{
12-
returnasync<Textends(...args:any)=>any>(
14+
const$=async<Textends(...args:any)=>any>(
1315
func:T,
1416
...args:Parameters<T>
1517
):Promise<ReturnType<T>>=>{
@@ -27,6 +29,58 @@ export const createRunner = ({
2729
}
2830
returnresult;
2931
};
32+
33+
constGiven=(desc:string, ...steps:Step[])=>{
34+
constGiven=async(desc:string, ...steps:Step[])=>{
35+
for(conststepofsteps){
36+
await$(step);
37+
}
38+
};
39+
Given.args=[desc, ...steps];
40+
returnGiven;
41+
};
42+
constWhen=(desc:string, ...steps:Step[])=>{
43+
constWhen=async(desc:string, ...steps:Step[])=>{
44+
for(conststepofsteps){
45+
await$(step);
46+
}
47+
};
48+
When.args=[desc, ...steps];
49+
returnWhen;
50+
};
51+
constThen=(desc:string, ...steps:Step[])=>{
52+
constThen=async(desc:string, ...steps:Step[])=>{
53+
for(conststepofsteps){
54+
await$(step);
55+
}
56+
};
57+
Then.args=[desc, ...steps];
58+
returnThen;
59+
};
60+
constAnd=(desc:string, ...steps:Step[])=>{
61+
constAnd=async(desc:string, ...steps:Step[])=>{
62+
for(conststepofsteps){
63+
await$(step);
64+
}
65+
};
66+
And.args=[desc, ...steps];
67+
returnAnd;
68+
};
69+
construn=async(...args:Step[])=>{
70+
for(conststepofargs){
71+
await$(step, ...(stepasany).args);
72+
}
73+
};
74+
return{
75+
run,
76+
$,
77+
Given,
78+
When,
79+
Then,
80+
And,
81+
};
3082
};
3183

32-
exportconstrun=createRunner();
84+
const{ run, $, Given, When, Then, And}=createRunner();
85+
86+
export{run,$,Given,When,Then,And,createRunner};

‎packages/fronts-test/test/index.test.ts‎

Lines changed: 110 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
import{run,useContext,createContextHook,createRunner}from'..';
1+
import{
2+
$,
3+
When,
4+
Given,
5+
Then,
6+
And,
7+
run,
8+
useContext,
9+
createContextHook,
10+
createRunner,
11+
}from'..';
212

313
test('run',async()=>{
414
constfunc=(num:number)=>num+1;
5-
constresult=awaitrun(func,1);
15+
constresult=await$(func,1);
616
expect(result).toBe(2);
717
});
818

@@ -19,9 +29,9 @@ test('useContext', async () => {
1929
constcontext=useContext<Context>();
2030
returncontext.test!+num+1;
2131
};
22-
constresult0=awaitrun(foo,1);
32+
constresult0=await$(foo,1);
2333
expect(result0).toBe(2);
24-
constresult1=awaitrun(bar,10);
34+
constresult1=await$(bar,10);
2535
expect(result1).toBe(111);
2636
});
2737

@@ -36,16 +46,16 @@ test('createContextHook', async () => {
3646
constcontext=useContext();
3747
returncontext.test!+num+1;
3848
};
39-
constresult0=awaitrun(foo,1);
49+
constresult0=await$(foo,1);
4050
expect(result0).toBe(2);
41-
constresult1=awaitrun(bar,10);
51+
constresult1=await$(bar,10);
4252
expect(result1).toBe(111);
4353
});
4454

4555
test('createRunner',async()=>{
4656
constmockBefore=jest.fn();
4757
constmockAfter=jest.fn();
48-
construn=createRunner({
58+
const{ $}=createRunner({
4959
before:mockBefore,
5060
after:mockAfter,
5161
});
@@ -59,11 +69,11 @@ test('createRunner', async () => {
5969
constcontext=useContext();
6070
returncontext.test!+num+1;
6171
};
62-
constresult0=awaitrun(foo,1);
72+
constresult0=await$(foo,1);
6373
expect(result0).toBe(2);
6474
expect(mockBefore.mock.calls).toEqual([[foo,[1]]]);
6575
expect(mockAfter.mock.calls).toEqual([[foo,[1],2]]);
66-
constresult1=awaitrun(bar,10);
76+
constresult1=await$(bar,10);
6777
expect(result1).toBe(111);
6878
expect(result1).toBe(111);
6979
expect(mockBefore.mock.calls).toEqual([
@@ -75,3 +85,94 @@ test('createRunner', async () => {
7585
[bar,[10],111],
7686
]);
7787
});
88+
89+
test('base runner',async()=>{
90+
constfn=jest.fn();
91+
// const { $, When, Given, Then, And, run } = createRunner({
92+
// before: mockBefore,
93+
// after: mockAfter,
94+
// });
95+
constuseContext=createContextHook<{test?:number}>();
96+
constfoo=(num:number)=>{
97+
fn('foo',num);
98+
constcontext=useContext();
99+
context.test=100;
100+
returnnum+1;
101+
};
102+
constbar=()=>{
103+
fn('bar');
104+
constcontext=useContext();
105+
returncontext.test!+10;
106+
};
107+
108+
constfoobar=()=>{
109+
fn('foobar');
110+
constcontext=useContext();
111+
returncontext.test!*100;
112+
};
113+
114+
awaitrun(
115+
Given('given foo',()=>$(foo,1)),
116+
When('when bar',bar),
117+
Then('then foobar',foobar),
118+
And('and fn')
119+
);
120+
121+
expect(fn.mock.calls).toEqual([['foo',1],['bar'],['foobar']]);
122+
});
123+
124+
test('createRunner',async()=>{
125+
constmockBefore=jest.fn();
126+
constmockAfter=jest.fn();
127+
const{ $, When, Given, Then, And, run}=createRunner({
128+
before:mockBefore,
129+
after:mockAfter,
130+
});
131+
constuseContext=createContextHook<{test?:number}>();
132+
constfoo=(num:number)=>{
133+
constcontext=useContext();
134+
context.test=100;
135+
returnnum+1;
136+
};
137+
constbar=()=>{
138+
constcontext=useContext();
139+
returncontext.test!+10;
140+
};
141+
142+
constfoobar=()=>{
143+
constcontext=useContext();
144+
returncontext.test!*100;
145+
};
146+
147+
awaitrun(
148+
Given('given foo',()=>$(foo,1)),
149+
When('when bar',bar),
150+
Then('then foobar',foobar),
151+
And('and fn')
152+
);
153+
154+
expect(
155+
mockBefore.mock.calls.map(([{ name},[first]])=>[name,first])
156+
).toEqual([
157+
['Given','given foo'],
158+
['',undefined],
159+
['foo',1],
160+
['When','when bar'],
161+
['bar',undefined],
162+
['Then','then foobar'],
163+
['foobar',undefined],
164+
['And','and fn'],
165+
]);
166+
expect(
167+
mockAfter.mock.calls.map((args)=>[args[0].name, ...args.slice(-1)])
168+
).toEqual([
169+
['foo',2],
170+
['',2],
171+
['Given',undefined],
172+
['bar',110],
173+
['When',undefined],
174+
['foobar',10000],
175+
['Then',undefined],
176+
['And',undefined],
177+
]);
178+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp