All files / src runner.test-harness.ts

81.79% Statements 346/423
73.26% Branches 74/101
90% Functions 18/20
81.79% Lines 346/423

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 4241x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 80704x 80704x 80704x 80704x 80704x 80704x 80704x 80704x 80704x 1x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 406x 44x 44x 8x 8x 8x 8x 44x 44x 44x 406x 406x 406x 406x 406x 406x 406x 406x 406x 2x 2x 406x 406x     406x 406x 406x 5702x 5702x 5702x 450x 450x 450x 450x 5252x 5252x 5252x 5252x 5252x 5252x 5252x 5702x         5702x         5252x 5252x 5252x 5252x 5702x 5252x 406x 406x 466x     466x 466x 466x 406x 406x 406x 406x 406x 406x 406x 74294x 74294x 74294x 74294x 32x 74294x 74262x 74262x 74262x 74262x 74262x 74262x 74262x 74262x 74262x 74262x 74294x 74294x 406x 406x 406x 406x 406x 406x 406x                                   406x 406x 406x 406x 406x 406x     406x                   406x 406x 4x 4x 4x 4x 406x 406x 406x 1x 1x 1x 1x 1x 1x 1x 218x 218x 218x 218x 218x 218x 218x 218x     218x 218x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 203x 203x 203x 203x 1x 203x 203x 203x           203x 203x 203x 203x 242x 242x 242x 242x 242x 242x 242x     242x 242x 232x 232x 218x 232x 218x 218x 232x 232x 10x 242x 242x 242x     10x 218x               10x 10x 203x 203x 203x 203x 203x 189x 189x 189x         189x 203x 1x 203x 203x           203x 203x 203x 203x 203x 188x 188x 188x 188x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 466x 466x 462x 462x 462x 462x 466x 23x 23x 23x 23x 23x 23x 23x 23x 462x 466x 211x 211x 211x 211x 462x 462x 462x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x     203x 203x 203x 203x 203x 203x     203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x     203x 203x 203x 203x 1x 39x 39x 39x 39x 39x 22x 22x 11x 11x 11x 24x 16x 16x 11x 11x 11x 11x 11x  
import * as ts from 'typescript';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import * as childProcess from 'child_process';
import * as tsrtti from './index';
import transformer from './transformer';
import { rimraf } from 'rimraf';
 
export interface RunInvocation {
    code: string;
    transformerEnabled?: boolean;
    moduleType?: 'commonjs' | 'esm';
    target?: ts.ScriptTarget;
    compilerOptions?: Partial<ts.CompilerOptions>;
    outputTransformer?: (filename: string, code: string) => string;
    modules?: Record<string, any>;
    trace?: boolean;
    checks?: (exports: any) => void;
}
 
function normalizeFilename(filename: string, extension?: string) {
    if (extension && !filename.endsWith(`.${extension}`))
        filename = `${filename}.${extension}`;
 
    if (!filename.includes('/'))
        filename = `./${filename}`;
 
    return filename;
}
 
export function compile(invocation: RunInvocation): Record<string, string> {
    let options: ts.CompilerOptions = {
        ...ts.getDefaultCompilerOptions(),
        ...<ts.CompilerOptions>{
            target: ts.ScriptTarget.ES2016,
            module: ts.ModuleKind.CommonJS,
            moduleResolution: ts.ModuleResolutionKind.NodeJs,
            experimentalDecorators: true,
            lib: ['lib.es2016.d.ts'],
            noLib: false,
            emitDecoratorMetadata: false,
            suppressOutputPathCheck: true,
            rtti: <any>{ trace: invocation.trace === true, throwOnFailure: true }
        },
        ...invocation.compilerOptions || {},
    };
 
    if (options.noLib)
        delete options.lib;
 
    let inputs: Record<string, ts.SourceFile> = {
        './main.ts': ts.createSourceFile('./main.ts', invocation.code, options.target!)
    };
 
    let otherFiles = Object.keys(invocation?.modules ?? {}).filter(x => x.endsWith('.ts') || x.startsWith('@types/'));
    for (let filename of otherFiles) {
        let source = invocation.modules[filename];
        if (filename.startsWith('@types/')) {
            let packageName = filename.replace(/^@types\//, '');
            source = `declare module "${packageName}" {\n${source}\n}`;
            filename = `./${packageName}.d.ts`;
        }
 
        inputs[filename] = ts.createSourceFile(filename, source, options.target!);
    }
 
    let outputs: Record<string, string> = {};
 
    if (invocation.moduleType) {
        if (invocation.moduleType === 'esm')
            options.module = ts.ModuleKind.ES2020;
    }
 
    if (invocation.target) {
        options.target = invocation.target;
    }
 
    if (invocation.trace) {
        console.log(`Test: Feeding ${Object.keys(inputs).length} files to Typescript: ${Object.keys(inputs).join(', ')}`);
    }
 
    const program = ts.createProgram(Object.keys(inputs), options, {
        getSourceFile: (fileName: string) => {
            fileName = normalizeFilename(fileName, 'ts');
 
            if (inputs[fileName]) {
                if (invocation.trace)
                    console.log(`Test: Typescript opened input '${fileName}'`);
                return inputs[fileName];
            }
 
            // libs
 
            let libLoc = path.resolve(__dirname, '../node_modules/typescript/lib', fileName);
            try {
                let stat = fs.statSync(libLoc);
 
                if (!stat.isFile()) {
                    if (invocation.trace)
                        console.log(`Test: Typescript tried to open library '${fileName}': not found`);
                    return;
                }
            } catch (e) {
                if (invocation.trace)
                    console.log(`Test: Typescript tried to open library '${fileName}', error: ${e.message}`);
                return;
            }
 
            let buf = fs.readFileSync(libLoc);
 
            if (invocation.trace)
                console.log(`Test: Typescript opened library ${fileName}`);
            return ts.createSourceFile(libLoc, buf.toString('utf-8'), ts.ScriptTarget.Latest);
        },
        writeFile: (filename: string, text: string) => {
            if (invocation.trace) {
                console.log(`Test: Emitting '${filename}'...`);
            }
 
            filename = normalizeFilename(filename, 'js');
            outputs[filename] = text;
        },
        getDefaultLibFileName: () => "lib.d.ts",
        useCaseSensitiveFileNames: () => false,
        getCanonicalFileName: fileName => fileName,
        getCurrentDirectory: () => "",
        getNewLine: () => "\n",
        fileExists: (fileName: string): boolean => {
            let exists = false;
            fileName = normalizeFilename(fileName, 'ts');
 
            if (!!inputs[fileName] || !!invocation?.modules?.[fileName]) {
                exists = true;
            } else {
                let libLoc = path.resolve(__dirname, '../node_modules/typescript/lib', fileName);
                try {
                    let stat = fs.statSync(libLoc);
 
                    if (stat.isFile())
                        exists = true;
                } catch (e) {
                    exists = false;
                }
            }
 
            return exists;
        },
        readFile: () => "",
        directoryExists: () => true,
        getDirectories: () => []
    });
 
    if (invocation.trace) {
        // TODO: All tests should have this enabled

        let optionsDiags = program.getOptionsDiagnostics();
        let syntacticDiags = program.getSyntacticDiagnostics();
        let semanticDiags = program.getSemanticDiagnostics();
        let diags = ([] as ts.Diagnostic[]).concat(optionsDiags, syntacticDiags, semanticDiags);

        diags = diags.filter(x => ![2307].includes(x.code)); // filter 'Cannot find module...'

        if (diags.length > 0) {
            throw new Error(`Typescript did not compile this test correctly. Errors: ${JSON.stringify(diags.map(x => ts.formatDiagnostic(x, {
                getCanonicalFileName: fileName => fileName,
                getCurrentDirectory: () => './test',
                getNewLine: () => `\n`
            })))}`);
        }
    }
 
    if (invocation.transformerEnabled !== false) {
        program.emit(undefined, undefined, undefined, undefined, {
            before: [transformer(program)]
        });
    } else {
        program.emit();
    }
    if (outputs['./main.js'] === undefined) {
        if (program.getOptionsDiagnostics().length > 0) {
            console.dir(program.getOptionsDiagnostics());
        } else {
            console.error(`Diagnostics for './main.ts':`);
            console.dir(program.getSyntacticDiagnostics(inputs['./main.ts']));
        }

        throw new Error(`Failed to compile test code: '${invocation.code}'. Code was emitted for: ${JSON.stringify(Object.keys(outputs))}`);
    }
 
    if (invocation.outputTransformer) {
        for (let key of Object.keys(outputs)) {
            outputs[key] = invocation.outputTransformer(key, outputs[key]);
        }
    }
 
    return outputs;
}
 
interface Module {
    exports: any;
    filename: string;
    code?: string;
}
 
function runCommonJS(module: Module, $require) {
    try {
        eval(`(
            function(module, exports, require){
                ${module.code}
            }
        )`)(module, module.exports, $require);
    } catch (e) {
        throw new Error(`[CJS] Failed to run '${module.filename}': ${e.message}`);
    }
    return module;
}
 
const { Script } = require('vm');
let pinnedScripts: any[] = [];
let originalRun = Script.prototype.runInThisContext;
Script.prototype.runInThisContext = function (options) {
    console.log(`HAPPENING`);
	pinnedScripts.push(this);
	return originalRun.call(this, options);
};
 
/**
 * Compile the given code using Typescript and typescript-rtti transformer plugin and return the
 * resulting exports.
 *
 * @param invocation
 * @returns
 */
export async function runSimple(invocation: RunInvocation) {
    await runSimpleCJS({ ...invocation, moduleType: 'commonjs' });
    await runSimpleESM({ ...invocation, moduleType: 'esm' });
}
 
async function runSimpleCJS(invocation: RunInvocation) {
    let outputs = compile(invocation);
    if (invocation.trace) {
        let outputs = compile(invocation);
        for (let filename of Object.keys(outputs)) {
            console.log(`    // [CJS] ${filename}\n\n    ${outputs[filename].replace(/\r?\n/g, `\n    `)}`);
        }
    }
 
    let modules: Record<string, Module> = {};
 
    let $require = (moduleName: string) => {
        let fileName = moduleName;
 
        fileName = normalizeFilename(fileName, 'js');
 
        let code = outputs[fileName];
 
        if (!code && typeof invocation.modules?.[moduleName] === 'string') {
            code = invocation.modules?.[moduleName];
        }
 
        if (code) {
            if (modules[fileName])
                return modules[fileName].exports;
 
            return modules[fileName] ?? runCommonJS(modules[fileName] = {
                exports: {}, code,
                filename: fileName
            }, $require).exports;
        }
 
        let symbols = invocation.modules?.[moduleName];
 
        if (!symbols && moduleName === 'typescript-rtti') {
            symbols = tsrtti;
        }
 
        if (!symbols) {
            throw new Error(
                `(RTTI Test) Cannot find module '${moduleName}' [aka '${fileName}']. `
                + `Compilation outputs are: ${JSON.stringify(Object.keys(outputs))}, `
                + `JSON modules are: ${JSON.stringify(Object.keys(invocation?.modules ?? [])
                    .filter(x => !x.endsWith('.ts')))}`
            );
        }
 
        return symbols;
    };
 
    let exports = $require('./main.js');
 
    if (invocation.checks) {
        try {
            invocation.checks(exports);
        } catch (e) {
            if (invocation.trace)
                console.error(`The check error occurred in CommonJS mode.`);
            throw e;
        }
    }
}
 
async function runSimpleESM(invocation: RunInvocation) {
    if (invocation.trace) {
        let outputs = compile(invocation);
        for (let filename of Object.keys(outputs)) {
            console.log(`    // [ESM] ${filename}\n\n    ${outputs[filename].replace(/\r?\n/g, `\n    `)}`);
        }
    }
 
    let tsrttiLocation = path.resolve(__dirname, "..", "dist.esm", "index.js").replace(/\\/g, '/');
    invocation.modules ??= {}
 
    if (!invocation.modules['typescript-rtti']) {
        invocation.modules['typescript-rtti'] = `
            export * from "file://${tsrttiLocation}";
        `
    }
 
    let outputs = compile(invocation);
    let exports: Record<string, any> = {};
    let files = { ...outputs, ...invocation.modules };
 
    let folder = path.resolve(os.tmpdir(), `tsrtti_esm_test_${Math.random() * 1_000_000 | 0}`);
 
    await rimraf(folder);
    fs.mkdirSync(folder);
    fs.mkdirSync(path.resolve(folder, "node_modules"));
 
    fs.writeFileSync(path.resolve(folder, "package.json"), JSON.stringify({
        type: 'module',
        main: './__test.js'
    }));
 
    let pkgJson = JSON.stringify({
        type: 'module',
        main: './index.js'
    });
 
    for (let file of Object.keys(files)) {
        if (file.startsWith('@types/'))
            continue;
 
        let filePath = path.resolve(folder, file);
        let fileContent = files[file];
 
        if (typeof fileContent !== 'string') {
            let object = fileContent;
            fileContent = ``;
 
            for (let key of Object.keys(object)) {
                fileContent += `export const ${key} = ${serializeJavascriptObject(object[key])};`;
                fileContent += `\n`;
            }
        }
 
        if (!file.startsWith("./")) {
            filePath = path.resolve(folder, "node_modules", file, "index.js");
            fs.mkdirSync(path.resolve(folder, "node_modules", file));
            fs.writeFileSync(path.resolve(folder, "node_modules", file, "package.json"), pkgJson);
        }
 
        fs.writeFileSync(filePath, fileContent);
    }
 
    let reflectMetadataLocation = path.resolve(__dirname, "..", "node_modules", "reflect-metadata").replace(/\\/g, '/');
    let chaiLocation = path.resolve(__dirname, "..", "node_modules", "chai").replace(/\\/g, '/')
 
    fs.writeFileSync(path.resolve(folder, "__test.js"), `
        import "file://${reflectMetadataLocation}/Reflect.js";
        import * as exports from './main.js';
        import * as chai_1 from "file://${chaiLocation}/index.mjs";
 
        (${(invocation.checks ?? (() => {})).toString()})(exports)
    `);
 
    if (invocation.trace) {
        console.log(`RTTI Test: executing code under test...`);
    }
 
    let proc = childProcess.spawn('node', [`${path.resolve(folder, '__test.js')}`], {
        stdio: 'pipe'
    });
 
    if (invocation.trace) {
        console.log(`RTTI Test: test folder is ${folder}`);
    }
 
    let stdout = Buffer.alloc(0);
 
    proc.stdout.on('data', buf => stdout = Buffer.concat([stdout, Buffer.from(buf)]));
    proc.stderr.on('data', buf => stdout = Buffer.concat([stdout, Buffer.from(buf)]));
 
    await new Promise<void>((resolve, reject) => proc.on('exit', code => {
        if (code === 0) {
            resolve();
            return;
        }

        reject(new Error(`Failed in ESM mode. Test folder: ${folder}\nOutput:\n${stdout.toString('utf-8')}`));
    }));
 
    await rimraf(folder);
}
 
function serializeJavascriptObject(object: object, depth = 1) {
    if (object === undefined)
        return `undefined`;
    else if (typeof object === 'function')
        return object.toString();
    else if (object === null || typeof object !== 'object')
        return JSON.stringify(object);
 
    let obj = `{\n`;
 
    for (let key of Object.keys(object)) {
        obj += `${Array(depth + 1).join('    ')}${JSON.stringify(key)}: ${serializeJavascriptObject(object[key], depth + 1)},\n`;
    }
 
    obj += `${Array(depth).join('    ')}}`
 
    return obj;
}