Skip to content

Commit 6507c88

Browse files
Robin BuschmannRobin Buschmann
authored andcommitted
tests added for checking transpiled code
1 parent cad18f7 commit 6507c88

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

test/specs/transpiled-code.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {expect, use} from 'chai';
2+
import {readFileSync, readdirSync, statSync} from 'fs';
3+
import * as chaiAsPromised from 'chai-as-promised';
4+
import {containsEs6Syntax} from "../utils/common";
5+
6+
use(chaiAsPromised);
7+
8+
describe('transpiled code', () => {
9+
10+
const es6FileSubPaths = [
11+
'v4/Model.js',
12+
'v4/Sequelize.js',
13+
];
14+
15+
(function run(path: string): void {
16+
17+
readdirSync(path)
18+
.forEach(name => {
19+
20+
const targetPath = path + '/' + name;
21+
22+
if (statSync(targetPath).isDirectory()) {
23+
24+
run(targetPath);
25+
} else if (name.slice(-3) === '.js') {
26+
27+
const parentDir = path.split('/').pop();
28+
const target = parentDir + '/' + name;
29+
const isEs6 = es6FileSubPaths.indexOf(target) !== -1;
30+
31+
describe(target, () => {
32+
33+
it(`should ${isEs6 ? '' : 'NOT '}contain es6 syntax`, () => {
34+
35+
const content = readFileSync(targetPath).toString();
36+
37+
try {
38+
39+
expect(containsEs6Syntax(content)).to.be[isEs6.toString()];
40+
} catch (e) {
41+
42+
e.message = e.message + '\n\n\nAffected content:\n\n' + content + '\n\n\n';
43+
44+
throw e;
45+
}
46+
});
47+
});
48+
}
49+
});
50+
51+
})(__dirname + '/../../lib');
52+
53+
});

test/utils/common.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,19 @@ export function assertInstance(instance: any|any[], expectedValues: any|any[]):
3636

3737
});
3838
}
39+
40+
/**
41+
* Checks if specified value contains es6 syntax or not;
42+
* @returns true if it contains es6 syntax
43+
*/
44+
export function containsEs6Syntax(value: string): boolean {
45+
46+
// tslint:disable:max-line-length
47+
const ES6_SYNTAX_REGEX = /(^class | class )|(^const | const )|(^let | let )|(^async | async )|(^await | await )|(^yield | yield )|(=>)|function\*|\`/gm;
48+
49+
return ES6_SYNTAX_REGEX.test(
50+
value
51+
.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '') // remove /** */ and /* */ // comments
52+
.replace(/(["'])(?:(?=(\\?))\2.)*?\1/gm, '') // remove all quotes
53+
);
54+
}

0 commit comments

Comments
 (0)