- Added snapshot testing

- Fixed formatter for snapshots
This commit is contained in:
Ferdi Koomen 2019-11-23 11:01:12 +01:00
parent 3b3552b243
commit 7388740cb1
10 changed files with 4009 additions and 36 deletions

2
.gitignore vendored
View File

@ -10,4 +10,4 @@ junit.xml
*.iml
dist
coverage
test/tmp
test/result

View File

@ -1,4 +1,4 @@
module.exports = {
testRegex: '\\.spec\\.ts$',
testRegex: '\\.spec\\.(ts|js)$',
testEnvironment: 'node'
};

View File

@ -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",

View File

@ -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

View File

@ -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);
}

View File

@ -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}"`);
}
});
}

File diff suppressed because it is too large Load Diff

11
test/index.js Executable file → Normal file
View 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
View 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);
});
});
});

View 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,