Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: fix: 🐛 tsconfig-paths support #1040

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion bin/_lab
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

'use strict';

require('tsconfig-paths/register');

// console.log(process.cwd());

const Util = require('util');
const Cpr = require('cpr');
const Rimraf = require('rimraf');


if (process.env.ROOT_SPAWN) {
(async () => {

Expand Down
2 changes: 2 additions & 0 deletions bin/lab
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

'use strict';

require('tsconfig-paths/register');

const inspectPattern = /^(--inspect|--inspect-brk)(=\d+)?$/;
const inspectArg = process.argv.find((argument) => {

Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@
},
"peerDependencies": {
"@hapi/eslint-plugin": "^5.1.0",
"typescript": ">=3.6.5"
"typescript": ">=3.6.5",
"tsconfig-paths": ">=3.14.1"
},
"peerDependenciesMeta": {
"typescript": {
"optional": true
},
"tsconfig-paths": {
"optional": true
}
},
"devDependencies": {
Expand All @@ -52,7 +56,8 @@
"lab-event-reporter": "1.x.x",
"rimraf": "3.x.x",
"semver": "7.x.x",
"typescript": "^4.5.4"
"typescript": "^4.5.4",
"tsconfig-paths": "^3.14.1"
},
"bin": {
"lab": "./bin/lab"
Expand Down
12 changes: 12 additions & 0 deletions test/cli_tsconfig_paths/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { expect } from '@hapi/code';
import * as _Lab from '../../test_runner';

const { describe, it } = exports.lab = _Lab.script();

describe('Test CLI', () => {

it('adds two numbers together', () => {

expect(1 + 1).to.equal(4);
});
});
6 changes: 6 additions & 0 deletions test/cli_tsconfig_paths/really/super/nested/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './types';

export function add(a: number, b: number) {

return a + b;
}
22 changes: 22 additions & 0 deletions test/cli_tsconfig_paths/simple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'tsconfig-paths/register';

import { expect } from '@hapi/code';
import * as _Lab from '../../test_runner';

import { add } from 'nested/api';


const { describe, it } = exports.lab = _Lab.script();

describe('Test CLI', () => {

it('adds two numbers together', () => {

expect(add(1, 1)).to.equal(2);
});

it('subtracts two numbers', () => {

expect(add(2, - 2)).to.equal(0);
});
});
15 changes: 15 additions & 0 deletions test/cli_tsconfig_paths/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "es2021",
"module": "commonjs",
"moduleResolution": "node",
"baseUrl": ".",
"paths": {
"nested/*": ["./really/super/nested/*"]
}
},
"include": [
"./*",
"../../node_modules"
]
}
1 change: 1 addition & 0 deletions test/cli_tsconfig_paths/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Test = string;
71 changes: 71 additions & 0 deletions test/tsconfig-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict';

const Fs = require('fs');
const Path = require('path');

const Code = require('@hapi/code');
const _Lab = require('../test_runner');
const RunCli = require('./run_cli');
const Typescript = require('../lib/modules/typescript');


const internals = {
cwd: process.cwd()
};


const { describe, it, afterEach } = exports.lab = _Lab.script();
const expect = Code.expect;

const cwd = Path.join(__dirname, 'cli_tsconfig_paths');

describe('TypeScript Paths', () => {

afterEach(() => process.chdir(internals.cwd));

it('supports TypeScript', async () => {

process.chdir(cwd);

const result = await RunCli(['simple.ts', '-m', '2000', '--typescript'], cwd);
expect(result.errorOutput).to.equal('');
expect(result.code).to.equal(0);
expect(result.output).to.contain('2 tests complete');
});

it('handles errors', async () => {

process.chdir(cwd);
const result = await RunCli(['error.ts', '-m', '2000', '--typescript'], cwd);
expect(result.errorOutput).to.equal('');
expect(result.code).to.equal(1);
expect(result.output).to.contain('error.ts:10:26');
});

it('supports coverage', async () => {

process.chdir(cwd);
const result = await RunCli(['simple.ts', '-m', '2000', '-t', '100', '--typescript'], cwd);

expect(result.errorOutput).to.equal('');
expect(result.code).to.equal(0);
expect(result.output).to.contain('2 tests complete');
expect(result.output).to.contain('Coverage: 100.00%');
});

describe('transform', () => {

it('generates embedded sourcemap with sourcesContent', () => {

const smre = /\/\/\#\s*sourceMappingURL=data:application\/json[^,]+base64,(.*)\r?\n?$/;
const path = Path.join(__dirname, 'cli_tsconfig_paths', 'simple.ts');
const transformed = Typescript.extensions[0].transform(Fs.readFileSync(path, { encoding: 'utf8' }), path);
const matches = smre.exec(transformed);
expect(matches).to.exist();
const sourcemap = JSON.parse(Buffer.from(matches[1], 'base64').toString());
expect(sourcemap.sources).to.equal(['simple.ts']);
expect(sourcemap.sourcesContent).to.exist();
expect(sourcemap.sourcesContent).to.have.length(1);
});
});
});