test: test snapshotting TypeScript compiler

PR-URL: https://github.com/nodejs/node/pull/38905
Refs: https://github.com/nodejs/node/issues/35711
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Joyee Cheung 2021-09-27 15:24:40 +08:00
parent a63af1fd64
commit 48229e5dbb
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
5 changed files with 168841 additions and 0 deletions

8
test/fixtures/snapshot/ts-example.js vendored Normal file
View File

@ -0,0 +1,8 @@
var VirtualPoint = /** @class */ (function () {
function VirtualPoint(x, y) {
this.x = x;
this.y = y;
}
return VirtualPoint;
}());
var newVPoint = new VirtualPoint(13, 56);

11
test/fixtures/snapshot/ts-example.ts vendored Normal file
View File

@ -0,0 +1,11 @@
class VirtualPoint {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
const newVPoint = new VirtualPoint(13, 56);

View File

@ -0,0 +1,28 @@
// This file is to be concatenated with
// https://github.com/microsoft/TypeScript/blob/main/lib/typescript.js
// to produce a snapshot that reads a file path from the command
// line and compile it into JavaScript, then write the
// result into another file.
const fs = require('fs');
const v8 = require('v8');
const assert = require('assert');
v8.startupSnapshot.setDeserializeMainFunction(( { ts }) => {
const input = process.argv[1];
const output = process.argv[2];
console.error(`Compiling ${input} to ${output}`);
assert(input);
assert(output);
const source = fs.readFileSync(input, 'utf8');
let result = ts.transpileModule(
source,
{
compilerOptions: {
module: ts.ModuleKind.CommonJS
}
});
fs.writeFileSync(output, result.outputText, 'utf8');
}, { ts });

168725
test/fixtures/snapshot/typescript.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,69 @@
'use strict';
// This tests the TypeScript compiler in the snapshot.
const common = require('../common');
if (process.features.debug) {
common.skip('V8 snapshot does not work with mutated globals yet: ' +
'https://bugs.chromium.org/p/v8/issues/detail?id=12772');
}
const assert = require('assert');
const { spawnSync } = require('child_process');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const path = require('path');
const fs = require('fs');
tmpdir.refresh();
const blobPath = path.join(tmpdir.path, 'snapshot.blob');
// Concat test/fixtures/snapshot/typescript.js with
// test/fixtures/snapshot/typescript.js into
// tmpdir/snapshot.js.
const file = path.join(tmpdir.path, 'snapshot.js');
fs.copyFileSync(fixtures.path('snapshot', 'typescript.js'), file);
fs.appendFileSync(file,
fs.readFileSync(fixtures.path('snapshot', 'typescript-main.js')));
{
const child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
'--build-snapshot',
file,
], {
cwd: tmpdir.path
});
const stderr = child.stderr.toString();
const stdout = child.stdout.toString();
console.log(stderr);
console.log(stdout);
assert.strictEqual(child.status, 0);
const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
assert(stats.isFile());
}
{
const outPath = path.join(tmpdir.path, 'ts-example.js');
const child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
fixtures.path('snapshot', 'ts-example.ts'),
outPath,
], {
cwd: tmpdir.path,
});
const stderr = child.stderr.toString();
const snapshotOutput = child.stdout.toString();
console.log(stderr);
console.log(snapshotOutput);
assert.strictEqual(child.status, 0);
const result = fs.readFileSync(outPath, 'utf8');
const expected = fs.readFileSync(
fixtures.path('snapshot', 'ts-example.js'), 'utf8');
assert.strictEqual(result, expected);
}