Skip to content

Commit 49bc0d9

Browse files
committed
test: add jest snapshot test
1 parent 8e37178 commit 49bc0d9

File tree

13 files changed

+2370
-22
lines changed

13 files changed

+2370
-22
lines changed

.eslintignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
node_modules
2-
dist
1+
node_modules/
2+
dist/
3+
coverage/

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"eslint:recommended",
99
"plugin:@typescript-eslint/recommended"
1010
],
11+
"env": {
12+
"node": true
13+
},
1114
"rules": {
1215
"@typescript-eslint/no-unused-vars": ["error", { "args": "none" }]
1316
}

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ jobs:
2424
cache: 'yarn'
2525
- run: yarn install --frozen-lockfile
2626
- run: yarn lint
27+
- run: yarn test
2728
- run: yarn build

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
node_modules/
22
dist/
3+
coverage/
34

45
*.log
56
*.swp

dprint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"excludes": [
1414
"**/node_modules",
1515
"**/*-lock.json",
16-
"dist"
16+
"dist",
17+
"coverage"
1718
],
1819
"plugins": [
1920
"https://plugins.dprint.dev/typescript-0.62.2.wasm",

jest.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
globals: {
6+
'ts-jest': {
7+
isolatedModules: true,
8+
},
9+
},
10+
testPathIgnorePatterns: ['dist'],
11+
};

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@
2121
"build": "rm -rf dist && npm run build:cjs && npm run build:ejs",
2222
"build:cjs": "tsc --outDir dist/cjs --declaration",
2323
"build:ejs": "tsc --outDir dist/ejs --module es6",
24+
"test": "jest --coverage",
2425
"lint": "dprint check && eslint .",
2526
"format": "dprint fmt"
2627
},
28+
"files": ["dist"],
2729
"devDependencies": {
30+
"@types/jest": "^27.4.0",
2831
"@typescript-eslint/eslint-plugin": "^5.10.2",
2932
"@typescript-eslint/parser": "^5.10.2",
3033
"dprint": "^0.22.0",
3134
"eslint": "^8.8.0",
35+
"jest": "^27.4.7",
36+
"ts-jest": "^27.1.3",
3237
"ttypescript": "^1.5.13",
3338
"typescript": "^4.5.5"
3439
},
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`transform export const enum into object literal test-case/const-enum.d.ts 1`] = `
4+
"export declare enum SomeEnum {
5+
A = 0,
6+
B = 1,
7+
C = \\"hello\\",
8+
D = 1000,
9+
E = 1001
10+
}
11+
export declare enum ComputedEnum {
12+
A = 0,
13+
B = 1,
14+
C = 2,
15+
D = -2,
16+
E = 4,
17+
F = -1,
18+
G = 1228,
19+
H = 31,
20+
I = 230,
21+
J = \\"12\\",
22+
K = 1
23+
}
24+
export declare enum RegularEnum {
25+
A = 0,
26+
B = 1
27+
}
28+
"
29+
`;
30+
31+
exports[`transform export const enum into object literal test-case/const-enum.js 1`] = `
32+
"export const SomeEnum = {
33+
A: 0,
34+
B: 1,
35+
C: \\"hello\\",
36+
D: 1000,
37+
E: 1001
38+
};
39+
export const ComputedEnum = {
40+
A: 0,
41+
B: 1,
42+
C: 2,
43+
D: -2,
44+
E: 4,
45+
F: -1,
46+
G: 1228,
47+
H: 31,
48+
I: 230,
49+
J: \\"12\\",
50+
K: 1
51+
};
52+
export var RegularEnum;
53+
(function (RegularEnum) {
54+
RegularEnum[RegularEnum[\\"A\\"] = 0] = \\"A\\";
55+
RegularEnum[RegularEnum[\\"B\\"] = 1] = \\"B\\";
56+
})(RegularEnum || (RegularEnum = {}));
57+
"
58+
`;

src/transform.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import ts from 'typescript';
2+
import transform from './transform';
3+
4+
describe('transform export const enum into object literal', () => {
5+
const output = compile('test-case/const-enum.ts');
6+
7+
for (const [fileName, content] of Object.entries(output)) {
8+
it(fileName, () => {
9+
expect(content).toMatchSnapshot();
10+
});
11+
}
12+
});
13+
14+
type Output = { [fileName: string]: string };
15+
function compile(fileName: string): Output {
16+
const program = ts.createProgram({
17+
rootNames: [fileName],
18+
options: {
19+
target: ts.ScriptTarget.ES2015,
20+
module: ts.ModuleKind.ES2015,
21+
preserveConstEnums: true,
22+
declaration: true,
23+
moduleResolution: ts.ModuleResolutionKind.NodeJs,
24+
},
25+
});
26+
27+
const transformer = transform(program);
28+
const output: Output = {};
29+
30+
program.emit(
31+
undefined,
32+
(fileName, data) => {
33+
output[fileName] = (output[fileName] || '') + data;
34+
},
35+
undefined,
36+
false,
37+
{
38+
before: [transformer],
39+
afterDeclarations: [transformer as any],
40+
},
41+
);
42+
43+
return output;
44+
}

src/transform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ts from 'typescript';
22
import { evaluate, hasModifier } from './utils';
33

4-
export default function(program: ts.Program, pluginOptions: unknown) {
4+
export default function(program: ts.Program, pluginOptions?: unknown) {
55
return (ctx: ts.TransformationContext) => {
66
return (sourceFile: ts.SourceFile) => {
77
const ambient = sourceFile.isDeclarationFile;

0 commit comments

Comments
 (0)