Skip to content

Commit 40a5a9c

Browse files
arthurfioretteRafaelGSS
authored andcommitted
refactor: test improvements
1 parent 3a24fd6 commit 40a5a9c

File tree

2 files changed

+86
-68
lines changed

2 files changed

+86
-68
lines changed

fixtures/ts-sample.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Loads a lot of libraries and demonstrates various TypeScript features
2-
import 'autocannon'
3-
import 'dotenv'
4-
import 'fastify'
5-
import 'lodash'
6-
import 'moment'
7-
import 'pino'
8-
import 'piscina'
9-
import 'prettier'
10-
import 'tinybench'
11-
import 'typescript'
12-
import 'underscore'
13-
import 'winston'
2+
import 'autocannon';
3+
import 'dotenv';
4+
import 'fastify';
5+
import 'lodash';
6+
import 'moment';
7+
import 'pino';
8+
import 'piscina';
9+
import 'prettier';
10+
import 'tinybench';
11+
import 'typescript';
12+
import 'underscore';
13+
import 'winston';
1414

1515
// Decorators
1616
function Logger(target: any, propertyKey: string) {

src/typescript-benchmark.js

Lines changed: 74 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,82 @@
1-
const fs = require('node:fs');
2-
const ts = require('typescript');
3-
const path = require('node:path');
1+
const fs = require("node:fs");
2+
const ts = require("typescript");
3+
const path = require("node:path");
44

5-
const filePath = path.join(__dirname, '..', 'fixtures', 'ts-sample.ts');
5+
const filePath = path.join(__dirname, "..", "fixtures", "ts-sample.ts");
6+
const code = fs.readFileSync(filePath, "utf8");
67

7-
const code = fs.readFileSync(filePath, 'utf8');
8+
module.exports = {
9+
name: "typescript",
10+
type: "operation",
11+
operations: [
12+
{
13+
name: "transpile",
14+
fn: () => {
15+
ts.transpile(
16+
code,
17+
{
18+
// CJS Settings
19+
target: ts.ScriptTarget.ESNext,
20+
module: ts.ModuleKind.CommonJS,
21+
moduleResolution: ts.ModuleResolutionKind.Node,
822

9-
/** @type {import('typescript').CompilerOptions} */
10-
const compilerOptions = {
11-
// also loads and checks js
12-
allowJs: true,
13-
checkJs: true,
23+
// Avoid writing to disk
24+
noEmit: true,
1425

15-
target: ts.ScriptTarget.ESNext,
16-
module: ts.ModuleKind.ESNext,
26+
// Avoids any checking related code
27+
checkJs: false,
28+
strict: false,
29+
isolatedModules: true,
30+
skipDefaultLibCheck: true,
31+
skipLibCheck: true,
32+
},
33+
filePath,
34+
);
35+
},
36+
},
37+
{
38+
name: "createSourceFile",
39+
fn: () => {
40+
ts.createSourceFile(
41+
filePath,
42+
code,
43+
ts.ScriptTarget.ESNext,
44+
false,
45+
ts.ScriptKind.TS,
46+
);
47+
},
48+
},
49+
{
50+
name: "getSemanticDiagnostics",
51+
fn: () => {
52+
const program = ts.createProgram({
53+
rootNames: [filePath],
54+
options: {
55+
// CJS Settings
56+
target: ts.ScriptTarget.ESNext,
57+
module: ts.ModuleKind.CommonJS,
58+
moduleResolution: ts.ModuleResolutionKind.Node,
1759

18-
// test types
19-
strict: true,
60+
// also loads and checks js
61+
allowJs: true,
62+
checkJs: true,
2063

21-
// loads all declarations
22-
isolatedModules: false,
23-
skipDefaultLibCheck: false,
24-
skipLibCheck: false
25-
};
64+
// test types
65+
strict: true,
2666

27-
module.exports = {
28-
name: 'typescript',
29-
type: 'operation',
30-
operations: [
31-
{
32-
name: 'transpile',
33-
fn: () => {
34-
ts.transpileModule(code, {
35-
compilerOptions: compilerOptions,
36-
fileName: filePath,
37-
reportDiagnostics: true
38-
});
39-
}
40-
},
41-
{
42-
name: 'createSourceFile',
43-
fn: () => {
44-
ts.createSourceFile(
45-
filePath,
46-
code,
47-
ts.ScriptTarget.ESNext,
48-
true,
49-
ts.ScriptKind.TS
50-
);
51-
}
52-
},
53-
{
54-
name: 'getTypeChecker',
55-
fn: () => {
56-
ts.createProgram({
57-
rootNames: [filePath],
58-
options: compilerOptions
59-
}).getTypeChecker();
60-
}
61-
}
62-
],
63-
benchmarker: 'tinybench'
67+
// Avoid writing to disk
68+
noEmit: true,
69+
70+
// Avoids loading external files
71+
isolatedModules: true,
72+
skipDefaultLibCheck: true,
73+
skipLibCheck: true,
74+
},
75+
});
76+
77+
program.getSemanticDiagnostics(program.getSourceFile(filePath));
78+
},
79+
},
80+
],
81+
benchmarker: "tinybench",
6482
};

0 commit comments

Comments
 (0)