- Added jest projects

- Added e2e scripts
This commit is contained in:
Ferdi Koomen 2020-09-25 12:00:58 +02:00
parent 1785e3ed90
commit 9985c45e61
12 changed files with 174 additions and 23 deletions

View File

@ -33,7 +33,7 @@ if (OpenAPI) {
exportCore: JSON.parse(program.exportCore) === true,
exportServices: JSON.parse(program.exportServices) === true,
exportModels: JSON.parse(program.exportModels) === true,
exportSchemas: JSON.parse(program.exportSchemas) === true
exportSchemas: JSON.parse(program.exportSchemas) === true,
})
.then(() => {
process.exit(0);

View File

@ -5,7 +5,7 @@
interface Config {
BASE: string;
VERSION: string;
CLIENT: 'fetch' | 'xhr';
CLIENT: 'fetch' | 'xhr' | 'node';
WITH_CREDENTIALS: boolean;
TOKEN: string;
}

View File

@ -54,7 +54,7 @@ exports[`v2 should generate: ./test/generated/v2/core/OpenAPI.ts 1`] = `
interface Config {
BASE: string;
VERSION: string;
CLIENT: 'fetch' | 'xhr';
CLIENT: 'fetch' | 'xhr' | 'node';
WITH_CREDENTIALS: boolean;
TOKEN: string;
}
@ -2505,7 +2505,7 @@ exports[`v3 should generate: ./test/generated/v3/core/OpenAPI.ts 1`] = `
interface Config {
BASE: string;
VERSION: string;
CLIENT: 'fetch' | 'xhr';
CLIENT: 'fetch' | 'xhr' | 'node';
WITH_CREDENTIALS: boolean;
TOKEN: string;
}

View File

@ -1,7 +1,11 @@
'use strict';
const generate = require('./scripts/generate');
const copy = require('./scripts/copy');
const compile = require('./scripts/compile');
const build = require('./scripts/build');
const server = require('./scripts/server');
const browser = require('./scripts/browser');
describe('e2e', () => {
@ -12,10 +16,28 @@ describe('e2e', () => {
await generate('v3', 'fetch');
await generate('v3', 'xhr');
await generate('v3', 'node');
});
await copy('v2', 'fetch');
await copy('v2', 'xhr');
await copy('v2', 'node');
await copy('v3', 'fetch');
await copy('v3', 'xhr');
await copy('v3', 'node');
await build('v2', 'fetch');
await build('v2', 'xhr');
await build('v2', 'node');
await build('v3', 'fetch');
await build('v3', 'xhr');
await build('v3', 'node');
await server.start();
await browser.start();
}, 30000);
afterAll(async () => {
//
await server.stop();
await browser.stop();
});
it('runs in chrome', async () => {
@ -23,7 +45,13 @@ describe('e2e', () => {
});
it('runs in node', async () => {
// const child1 = require('./generated/v2/fetch/index.js');
// const child2 = require('./generated/v3/fetch/index.js');
// const resultDefaultsService1 = child1.testDefaultsService();
// const resultDefaultsService2 = child2.testDefaultsService();
// expect(resultDefaultsService1).toContain('aap');
// expect(resultDefaultsService2).toContain('aap');
expect(true).toBeTruthy();
});
})
});

View File

@ -1,7 +1,21 @@
function compile(dir) {
'use strict';
const puppeteer = require('puppeteer');
let browser;
let page
async function start() {
browser = await puppeteer.launch();
page = await browser.newPage();
}
async function stop() {
await page.close();
await browser.close();
}
module.exports = {
compile,
start,
stop,
};

29
test/e2e/scripts/build.js Normal file
View File

@ -0,0 +1,29 @@
'use strict';
const rollup = require('rollup');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const typescript = require('rollup-plugin-typescript2');
async function build(version, client) {
const input = `./test/e2e/generated/${version}/${client}/index.ts`;
const output = `./test/e2e/generated/${version}/${client}/index.js`;
const options = {
treeshake: false,
plugins: [
typescript(),
nodeResolve(),
commonjs(),
],
input: input,
output: {
file: output,
format: 'cjs',
},
}
const bundle = await rollup.rollup(options);
await bundle.generate(options.output);
await bundle.write(options.output);
}
module.exports = build;

View File

@ -1,22 +1,32 @@
'use strict';
const path = require('path');
const ts = require('typescript');
const path = require('path');
const os = require('os');
function compile(dir) {
const config = {
function compile(version, client) {
const baseDir = `./test/e2e/generated/src/${version}/${client}`;
const tsconfig = {
compilerOptions: {
target: 'esnext',
target: 'es6',
module: 'commonjs',
moduleResolution: 'node',
},
include: ['./index.ts'],
};
const configFile = ts.parseConfigFileTextToJson('tsconfig.json', JSON.stringify(config));
const configFileResult = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.resolve(process.cwd(), dir), undefined, 'tsconfig.json');
const configFile = ts.parseConfigFileTextToJson('tsconfig.json', JSON.stringify(tsconfig));
const configFileResult = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.resolve(process.cwd(), baseDir), undefined, 'tsconfig.json');
const compilerHost = ts.createCompilerHost(configFileResult.options);
const compiler = ts.createProgram(configFileResult.fileNames, configFileResult.options, compilerHost);
compiler.emit();
const result = compiler.emit();
const diagnostics = ts.getPreEmitDiagnostics(compiler).concat(result.diagnostics);
if (diagnostics.length) {
console.log(ts.formatDiagnosticsWithColorAndContext(diagnostics, {
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
getCanonicalFileName: f => f,
getNewLine: () => os.EOL
}));
}
}
module.exports = compile;

11
test/e2e/scripts/copy.js Normal file
View File

@ -0,0 +1,11 @@
'use strict';
const fs = require('fs');
async function copy(version, client) {
return new Promise(resolve => {
fs.copyFile('./test/e2e/test/index.ts', `./test/e2e/generated/${version}/${client}/index.ts`, resolve);
});
}
module.exports = copy;

View File

@ -5,14 +5,10 @@ const OpenAPI = require('../../../dist');
async function generate(version, client) {
await OpenAPI.generate({
input: `./test/spec/${version}.json`,
output: `./test/e2e/generated/src/${version}/${client}`,
output: `./test/e2e/generated/${version}/${client}/api`,
httpClient: client,
useOptions: false,
useUnionTypes: false,
exportCore: true,
exportSchemas: true,
exportModels: true,
exportServices: true,
});
}

View File

@ -0,0 +1,9 @@
'use strict';
async function runner() {
return new Promise(resolve => {
resolve();
});
}
module.exports = runner;

View File

@ -1,7 +1,36 @@
function compile(dir) {
'use strict';
const express = require('express');
let app;
let server
async function start() {
return new Promise(resolve => {
app = express();
app.all('/api/*', (req, res) => {
res.send({
method: req.method,
protocol: req.protocol,
hostname: req.hostname,
path: req.path,
url: req.url,
query: req.query,
body: req.body,
headers: req.headers,
});
});
server = app.listen(3000, resolve);
});
}
async function stop() {
return new Promise(resolve => {
server.close(resolve);
});
}
module.exports = {
compile,
start,
stop,
};

25
test/e2e/test/index.ts Normal file
View File

@ -0,0 +1,25 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import { ComplexService, DefaultsService } from './api';
export async function testDefaultsService(): Promise<any> {
const callWithDefaultParameters = await DefaultsService.callWithDefaultParameters();
const callWithDefaultOptionalParameters = await DefaultsService.callWithDefaultOptionalParameters();
return {
callWithDefaultParameters,
callWithDefaultOptionalParameters,
};
}
export async function testComplexService(): Promise<any> {
const complexTypes = await ComplexService.complexTypes(
{},
{
prop: 'Hello World!',
}
);
return {
complexTypes,
};
}