Skip to content

Commit

Permalink
Merge branch 'chore/snapshots'
Browse files Browse the repository at this point in the history
  • Loading branch information
yhnavein committed Mar 30, 2024
2 parents 42dbb35 + ab2bf5f commit caea132
Show file tree
Hide file tree
Showing 15 changed files with 3,009 additions and 211 deletions.
3 changes: 0 additions & 3 deletions .mocharc.js

This file was deleted.

5 changes: 5 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extension": ["js", "ts"],
"require": ["sucrase/register"],
"spec": ["src/**/*.spec.ts", "test/**/*.spec.ts"]
}
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/snapshots
35 changes: 16 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swaggie",
"version": "0.8.3",
"version": "0.8.4",
"description": "Generate ES6 or TypeScript service integration code from an OpenAPI spec",
"author": {
"name": "Piotr Dabrowski",
Expand All @@ -16,7 +16,7 @@
"url": "https://github.com/yhnavein/swaggie/issues"
},
"engines": {
"node": ">=14.0.0"
"node": ">=16.0.0"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -27,12 +27,9 @@
"build": "sucrase ./src -d ./dist --transforms typescript,imports && npm run rm-tests && npm run types",
"rm-tests": "find dist/ -name '*.spec.js' -type f -delete",
"types": "tsc src/types.ts --outDir dist/ --declaration --emitDeclarationOnly && cp test/index.d.ts ./dist/",
"test": "mocha 'src/**/*.spec.ts'"
"test": "mocha"
},
"files": [
"dist",
"templates"
],
"files": ["dist", "templates"],
"keywords": [
"swagger",
"swagger 2.0",
Expand All @@ -45,22 +42,22 @@
"dependencies": {
"case": "^1.6.3",
"commander": "^10.0.0",
"dset": "^3.1.2",
"eta": "^2.0.1",
"dset": "^3.1.3",
"eta": "^3.4.0",
"js-yaml": "^4.1.0",
"nanocolors": "^0.2.0",
"node-fetch": "^2.6.7"
},
"devDependencies": {
"@types/chai": "4.3.9",
"@types/js-yaml": "4.0.8",
"@types/mocha": "10.0.3",
"@types/node-fetch": "2.6.7",
"@types/sinon": "^10.0.20",
"chai": "4.3.10",
"mocha": "10.2.0",
"sinon": "^17.0.0",
"sucrase": "3.34.0",
"typescript": "5.2.2"
"@types/chai": "4.3.14",
"@types/js-yaml": "4.0.9",
"@types/mocha": "10.0.6",
"@types/node-fetch": "2.6.11",
"@types/sinon": "17.0.3",
"chai": "4.4.1",
"mocha": "10.4.0",
"sinon": "17.0.1",
"sucrase": "3.35.0",
"typescript": "5.4.3"
}
}
18 changes: 4 additions & 14 deletions src/gen/templateManager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import os from 'os';
import fs from 'fs';
import path from 'path';
import * as Eta from 'eta';
import { expect } from 'chai';
import { loadAllTemplateFiles, renderFile } from './templateManager';

const GOOD_FILE = 'client.ejs';

describe('loadAllTemplateFiles', () => {
beforeEach(() => Eta.templates.reset());

it('should handle loading wrong template', async () => {
expect(() => {
loadAllTemplateFiles('non-existent');
Expand All @@ -27,22 +24,15 @@ describe('loadAllTemplateFiles', () => {
loadAllTemplateFiles(null);
}).to.throw('No template');
});

it('should load template file to the memory', async () => {
loadAllTemplateFiles('axios');

const cachedTemplate = Eta.templates.get('client.ejs');
expect(cachedTemplate).to.be.a('function');
});
});

describe('render', () => {
beforeEach(() => {
loadAllTemplateFiles('axios');
});

it('should get existing template', async () => {
const templateFunction = await renderFile(GOOD_FILE, {
it('should get existing template', () => {
const templateFunction = renderFile(GOOD_FILE, {
clientName: 'Test',
camelCaseName: 'test',
baseUrl: null,
Expand All @@ -52,8 +42,8 @@ describe('render', () => {
expect(templateFunction).to.contain('testClient');
});

it('should render template that is complex (multiple levels of includes)', async () => {
const templateFunction = await renderFile(GOOD_FILE, {
it('should render template that is complex (multiple levels of includes)', () => {
const templateFunction = renderFile(GOOD_FILE, {
clientName: 'Test',
camelCaseName: 'test',
baseUrl: null,
Expand Down
15 changes: 5 additions & 10 deletions src/gen/templateManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import fs from 'fs';
import path from 'path';
import * as Eta from 'eta';
import { Eta } from 'eta';

let engine: Eta;

export function loadAllTemplateFiles(templateName: string | null) {
if (!templateName) {
Expand All @@ -16,16 +18,9 @@ export function loadAllTemplateFiles(templateName: string | null) {
`Could not found directory with the template (we tried ${templatesDir}). Template name is correct?`
);
}
const templates = fs.readdirSync(templatesDir);

templates.forEach((t) => {
const filePath = path.join(templatesDir, t);
const file = fs.readFileSync(filePath, 'utf8');
Eta.templates.define(t, Eta.compile(file));
});
engine = new Eta({ views: templatesDir });
}

export function renderFile(templateFile: string, data: object = {}) {
const template = Eta.templates.get(templateFile);
return Eta.render(template, data);
return engine.render(templateFile, data);
}
2 changes: 1 addition & 1 deletion swaggie.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions test/snapshots.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect } from 'chai';
import fs from 'fs';
import { runCodeGenerator } from '../src/index';
import { FullAppOptions, Template } from '../src/types';

const templates: Template[] = ['axios', 'swr-axios', 'fetch', 'ng1', 'ng2'];

describe('petstore snapshots', () => {
templates.forEach((template) => {
it(`should match existing ${template} snapshot`, async () => {
const snapshotFile = `./test/snapshots/${template}.ts`;
const parameters: FullAppOptions = {
src: './test/petstore-v2.json',
out: './.tmp/test/',
template,
};

const [generatedCode] = await runCodeGenerator(parameters);

if (process.env.UPDATE_SNAPSHOTS) {
fs.writeFileSync(snapshotFile, generatedCode, 'utf8');
expect(true).to.eq(true);
} else {
const existingSnapshot = fs.readFileSync(snapshotFile, 'utf8');

expect(existingSnapshot).to.equal(generatedCode);
}
});
});
});
12 changes: 12 additions & 0 deletions test/snapshots/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Snapshots

This directory contains snapshots of all supported templates. This is a very simple
mechanism for detecting (wanted or not) changes in the output of the templates.

The files are rendered as-is without Prettier formatting.

Do not make any manual changes in this directory.

## Updating snapshots

To update the snapshots, run `UPDATE_SNAPSHOTS=1 yarn test`
Loading

0 comments on commit caea132

Please sign in to comment.