Skip to content

Commit 6c35b19

Browse files
committed
- Improved test readablity
1 parent 85f1e8c commit 6c35b19

11 files changed

+67
-74
lines changed

test/e2e/scripts/browser.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ async function evaluate(fn, ...args) {
3131
return await page.evaluate(fn, args);
3232
}
3333

34+
async function exposeFunction(name, fn) {
35+
return await page.exposeFunction(name, fn);
36+
}
37+
3438
module.exports = {
3539
start,
3640
stop,
3741
evaluate,
42+
exposeFunction,
3843
};

test/e2e/scripts/transpile.js renamed to test/e2e/scripts/compileWithBabel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const glob = require('glob');
44
const fs = require('fs');
55
const babel = require('@babel/core');
66

7-
function transpile(dir) {
7+
function compileWithBabel(dir) {
88
glob.sync(`./test/e2e/generated/${dir}/**/*.ts`).forEach(file => {
99
try {
1010
const content = fs.readFileSync(file, 'utf8').toString();
@@ -30,4 +30,4 @@ function transpile(dir) {
3030
});
3131
}
3232

33-
module.exports = transpile;
33+
module.exports = compileWithBabel;

test/e2e/scripts/compile.js renamed to test/e2e/scripts/compileWithTypescript.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const ts = require('typescript');
44
const path = require('path');
55
const os = require('os');
66

7-
function compile(dir) {
7+
function compileWithTypescript(dir) {
88
const baseDir = `./test/e2e/generated/${dir}/`;
99
const tsconfig = {
1010
compilerOptions: {
@@ -35,4 +35,4 @@ function compile(dir) {
3535
}
3636
}
3737

38-
module.exports = compile;
38+
module.exports = compileWithTypescript;

test/e2e/v2.babel.spec.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const generate = require('./scripts/generate');
44
const copy = require('./scripts/copy');
5-
const transpile = require('./scripts/transpile');
5+
const compileWithBabel = require('./scripts/compileWithBabel');
66
const server = require('./scripts/server');
77
const browser = require('./scripts/browser');
88

@@ -11,7 +11,7 @@ describe('v2.fetch', () => {
1111
beforeAll(async () => {
1212
await generate('v2/babel', 'v2', 'fetch', true, true);
1313
await copy('v2/babel');
14-
transpile('v2/babel');
14+
compileWithBabel('v2/babel');
1515
await server.start('v2/babel');
1616
await browser.start();
1717
}, 30000);
@@ -22,25 +22,22 @@ describe('v2.fetch', () => {
2222
});
2323

2424
it('requests token', async () => {
25+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2526
const result = await browser.evaluate(async () => {
26-
window.api.OpenAPI.TOKEN = new Promise(resolve => {
27-
setTimeout(() => {
28-
resolve('MY_TOKEN');
29-
}, 500);
30-
});
31-
return await window.api.SimpleService.getCallWithoutParametersAndResponse();
27+
const { OpenAPI, SimpleService } = window.api;
28+
OpenAPI.TOKEN = window.tokenRequest;
29+
return await SimpleService.getCallWithoutParametersAndResponse();
3230
});
3331
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3432
});
3533

3634
it('complexService', async () => {
3735
const result = await browser.evaluate(async () => {
38-
return await window.api.ComplexService.complexTypes({
39-
parameterObject: {
40-
first: {
41-
second: {
42-
third: 'Hello World!'
43-
}
36+
const { ComplexService } = window.api;
37+
return await ComplexService.complexTypes({
38+
first: {
39+
second: {
40+
third: 'Hello World!'
4441
}
4542
}
4643
});

test/e2e/v2.fetch.spec.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const generate = require('./scripts/generate');
44
const copy = require('./scripts/copy');
5-
const compile = require('./scripts/compile');
5+
const compileWithTypescript = require('./scripts/compileWithTypescript');
66
const server = require('./scripts/server');
77
const browser = require('./scripts/browser');
88

@@ -11,7 +11,7 @@ describe('v2.fetch', () => {
1111
beforeAll(async () => {
1212
await generate('v2/fetch', 'v2', 'fetch');
1313
await copy('v2/fetch');
14-
compile('v2/fetch');
14+
compileWithTypescript('v2/fetch');
1515
await server.start('v2/fetch');
1616
await browser.start();
1717
}, 30000);
@@ -22,20 +22,19 @@ describe('v2.fetch', () => {
2222
});
2323

2424
it('requests token', async () => {
25+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2526
const result = await browser.evaluate(async () => {
26-
window.api.OpenAPI.TOKEN = new Promise(resolve => {
27-
setTimeout(() => {
28-
resolve('MY_TOKEN');
29-
}, 500);
30-
});
31-
return await window.api.SimpleService.getCallWithoutParametersAndResponse();
27+
const { OpenAPI, SimpleService } = window.api;
28+
OpenAPI.TOKEN = window.tokenRequest;
29+
return await SimpleService.getCallWithoutParametersAndResponse();
3230
});
3331
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3432
});
3533

3634
it('complexService', async () => {
3735
const result = await browser.evaluate(async () => {
38-
return await window.api.ComplexService.complexTypes({
36+
const { ComplexService } = window.api;
37+
return await ComplexService.complexTypes({
3938
first: {
4039
second: {
4140
third: 'Hello World!'

test/e2e/v2.node.spec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
'use strict';
22

33
const generate = require('./scripts/generate');
4-
const copy = require('./scripts/copy');
5-
const compile = require('./scripts/compile');
4+
const compileWithTypescript = require('./scripts/compileWithTypescript');
65
const server = require('./scripts/server');
76

87
describe('v2.node', () => {
98

109
beforeAll(async () => {
1110
await generate('v2/node', 'v2', 'node');
12-
compile('v2/node');
11+
compileWithTypescript('v2/node');
1312
await server.start('v2/node');
1413
}, 30000);
1514

test/e2e/v2.xhr.spec.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const generate = require('./scripts/generate');
44
const copy = require('./scripts/copy');
5-
const compile = require('./scripts/compile');
5+
const compileWithTypescript = require('./scripts/compileWithTypescript');
66
const server = require('./scripts/server');
77
const browser = require('./scripts/browser');
88

@@ -11,7 +11,7 @@ describe('v2.xhr', () => {
1111
beforeAll(async () => {
1212
await generate('v2/xhr', 'v2', 'xhr');
1313
await copy('v2/xhr');
14-
compile('v2/xhr');
14+
compileWithTypescript('v2/xhr');
1515
await server.start('v2/xhr');
1616
await browser.start();
1717
}, 30000);
@@ -22,20 +22,19 @@ describe('v2.xhr', () => {
2222
});
2323

2424
it('requests token', async () => {
25+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2526
const result = await browser.evaluate(async () => {
26-
window.api.OpenAPI.TOKEN = new Promise(resolve => {
27-
setTimeout(() => {
28-
resolve('MY_TOKEN');
29-
}, 500);
30-
});
31-
return await window.api.SimpleService.getCallWithoutParametersAndResponse();
27+
const { OpenAPI, SimpleService } = window.api;
28+
OpenAPI.TOKEN = window.tokenRequest;
29+
return await SimpleService.getCallWithoutParametersAndResponse();
3230
});
3331
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3432
});
3533

3634
it('complexService', async () => {
3735
const result = await browser.evaluate(async () => {
38-
return await window.api.ComplexService.complexTypes({
36+
const { ComplexService } = window.api;
37+
return await ComplexService.complexTypes({
3938
first: {
4039
second: {
4140
third: 'Hello World!'

test/e2e/v3.babel.spec.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const generate = require('./scripts/generate');
44
const copy = require('./scripts/copy');
5-
const transpile = require('./scripts/transpile');
5+
const compileWithBabel = require('./scripts/compileWithBabel');
66
const server = require('./scripts/server');
77
const browser = require('./scripts/browser');
88

@@ -11,7 +11,7 @@ describe('v3.fetch', () => {
1111
beforeAll(async () => {
1212
await generate('v3/babel', 'v3', 'fetch', true, true);
1313
await copy('v3/babel');
14-
transpile('v3/babel');
14+
compileWithBabel('v3/babel');
1515
await server.start('v3/babel');
1616
await browser.start();
1717
}, 30000);
@@ -22,25 +22,22 @@ describe('v3.fetch', () => {
2222
});
2323

2424
it('requests token', async () => {
25+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2526
const result = await browser.evaluate(async () => {
26-
window.api.OpenAPI.TOKEN = new Promise(resolve => {
27-
setTimeout(() => {
28-
resolve('MY_TOKEN');
29-
}, 500);
30-
});
31-
return await window.api.SimpleService.getCallWithoutParametersAndResponse();
27+
const { OpenAPI, SimpleService } = window.api;
28+
OpenAPI.TOKEN = window.tokenRequest;
29+
return await SimpleService.getCallWithoutParametersAndResponse();
3230
});
3331
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3432
});
3533

3634
it('complexService', async () => {
3735
const result = await browser.evaluate(async () => {
38-
return await window.api.ComplexService.complexTypes({
39-
parameterObject: {
40-
first: {
41-
second: {
42-
third: 'Hello World!'
43-
}
36+
const { ComplexService } = window.api;
37+
return await ComplexService.complexTypes({
38+
first: {
39+
second: {
40+
third: 'Hello World!'
4441
}
4542
}
4643
});

test/e2e/v3.fetch.spec.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const generate = require('./scripts/generate');
44
const copy = require('./scripts/copy');
5-
const compile = require('./scripts/compile');
5+
const compileWithTypescript = require('./scripts/compileWithTypescript');
66
const server = require('./scripts/server');
77
const browser = require('./scripts/browser');
88

@@ -11,7 +11,7 @@ describe('v3.fetch', () => {
1111
beforeAll(async () => {
1212
await generate('v3/fetch', 'v3', 'fetch');
1313
await copy('v3/fetch');
14-
compile('v3/fetch');
14+
compileWithTypescript('v3/fetch');
1515
await server.start('v3/fetch');
1616
await browser.start();
1717
}, 30000);
@@ -22,20 +22,19 @@ describe('v3.fetch', () => {
2222
});
2323

2424
it('requests token', async () => {
25+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2526
const result = await browser.evaluate(async () => {
26-
window.api.OpenAPI.TOKEN = new Promise(resolve => {
27-
setTimeout(() => {
28-
resolve('MY_TOKEN');
29-
}, 500);
30-
});
31-
return await window.api.SimpleService.getCallWithoutParametersAndResponse();
27+
const { OpenAPI, SimpleService } = window.api;
28+
OpenAPI.TOKEN = window.tokenRequest;
29+
return await SimpleService.getCallWithoutParametersAndResponse();
3230
});
3331
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3432
});
3533

3634
it('complexService', async () => {
3735
const result = await browser.evaluate(async () => {
38-
return await window.api.ComplexService.complexTypes({
36+
const { ComplexService } = window.api;
37+
return await ComplexService.complexTypes({
3938
first: {
4039
second: {
4140
third: 'Hello World!'

test/e2e/v3.node.spec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
'use strict';
22

33
const generate = require('./scripts/generate');
4-
const copy = require('./scripts/copy');
5-
const compile = require('./scripts/compile');
4+
const compileWithTypescript = require('./scripts/compileWithTypescript');
65
const server = require('./scripts/server');
76

87
describe('v3.node', () => {
98

109
beforeAll(async () => {
1110
await generate('v3/node', 'v3', 'node');
12-
compile('v3/node');
11+
compileWithTypescript('v3/node');
1312
await server.start('v3/node');
1413
}, 30000);
1514

0 commit comments

Comments
 (0)