mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
- Added snapshot testing
- Fixed formatter for snapshots
This commit is contained in:
parent
3b3552b243
commit
7388740cb1
2
.gitignore
vendored
2
.gitignore
vendored
@ -10,4 +10,4 @@ junit.xml
|
||||
*.iml
|
||||
dist
|
||||
coverage
|
||||
test/tmp
|
||||
test/result
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
module.exports = {
|
||||
testRegex: '\\.spec\\.ts$',
|
||||
testRegex: '\\.spec\\.(ts|js)$',
|
||||
testEnvironment: 'node'
|
||||
};
|
||||
|
||||
19
package.json
19
package.json
@ -40,17 +40,22 @@
|
||||
"files": [
|
||||
"bin/index.js",
|
||||
"dist/index.js",
|
||||
"dist/index.d.ts",
|
||||
"dist/**/*.js",
|
||||
"src/templates/javascript/*.hbs",
|
||||
"src/templates/typescript/*.hbs"
|
||||
"dist/**/*.d.ts",
|
||||
"src/templates/javascript/**/*.hbs",
|
||||
"src/templates/javascript/**/*.js",
|
||||
"src/templates/typescript/**/*.hbs",
|
||||
"src/templates/typescript/**/*.ts"
|
||||
],
|
||||
"scripts": {
|
||||
"clean": "rimraf \"./dist\" \"./coverage\" \"./test/tmp\"",
|
||||
"clean": "rimraf \"./dist\" \"./coverage\" \"./test/result\"",
|
||||
"build": "tsc",
|
||||
"start": "tsc && node ./test/index.js",
|
||||
"test": "jest ./src",
|
||||
"test:watch": "jest --watch",
|
||||
"test:coverage": "jest --coverage",
|
||||
"run": "tsc && node ./test/index.js",
|
||||
"test": "tsc && jest",
|
||||
"test:update": "tsc && jest --updateSnapshot",
|
||||
"test:watch": "tsc && jest --watch",
|
||||
"test:coverage": "tsc && jest --coverage",
|
||||
"eslint": "eslint \"./src/**/*.ts\"",
|
||||
"eslint:fix": "eslint \"./src/**/*.ts\" --fix",
|
||||
"prettier": "prettier \"./src/**/*.ts\" --check",
|
||||
|
||||
@ -4,8 +4,6 @@ import { parse as parseV3 } from './openApi/v3';
|
||||
import { readHandlebarsTemplates } from './utils/readHandlebarsTemplates';
|
||||
import { getOpenApiSpec } from './utils/getOpenApiSpec';
|
||||
import { writeClient } from './utils/writeClient';
|
||||
import * as os from 'os';
|
||||
import * as chalk from 'chalk';
|
||||
import * as ts from 'typescript';
|
||||
import { getOpenApiVersion, OpenApiVersion } from './utils/getOpenApiVersion';
|
||||
|
||||
@ -32,13 +30,6 @@ export function generate(input: string, output: string, language: Language = Lan
|
||||
const inputPath = path.resolve(process.cwd(), input);
|
||||
const outputPath = path.resolve(process.cwd(), output);
|
||||
|
||||
console.log(chalk.bold.green('Generate:'));
|
||||
console.log(chalk.grey(' Input:'), input);
|
||||
console.log(chalk.grey(' Output:'), output);
|
||||
console.log(chalk.grey(' Language:'), language);
|
||||
console.log(chalk.grey(' HTTP client:'), httpClient);
|
||||
console.log(os.EOL);
|
||||
|
||||
try {
|
||||
// Load the specification, read the OpenAPI version and load the
|
||||
// handlebar templates for the given language
|
||||
|
||||
@ -13,7 +13,11 @@ export function format(s: string): string {
|
||||
indent--;
|
||||
i--;
|
||||
}
|
||||
return `${' '.repeat(i)}${line}`;
|
||||
const result = `${' '.repeat(i)}${line}`;
|
||||
if (result.trim() === '') {
|
||||
return '';
|
||||
}
|
||||
return result;
|
||||
});
|
||||
return lines.join(EOL);
|
||||
}
|
||||
|
||||
@ -17,12 +17,12 @@ import { format } from './format';
|
||||
export function writeClientModels(models: Map<string, Model>, language: Language, templates: Templates, outputPath: string): void {
|
||||
models.forEach(model => {
|
||||
const fileName = getFileName(model.name, language);
|
||||
// try {
|
||||
const templateData = exportModel(model);
|
||||
const templateResult = templates.model(templateData);
|
||||
fs.writeFileSync(path.resolve(outputPath, fileName), format(templateResult));
|
||||
// } catch (e) {
|
||||
// throw new Error(`Could not write model: "${fileName}"`);
|
||||
// }
|
||||
try {
|
||||
const templateData = exportModel(model);
|
||||
const templateResult = templates.model(templateData);
|
||||
fs.writeFileSync(path.resolve(outputPath, fileName), format(templateResult));
|
||||
} catch (e) {
|
||||
throw new Error(`Could not write model: "${fileName}"`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
3934
test/__snapshots__/index.spec.js.snap
Normal file
3934
test/__snapshots__/index.spec.js.snap
Normal file
File diff suppressed because it is too large
Load Diff
11
test/index.js
Executable file → Normal file
11
test/index.js
Executable file → Normal file
@ -1,22 +1,15 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
'use strict';
|
||||
|
||||
const OpenAPI = require('../dist');
|
||||
|
||||
OpenAPI.generate(
|
||||
'./test/mock/spec-v2.json',
|
||||
'./test/tmp/v2/ts/',
|
||||
'./test/result/v2/typescript/',
|
||||
OpenAPI.Language.TYPESCRIPT,
|
||||
OpenAPI.HttpClient.FETCH,
|
||||
);
|
||||
|
||||
OpenAPI.generate(
|
||||
'./test/mock/spec-v2.json',
|
||||
'./test/tmp/v2/js/',
|
||||
'./test/result/v2/javascript/',
|
||||
OpenAPI.Language.JAVASCRIPT,
|
||||
OpenAPI.HttpClient.XHR,
|
||||
);
|
||||
|
||||
OpenAPI.compile('./test/tmp/v2/ts/');
|
||||
|
||||
|
||||
46
test/index.spec.js
Normal file
46
test/index.spec.js
Normal file
@ -0,0 +1,46 @@
|
||||
const OpenAPI = require('../dist');
|
||||
const glob = require('glob');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
describe('generation', () => {
|
||||
|
||||
describe('typescript', () => {
|
||||
|
||||
OpenAPI.generate(
|
||||
'./test/mock/spec-v2.json',
|
||||
'./test/result/v2/typescript/',
|
||||
OpenAPI.Language.TYPESCRIPT,
|
||||
OpenAPI.HttpClient.FETCH,
|
||||
);
|
||||
|
||||
test.each(glob
|
||||
.sync('./test/result/v2/typescript/**/*.ts')
|
||||
.map(file => [file])
|
||||
)('file(%s)', file => {
|
||||
const content = fs.readFileSync(file, 'utf8').toString();
|
||||
expect(content).toMatchSnapshot(file);
|
||||
});
|
||||
});
|
||||
|
||||
describe('javascript', () => {
|
||||
|
||||
OpenAPI.generate(
|
||||
'./test/mock/spec-v2.json',
|
||||
'./test/result/v2/javascript/',
|
||||
OpenAPI.Language.JAVASCRIPT,
|
||||
OpenAPI.HttpClient.XHR,
|
||||
);
|
||||
|
||||
test.each(glob
|
||||
.sync('./test/result/v2/javascript/**/*.js')
|
||||
.map(file => [file])
|
||||
)('file(%s)', file => {
|
||||
const content = fs.readFileSync(file, 'utf8').toString();
|
||||
expect(content).toMatchSnapshot(file);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
"lib": ["esnext", "dom"],
|
||||
"types": ["node", "jest"],
|
||||
"typeRoots": ["node_modules/@types"],
|
||||
"declaration": false,
|
||||
"declaration": true,
|
||||
"declarationMap": false,
|
||||
"sourceMap": false,
|
||||
"noImplicitReturns": true,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user