Skip to content

Commit ddbd431

Browse files
Added basic tests (#23)
1 parent 1265d85 commit ddbd431

File tree

10 files changed

+1886
-45
lines changed

10 files changed

+1886
-45
lines changed

jest.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
transform: {'^.+\\.ts?$': 'ts-jest'},
5+
testRegex: '/test/.*\\.(test|spec)?\\.(ts|tsx)$',
6+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
7+
};

.github/workflows/pr.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ jobs:
2525
run: |
2626
yarn install
2727
yarn build
28+
- name: Test
29+
run: |
30+
yarn test
31+
- name: Create Binaries
32+
run: |
2833
yarn binary
2934
- name: Rename
3035
run: |

package.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@joernio/astgen",
3-
"version": "3.19.0",
3+
"version": "3.20.0",
44
"description": "Generate JS/TS AST in json format with Babel",
55
"exports": "./index.js",
66
"keywords": [
@@ -20,23 +20,29 @@
2020
"yargs": "^17.7.2"
2121
},
2222
"scripts": {
23+
"test": "jest --silent --config=\" jest.config.js\"",
2324
"build": "tsc --build",
2425
"binary": "pkg . --options max-old-space-size=8192 --no-bytecode --no-native-build --public --compress GZip --targets node18-linux-x64,node18-linux-arm64,node18-macos-x64,node18-win-x64,node18-macos-arm64"
2526
},
2627
"engines": {
2728
"node": ">=16.0.0"
2829
},
2930
"files": [
30-
"*.js",
31-
"dist/"
31+
"dist/**/*.js"
3232
],
33+
"directories": {
34+
"test": "test"
35+
},
3336
"devDependencies": {
34-
"@tsconfig/node16": "^16.1.3",
37+
"@tsconfig/node18": "^18.2.4",
3538
"@types/n-readlines": "^1.0.6",
36-
"@types/node": "^16.1.3",
39+
"@types/jest": "^29.5.14",
40+
"@types/node": "^18.2.4",
3741
"@types/yargs": "^17.0.33",
3842
"cross-env": "^7.0.3",
43+
"jest": "^29.7.0",
3944
"pkg": "^5.8.1",
45+
"ts-jest": "^29.2.5",
4046
"typescript": "^5.5.3"
4147
}
4248
}

src/FileUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as Defaults from "./Defaults.js";
1+
import * as Defaults from "./Defaults";
22

33
import {readdirpPromise} from 'readdirp';
44
import * as fs from "node:fs";

src/TscUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as Defaults from "./Defaults.js";
1+
import * as Defaults from "./Defaults";
22

33
import tsc from "typescript";
44

src/astgen.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env node
22

3-
import start from "./index.js"
4-
import Options from "./Options.js"
3+
import start from "./index"
4+
import Options from "./Options"
55

6-
import path from "path"
6+
import * as path from "node:path"
77
import yargs from "yargs"
88
import {hideBin} from "yargs/helpers"
99

src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import Options from "./Options.js"
2-
import * as Defaults from "./Defaults.js"
3-
import * as FileUtils from "./FileUtils.js"
4-
import * as JsonUtils from "./JsonUtils.js"
5-
import * as VueCodeCleaner from "./VueCodeCleaner.js"
6-
import * as TscUtils from "./TscUtils.js"
1+
import Options from "./Options"
2+
import * as Defaults from "./Defaults"
3+
import * as FileUtils from "./FileUtils"
4+
import * as JsonUtils from "./JsonUtils"
5+
import * as VueCodeCleaner from "./VueCodeCleaner"
6+
import * as TscUtils from "./TscUtils"
77

88
import * as babelParser from "@babel/parser"
99
import * as babelTypes from "@babel/types"
1010
import tsc, {SourceFile} from "typescript"
11-
import path from "node:path"
11+
import * as path from "node:path"
1212
import * as fs from "node:fs"
1313

1414
/**

test/astgen.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as path from "node:path"
2+
import * as os from "node:os"
3+
import * as fs from "node:fs"
4+
import start from "../src/index"
5+
6+
describe('astgen basic functionality', () => {
7+
it('should parse simple js file correctly', async () => {
8+
const tmpDir: string = fs.mkdtempSync(path.join(os.tmpdir(), "astgen-tests"));
9+
const testFile = path.join(tmpDir, "main.js");
10+
fs.writeFileSync(testFile, "console.log(\"Hello, world!\");");
11+
12+
await start({
13+
src: tmpDir,
14+
type: "js",
15+
output: path.join(tmpDir, "ast_out"),
16+
recurse: true,
17+
tsTypes: true
18+
});
19+
const resultAst = fs.readFileSync(path.join(tmpDir, "ast_out", "main.js.json")).toString();
20+
expect(resultAst).toContain("\"fullName\":\"" + testFile.replaceAll("\\", "\\\\") + "\"");
21+
expect(resultAst).toContain("\"relativeName\":\"main.js\"");
22+
const resultTypes = fs.readFileSync(path.join(tmpDir, "ast_out", "main.js.typemap")).toString();
23+
expect(resultTypes).toEqual("{\"0\":\"Console\",\"8\":\"{ (...data: any[]): void; (message?: any, ...optionalParams: any[]): void; }\",\"12\":\"\\\"Hello, world!\\\"\"}");
24+
25+
fs.rmSync(tmpDir, {recursive: true});
26+
});
27+
});

tsconfig.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
{
2-
"extends": "@tsconfig/node16/tsconfig.json",
3-
"include": ["src/**/*"],
4-
"exclude": ["node_modules"],
2+
"extends": "@tsconfig/node18/tsconfig.json",
3+
"include": [
4+
"src/**/*"
5+
],
6+
"exclude": [
7+
"node_modules"
8+
],
59
"compilerOptions": {
610
"sourceMap": true,
7-
"outDir": "dist"
11+
"outDir": "dist",
12+
"rootDir": "./src"
813
}
914
}
1015

0 commit comments

Comments
 (0)