Skip to content

Commit 9985c45

Browse files
committed
- Added jest projects
- Added e2e scripts
1 parent 1785e3e commit 9985c45

File tree

12 files changed

+174
-23
lines changed

12 files changed

+174
-23
lines changed

bin/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ if (OpenAPI) {
3333
exportCore: JSON.parse(program.exportCore) === true,
3434
exportServices: JSON.parse(program.exportServices) === true,
3535
exportModels: JSON.parse(program.exportModels) === true,
36-
exportSchemas: JSON.parse(program.exportSchemas) === true
36+
exportSchemas: JSON.parse(program.exportSchemas) === true,
3737
})
3838
.then(() => {
3939
process.exit(0);

src/templates/core/OpenAPI.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
interface Config {
66
BASE: string;
77
VERSION: string;
8-
CLIENT: 'fetch' | 'xhr';
8+
CLIENT: 'fetch' | 'xhr' | 'node';
99
WITH_CREDENTIALS: boolean;
1010
TOKEN: string;
1111
}

test/__snapshots__/index.spec.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ exports[`v2 should generate: ./test/generated/v2/core/OpenAPI.ts 1`] = `
5454
interface Config {
5555
BASE: string;
5656
VERSION: string;
57-
CLIENT: 'fetch' | 'xhr';
57+
CLIENT: 'fetch' | 'xhr' | 'node';
5858
WITH_CREDENTIALS: boolean;
5959
TOKEN: string;
6060
}
@@ -2505,7 +2505,7 @@ exports[`v3 should generate: ./test/generated/v3/core/OpenAPI.ts 1`] = `
25052505
interface Config {
25062506
BASE: string;
25072507
VERSION: string;
2508-
CLIENT: 'fetch' | 'xhr';
2508+
CLIENT: 'fetch' | 'xhr' | 'node';
25092509
WITH_CREDENTIALS: boolean;
25102510
TOKEN: string;
25112511
}

test/e2e/index.js

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

33
const generate = require('./scripts/generate');
4+
const copy = require('./scripts/copy');
45
const compile = require('./scripts/compile');
6+
const build = require('./scripts/build');
7+
const server = require('./scripts/server');
8+
const browser = require('./scripts/browser');
59

610
describe('e2e', () => {
711

@@ -12,18 +16,42 @@ describe('e2e', () => {
1216
await generate('v3', 'fetch');
1317
await generate('v3', 'xhr');
1418
await generate('v3', 'node');
15-
});
19+
20+
await copy('v2', 'fetch');
21+
await copy('v2', 'xhr');
22+
await copy('v2', 'node');
23+
await copy('v3', 'fetch');
24+
await copy('v3', 'xhr');
25+
await copy('v3', 'node');
26+
27+
await build('v2', 'fetch');
28+
await build('v2', 'xhr');
29+
await build('v2', 'node');
30+
await build('v3', 'fetch');
31+
await build('v3', 'xhr');
32+
await build('v3', 'node');
33+
34+
await server.start();
35+
await browser.start();
36+
}, 30000);
1637

1738
afterAll(async () => {
18-
//
39+
await server.stop();
40+
await browser.stop();
1941
});
2042

2143
it('runs in chrome', async () => {
2244
expect(true).toBeTruthy();
2345
});
2446

2547
it('runs in node', async () => {
48+
// const child1 = require('./generated/v2/fetch/index.js');
49+
// const child2 = require('./generated/v3/fetch/index.js');
50+
// const resultDefaultsService1 = child1.testDefaultsService();
51+
// const resultDefaultsService2 = child2.testDefaultsService();
52+
// expect(resultDefaultsService1).toContain('aap');
53+
// expect(resultDefaultsService2).toContain('aap');
2654
expect(true).toBeTruthy();
2755
});
2856

29-
})
57+
});

test/e2e/scripts/browser.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1-
function compile(dir) {
1+
'use strict';
22

3+
const puppeteer = require('puppeteer');
4+
5+
let browser;
6+
let page
7+
8+
async function start() {
9+
browser = await puppeteer.launch();
10+
page = await browser.newPage();
11+
}
12+
13+
async function stop() {
14+
await page.close();
15+
await browser.close();
316
}
417

518
module.exports = {
6-
compile,
19+
start,
20+
stop,
721
};

test/e2e/scripts/build.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
const rollup = require('rollup');
4+
const { nodeResolve } = require('@rollup/plugin-node-resolve');
5+
const commonjs = require('@rollup/plugin-commonjs');
6+
const typescript = require('rollup-plugin-typescript2');
7+
8+
async function build(version, client) {
9+
const input = `./test/e2e/generated/${version}/${client}/index.ts`;
10+
const output = `./test/e2e/generated/${version}/${client}/index.js`;
11+
const options = {
12+
treeshake: false,
13+
plugins: [
14+
typescript(),
15+
nodeResolve(),
16+
commonjs(),
17+
],
18+
input: input,
19+
output: {
20+
file: output,
21+
format: 'cjs',
22+
},
23+
}
24+
const bundle = await rollup.rollup(options);
25+
await bundle.generate(options.output);
26+
await bundle.write(options.output);
27+
}
28+
29+
module.exports = build;

test/e2e/scripts/compile.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
'use strict';
22

3-
const path = require('path');
43
const ts = require('typescript');
4+
const path = require('path');
5+
const os = require('os');
56

6-
function compile(dir) {
7-
const config = {
7+
function compile(version, client) {
8+
const baseDir = `./test/e2e/generated/src/${version}/${client}`;
9+
const tsconfig = {
810
compilerOptions: {
9-
target: 'esnext',
11+
target: 'es6',
1012
module: 'commonjs',
1113
moduleResolution: 'node',
1214
},
1315
include: ['./index.ts'],
1416
};
15-
const configFile = ts.parseConfigFileTextToJson('tsconfig.json', JSON.stringify(config));
16-
const configFileResult = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.resolve(process.cwd(), dir), undefined, 'tsconfig.json');
17+
const configFile = ts.parseConfigFileTextToJson('tsconfig.json', JSON.stringify(tsconfig));
18+
const configFileResult = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.resolve(process.cwd(), baseDir), undefined, 'tsconfig.json');
1719
const compilerHost = ts.createCompilerHost(configFileResult.options);
1820
const compiler = ts.createProgram(configFileResult.fileNames, configFileResult.options, compilerHost);
19-
compiler.emit();
21+
const result = compiler.emit();
22+
const diagnostics = ts.getPreEmitDiagnostics(compiler).concat(result.diagnostics);
23+
if (diagnostics.length) {
24+
console.log(ts.formatDiagnosticsWithColorAndContext(diagnostics, {
25+
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
26+
getCanonicalFileName: f => f,
27+
getNewLine: () => os.EOL
28+
}));
29+
}
2030
}
2131

2232
module.exports = compile;

test/e2e/scripts/copy.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
5+
async function copy(version, client) {
6+
return new Promise(resolve => {
7+
fs.copyFile('./test/e2e/test/index.ts', `./test/e2e/generated/${version}/${client}/index.ts`, resolve);
8+
});
9+
}
10+
11+
module.exports = copy;

test/e2e/scripts/generate.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ const OpenAPI = require('../../../dist');
55
async function generate(version, client) {
66
await OpenAPI.generate({
77
input: `./test/spec/${version}.json`,
8-
output: `./test/e2e/generated/src/${version}/${client}`,
8+
output: `./test/e2e/generated/${version}/${client}/api`,
99
httpClient: client,
1010
useOptions: false,
1111
useUnionTypes: false,
12-
exportCore: true,
13-
exportSchemas: true,
14-
exportModels: true,
15-
exportServices: true,
1612
});
1713
}
1814

test/e2e/scripts/runner.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
async function runner() {
4+
return new Promise(resolve => {
5+
resolve();
6+
});
7+
}
8+
9+
module.exports = runner;

0 commit comments

Comments
 (0)