diff --git a/bin/index.js b/bin/index.js index 6fee0a85..99605b78 100755 --- a/bin/index.js +++ b/bin/index.js @@ -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); diff --git a/src/templates/core/OpenAPI.hbs b/src/templates/core/OpenAPI.hbs index a2dbfef5..f24b4756 100644 --- a/src/templates/core/OpenAPI.hbs +++ b/src/templates/core/OpenAPI.hbs @@ -5,7 +5,7 @@ interface Config { BASE: string; VERSION: string; - CLIENT: 'fetch' | 'xhr'; + CLIENT: 'fetch' | 'xhr' | 'node'; WITH_CREDENTIALS: boolean; TOKEN: string; } diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index 2c2d8d2b..18d2a2bd 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -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; } diff --git a/test/e2e/index.js b/test/e2e/index.js index 85906341..1739e310 100644 --- a/test/e2e/index.js +++ b/test/e2e/index.js @@ -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(); }); -}) +}); diff --git a/test/e2e/scripts/browser.js b/test/e2e/scripts/browser.js index bf955522..1a51a2a2 100644 --- a/test/e2e/scripts/browser.js +++ b/test/e2e/scripts/browser.js @@ -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, }; diff --git a/test/e2e/scripts/build.js b/test/e2e/scripts/build.js new file mode 100644 index 00000000..1c1371fb --- /dev/null +++ b/test/e2e/scripts/build.js @@ -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; diff --git a/test/e2e/scripts/compile.js b/test/e2e/scripts/compile.js index 185cbfd7..94d6386e 100644 --- a/test/e2e/scripts/compile.js +++ b/test/e2e/scripts/compile.js @@ -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; diff --git a/test/e2e/scripts/copy.js b/test/e2e/scripts/copy.js new file mode 100644 index 00000000..51fe6472 --- /dev/null +++ b/test/e2e/scripts/copy.js @@ -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; diff --git a/test/e2e/scripts/generate.js b/test/e2e/scripts/generate.js index c789e5fd..933739de 100644 --- a/test/e2e/scripts/generate.js +++ b/test/e2e/scripts/generate.js @@ -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, }); } diff --git a/test/e2e/scripts/runner.js b/test/e2e/scripts/runner.js new file mode 100644 index 00000000..2ffd5ac6 --- /dev/null +++ b/test/e2e/scripts/runner.js @@ -0,0 +1,9 @@ +'use strict'; + +async function runner() { + return new Promise(resolve => { + resolve(); + }); +} + +module.exports = runner; diff --git a/test/e2e/scripts/server.js b/test/e2e/scripts/server.js index bf955522..c20d1b81 100644 --- a/test/e2e/scripts/server.js +++ b/test/e2e/scripts/server.js @@ -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, }; diff --git a/test/e2e/test/index.ts b/test/e2e/test/index.ts new file mode 100644 index 00000000..00811a71 --- /dev/null +++ b/test/e2e/test/index.ts @@ -0,0 +1,25 @@ +/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +import { ComplexService, DefaultsService } from './api'; + +export async function testDefaultsService(): Promise { + const callWithDefaultParameters = await DefaultsService.callWithDefaultParameters(); + const callWithDefaultOptionalParameters = await DefaultsService.callWithDefaultOptionalParameters(); + return { + callWithDefaultParameters, + callWithDefaultOptionalParameters, + }; +} + +export async function testComplexService(): Promise { + const complexTypes = await ComplexService.complexTypes( + {}, + { + prop: 'Hello World!', + } + ); + return { + complexTypes, + }; +}