Use tsconfig-paths for easier access to the source

Currently, tests only can import source code modules through relative
paths `../../`. This makes it very difficult to refactor and organize
tests in folders as the paths change.

[tsconfig-paths](https://www.npmjs.com/package/tsconfig-paths) allows to
reference the source through an alias defined in the "paths" section of
tsconfig.json
This commit is contained in:
Felipe Lalanne 2022-08-17 18:31:16 -04:00
parent 1439ee30f2
commit 71eaa3327e
5 changed files with 42 additions and 2 deletions

31
package-lock.json generated
View File

@ -9760,6 +9760,12 @@
"ansi-regex": "^2.0.0" "ansi-regex": "^2.0.0"
} }
}, },
"strip-bom": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz",
"integrity": "sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==",
"dev": true
},
"strip-final-newline": { "strip-final-newline": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/strip-final-newline/-/strip-final-newline-2.0.0.tgz", "resolved": "https://registry.npmjs.org/strip-final-newline/-/strip-final-newline-2.0.0.tgz",
@ -10446,6 +10452,31 @@
} }
} }
}, },
"tsconfig-paths": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-4.1.0.tgz",
"integrity": "sha512-AHx4Euop/dXFC+Vx589alFba8QItjF+8hf8LtmuiCwHyI4rHXQtOOENaM8kvYf5fR0dRChy3wzWIZ9WbB7FWow==",
"dev": true,
"requires": {
"json5": "^2.2.1",
"minimist": "^1.2.6",
"strip-bom": "^3.0.0"
},
"dependencies": {
"json5": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz",
"integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==",
"dev": true
},
"minimist": {
"version": "1.2.6",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.6.tgz",
"integrity": "sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==",
"dev": true
}
}
},
"tslib": { "tslib": {
"version": "1.9.3", "version": "1.9.3",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz",

View File

@ -119,6 +119,7 @@
"tmp": "^0.1.0", "tmp": "^0.1.0",
"ts-loader": "^7.0.5", "ts-loader": "^7.0.5",
"ts-node": "^8.10.2", "ts-node": "^8.10.2",
"tsconfig-paths": "^4.1.0",
"typed-error": "^3.2.1", "typed-error": "^3.2.1",
"typescript": "^4.2.4", "typescript": "^4.2.4",
"webpack": "^4.44.1", "webpack": "^4.44.1",

View File

@ -5,6 +5,7 @@ module.exports = {
require: [ require: [
// Files to execute before running suites // Files to execute before running suites
'ts-node/register/transpile-only', 'ts-node/register/transpile-only',
'tsconfig-paths/register',
'test/config/fixtures.ts', 'test/config/fixtures.ts',
], ],
spec: ['test/**/*.spec.ts'], spec: ['test/**/*.spec.ts'],

View File

@ -1,5 +1,5 @@
import { expect } from 'chai'; import { expect } from 'chai';
import constants = require('../src/lib/constants'); import * as constants from '~/src-lib/constants';
describe('constants', function () { describe('constants', function () {
it('has the correct configJsonPathOnHost', () => it('has the correct configJsonPathOnHost', () =>

View File

@ -1,5 +1,6 @@
{ {
"compilerOptions": { "compilerOptions": {
"baseUrl": "./",
"target": "ES2019", "target": "ES2019",
"module": "commonjs", "module": "commonjs",
"strict": true, "strict": true,
@ -11,7 +12,13 @@
"skipLibCheck": true, "skipLibCheck": true,
"lib": ["es2019"], "lib": ["es2019"],
"resolveJsonModule": true, "resolveJsonModule": true,
"allowJs": true "allowJs": true,
"paths": {
"~/src/*": ["src/*"],
"~/lib/*": ["src/lib/*"],
"~/test-lib/*": ["test/lib/*"],
"~/test-data/*": ["test/data/*"]
}
}, },
"include": ["src/**/*", "test/**/*", "typings/**/*.d.ts"] "include": ["src/**/*", "test/**/*", "typings/**/*.d.ts"]
} }