Skip to content

Commit

Permalink
build: remove ts-node dev-dependency
Browse files Browse the repository at this point in the history
Refactor project infrastructure to transpile test files together with
production code and then run the tests from plain javascript code.

Benefits:

 - consistent error message parseable by VSCode's $tsc problem matcher
   (compiler errors from test files will show in Problems window now)

 - earlier feedback about type-related problems in test files,
   no need to wait for ts-node to load type information from
   tsc output

 - slightly faster red-green-refactor cycle,
   `npm test` takes 27s instead of 32s.

 - we are opening doors to eventually start using `tsc --watch`
   once TypeScript adds support for multi-project repositories,
   see microsoft/TypeScript#3469
  • Loading branch information
bajtos committed Oct 5, 2017
1 parent b6ce426 commit 0b49bad
Show file tree
Hide file tree
Showing 48 changed files with 203 additions and 121 deletions.
4 changes: 2 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
packages/*/lib
packages/*/lib6
packages/*/dist
packages/*/dist6
packages/*/api-docs
4 changes: 2 additions & 2 deletions bin/compile-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ if (!target) {
let outDir;
switch (target) {
case 'es2017':
outDir = 'lib';
outDir = 'dist';
break;
case 'es2015':
outDir = 'lib6';
outDir = 'dist6';
break;
default:
console.error('Unknown build target %s. Supported values: es2015, es2017');
Expand Down
48 changes: 48 additions & 0 deletions bin/select-dist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright IBM Corp. 2013,2017. All Rights Reserved.
// Node module: loopback-next
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

/*
========
Usage:
node ../../bin/select-dist command [arguments...]
The script will scan all arguments (including the command) and replace
the string DIST with either dist or dist6, depending on the current
Node.js version.
Then the provided command is executed with the modified arguments.
Example usage:
node ../../bin/select-dist mocha DIST/test
========
*/

'use strict';

const path = require('path');
const spawn = require('child_process').spawn;

const nodeMajorVersion = +process.versions.node.split('.')[0];
const dist = nodeMajorVersion >= 7 ? 'dist' : 'dist6';

const args = process.argv.slice(2).map(a => a.replace(/\bDIST\b/g, dist));
const command = args.shift();

console.log(command, args.join(' '));

spawn(
command,
args,
{
stdio: 'inherit',
// On Windows, npm creates `.cmd` files instead of symlinks in
// `node_modules/.bin` folder. These files cannot be executed directly,
// only via a shell.
shell: true,
}
).on('close', (number, signal) => (process.exitCode = number));
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"node": ">=6"
},
"license": "MIT",
"dependencies": {},
"devDependencies": {
"@commitlint/cli": "^3.2.0",
"@commitlint/config-angular": "^3.1.1",
Expand All @@ -26,7 +27,6 @@
"request": "^2.79.0",
"request-promise": "^4.1.1",
"strong-docs": "^1.4.0",
"ts-node": "^3.3.0",
"tslint": "^5.7.0",
"typescript": "^2.5.2"
},
Expand All @@ -47,7 +47,7 @@
"build:current": "lerna run --loglevel=silent build:current",
"pretest": "npm run build:current",
"test": "nyc npm run mocha",
"mocha": "mocha --opts test/mocha.opts \"packages/*/test/**/*.ts\"",
"mocha": "node bin/select-dist mocha --opts test/mocha.opts \"packages/*/DIST/test/**/*.js\"",
"posttest": "npm run lint"
},
"nyc": {
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*.tgz
lib*
dist*
package
2 changes: 1 addition & 1 deletion packages/authentication/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './lib';
export * from './dist/src';
4 changes: 2 additions & 2 deletions packages/authentication/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

const nodeMajorVersion = +process.versions.node.split('.')[0];
module.exports = nodeMajorVersion >= 7 ?
require('./lib') :
require('./lib6');
require('./dist/src') :
require('./dist6/src');
20 changes: 10 additions & 10 deletions packages/authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
"node": ">=6"
},
"scripts": {
"acceptance": "mocha --opts ../../test/mocha.opts 'test/acceptance/**/*.ts'",
"build": "npm run build:lib && npm run build:lib6",
"acceptance": "node ../../bin/select-dist mocha --opts ../../test/mocha.opts 'DIST/test/acceptance/**/*.js'",
"build": "npm run build:dist && npm run build:dist6",
"build:current": "node ../../bin/compile-package",
"build:lib": "node ../../bin/compile-package es2017",
"build:lib6": "node ../../bin/compile-package es2015",
"build:dist": "node ../../bin/compile-package es2017",
"build:dist6": "node ../../bin/compile-package es2015",
"build:apidocs": "node ../../bin/generate-apidocs",
"clean": "rm -rf loopback-authentication*.tgz lib* package",
"integration": "mocha --opts ../../test/mocha.opts 'test/integration/**/*.ts'",
"clean": "rm -rf loopback-authentication*.tgz dist* package",
"integration": "node ../../bin/select-dist mocha --opts ../../test/mocha.opts 'DIST/test/integration/**/*.js'",
"prepublish": "npm run build && npm run build:apidocs",
"pretest": "npm run build:current",
"test": "mocha --opts ../../test/mocha.opts 'test/unit/**/*.ts' 'test/integration/**/*.ts' 'test/acceptance/**/*.ts'",
"unit": "mocha --opts ../../test/mocha.opts 'test/unit/**/*.ts'",
"test": "node ../../bin/select-dist mocha --opts ../../test/mocha.opts 'DIST/test/unit/**/*.js' 'DIST/test/integration/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "node bin/select-dist mocha --opts ../../test/mocha.opts 'DIST/test/unit/**/*.js'",
"verify": "npm pack && tar xf loopback-authentication*.tgz && tree package && npm run clean"
},
"author": "IBM",
Expand All @@ -44,8 +44,8 @@
"README.md",
"index.js",
"index.d.ts",
"lib",
"lib6",
"dist/src",
"dist6/src",
"api-docs"
],
"repository": {
Expand Down
6 changes: 5 additions & 1 deletion packages/authentication/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "../../tsconfig.common.json",
"compilerOptions": {
"rootDir": "."
},
"include": [
"src"
"src",
"test"
]
}
2 changes: 1 addition & 1 deletion packages/context/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*.tgz
lib*
dist*
package
2 changes: 1 addition & 1 deletion packages/context/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './lib';
export * from './dist/src';
4 changes: 2 additions & 2 deletions packages/context/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

const nodeMajorVersion = +process.versions.node.split('.')[0];
module.exports = nodeMajorVersion >= 7 ?
require('./lib') :
require('./lib6');
require('./dist/src') :
require('./dist6/src');
18 changes: 9 additions & 9 deletions packages/context/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
"node": ">=6"
},
"scripts": {
"acceptance": "mocha --opts ../../test/mocha.opts 'test/acceptance/**/*.ts'",
"build": "npm run build:lib && npm run build:lib6",
"acceptance": "node ../../bin/select-dist mocha --opts ../../test/mocha.opts 'DIST/test/acceptance/**/*.js'",
"build": "npm run build:dist && npm run build:dist6",
"build:current": "node ../../bin/compile-package",
"build:lib": "node ../../bin/compile-package es2017",
"build:lib6": "node ../../bin/compile-package es2015",
"build:dist": "node ../../bin/compile-package es2017",
"build:dist6": "node ../../bin/compile-package es2015",
"build:apidocs": "node ../../bin/generate-apidocs",
"clean": "rm -rf loopback-context*.tgz lib* package",
"clean": "rm -rf loopback-context*.tgz dist* package",
"prepublish": "npm run build && npm run build:apidocs",
"pretest": "npm run build:current",
"test": "mocha --opts ../../test/mocha.opts 'test/unit/**/*.ts' 'test/acceptance/**/*.ts'",
"unit": "mocha --opts ../../test/mocha.opts 'test/unit/**/*.ts'",
"test": "node ../../bin/select-dist mocha --opts ../../test/mocha.opts 'DIST/test/unit/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "node ../../bin/select-dist mocha --opts ../../test/mocha.opts 'DIST/test/unit/**/*.js'",
"verify": "npm pack && tar xf loopback-context*.tgz && tree package && npm run clean"
},
"author": "IBM",
Expand All @@ -42,8 +42,8 @@
"README.md",
"index.js",
"index.d.ts",
"lib",
"lib6",
"dist/src",
"dist6/src",
"api-docs"
],
"repository": {
Expand Down
6 changes: 5 additions & 1 deletion packages/context/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "../../tsconfig.common.json",
"compilerOptions": {
"rootDir": "."
},
"include": [
"src"
"src",
"test"
]
}
2 changes: 1 addition & 1 deletion packages/core/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*.tgz
lib*
dist*
package
2 changes: 1 addition & 1 deletion packages/core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './lib';
export * from './dist/src';
4 changes: 2 additions & 2 deletions packages/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

const nodeMajorVersion = +process.versions.node.split('.')[0];
module.exports = nodeMajorVersion >= 7 ?
require('./lib') :
require('./lib6');
require('./dist/src') :
require('./dist6/src');
20 changes: 10 additions & 10 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
"node": ">=6"
},
"scripts": {
"acceptance": "mocha --opts ../../test/mocha.opts 'test/acceptance/**/*.ts'",
"build": "npm run build:lib && npm run build:lib6",
"acceptance": "node ../../bin/select-dist mocha --opts ../../test/mocha.opts 'DIST/test/acceptance/**/*.js'",
"build": "npm run build:dist && npm run build:dist6",
"build:current": "node ../../bin/compile-package",
"build:lib": "node ../../bin/compile-package es2017",
"build:lib6": "node ../../bin/compile-package es2015",
"build:dist": "node ../../bin/compile-package es2017",
"build:dist6": "node ../../bin/compile-package es2015",
"build:apidocs": "node ../../bin/generate-apidocs",
"clean": "rm -rf loopback-core*.tgz lib* package",
"clean": "rm -rf loopback-core*.tgz dist* package",
"prepublish": "npm run build && npm run build:apidocs",
"pretest": "npm run build:current",
"integration": "mocha --opts ../../test/mocha.opts 'test/integration/**/*.ts'",
"test": "mocha --opts ../../test/mocha.opts 'test/unit/**/*.ts' 'test/integration/**/*.ts' 'test/acceptance/**/*.ts'",
"unit": "mocha --opts ../../test/mocha.opts 'test/unit/**/*.ts'",
"integration": "node ../../bin/select-dist mocha --opts ../../test/mocha.opts 'DIST/test/integration/**/*.js'",
"test": "node ../../bin/select-dist mocha --opts ../../test/mocha.opts 'DIST/test/unit/**/*.js' 'DIST/test/integration/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "node ../../bin/select-dist mocha --opts ../../test/mocha.opts 'DIST/test/unit/**/*.js'",
"verify": "npm pack && tar xf loopback-core*.tgz && tree package && npm run clean"
},
"author": "IBM",
Expand All @@ -33,8 +33,8 @@
"README.md",
"index.js",
"index.d.ts",
"lib",
"lib6",
"dist/src",
"dist6/src",
"api-docs"
],
"repository": {
Expand Down
6 changes: 5 additions & 1 deletion packages/core/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "../../tsconfig.common.json",
"compilerOptions": {
"rootDir": "."
},
"include": [
"src"
"src",
"test"
]
}
11 changes: 6 additions & 5 deletions packages/example-codehub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
},
"private": true,
"scripts": {
"acceptance": "mocha --opts ../../test/mocha.opts 'test/acceptance/**/*.ts'",
"integration": "mocha --opts ../../test/mocha.opts 'test/integration/**/*.ts'",
"test": "mocha --opts ../../test/mocha.opts 'test/unit/**/*.ts' 'test/integration/**/*.ts' 'test/acceptance/**/*.ts'",
"unit": "mocha --opts ../../test/mocha.opts 'test/unit/**/*.ts'"
"acceptance": "mocha 'test/acceptance/**/*.ts'",
"integration": "mocha 'test/integration/**/*.ts'",
"test": "mocha 'test/unit/**/*.ts' 'test/integration/**/*.ts' 'test/acceptance/**/*.ts'",
"unit": "mocha 'test/unit/**/*.ts'"
},
"author": "IBM",
"license": "MIT",
Expand All @@ -19,6 +19,7 @@
"@loopback/rest": "^4.0.0-alpha.3"
},
"devDependencies": {
"@loopback/testlab": "^4.0.0-alpha.9"
"@loopback/testlab": "^4.0.0-alpha.9",
"ts-node": "^3.3.0"
}
}
2 changes: 2 additions & 0 deletions packages/example-codehub/test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--compilers ts:ts-node/register
--recursive
2 changes: 1 addition & 1 deletion packages/openapi-spec-builder/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*.tgz
lib*
dist*
package
2 changes: 1 addition & 1 deletion packages/openapi-spec-builder/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './lib';
export * from './dist/src';
4 changes: 2 additions & 2 deletions packages/openapi-spec-builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

const nodeMajorVersion = +process.versions.node.split('.')[0];
module.exports = nodeMajorVersion >= 7 ?
require('./lib') :
require('./lib6');
require('./dist/src') :
require('./dist6/src');
12 changes: 6 additions & 6 deletions packages/openapi-spec-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"node": ">=6"
},
"scripts": {
"build": "npm run build:lib && npm run build:lib6",
"build": "npm run build:dist && npm run build:dist6",
"build:current": "node ../../bin/compile-package",
"build:lib": "node ../../bin/compile-package es2017",
"build:lib6": "node ../../bin/compile-package es2015",
"build:dist": "node ../../bin/compile-package es2017",
"build:dist6": "node ../../bin/compile-package es2015",
"build:apidocs": "node ../../bin/generate-apidocs",
"clean": "rm -rf loopback-openapi-spec*.tgz lib* package",
"clean": "rm -rf loopback-openapi-spec*.tgz dist* package",
"prepublish": "npm run build && npm run build:apidocs",
"verify": "npm pack && tar xf loopback-openapi-spec*.tgz && tree package && npm run clean"
},
Expand All @@ -31,8 +31,8 @@
"README.md",
"index.js",
"index.d.ts",
"lib",
"lib6",
"dist/src",
"dist6/src",
"api-docs"
],
"repository": {
Expand Down
6 changes: 5 additions & 1 deletion packages/openapi-spec-builder/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "../../tsconfig.common.json",
"compilerOptions": {
"rootDir": "."
},
"include": [
"src"
"src",
"test"
]
}
2 changes: 1 addition & 1 deletion packages/openapi-spec/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*.tgz
lib*
dist*
package
2 changes: 1 addition & 1 deletion packages/openapi-spec/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './lib';
export * from './dist/src';
Loading

0 comments on commit 0b49bad

Please sign in to comment.