Skip to content

Commit

Permalink
fix: make mocha self-contained with the source map support
Browse files Browse the repository at this point in the history
This is a follow-up to #884.
The changes make it impossible to run 'npm test' for individual packages.

- There is no local mocha command for each package and it depends on the
  global installation of 'mocha'
- With the global mocha, --require source-map-support/register cannot be
  resolved.

The PR adds 'lb-mocha' command so that we can support source-map-support
correctly along with other options. No global mocha installation is needed.
  • Loading branch information
raymondfeng committed Jan 25, 2018
1 parent b03bd60 commit 56fc63b
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 39 deletions.
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
"@types/node": "^8.5.9",
"coveralls": "^3.0.0",
"cz-conventional-changelog": "^2.1.0",
"lerna": "^2.5.1",
"mocha": "^5.0.0",
"source-map-support": "^0.5.2"
"lerna": "^2.8.0"
},
"scripts": {
"bootstrap": "npm i && lerna bootstrap",
Expand All @@ -41,7 +39,7 @@
"build:full": "npm run clean:full && npm run bootstrap && npm run build && npm run mocha && npm run lint",
"pretest": "npm run clean && npm run build:current",
"test": "node packages/build/bin/run-nyc npm run mocha",
"mocha": "node packages/build/bin/select-dist mocha --opts packages/build/mocha.ts.opts \"packages/*/DIST/test/**/*.js\" \"packages/cli/test/*.js\"",
"mocha": "node packages/build/bin/run-mocha --opts packages/build/mocha.ts.opts \"packages/*/DIST/test/**/*.js\" \"packages/cli/test/*.js\"",
"posttest": "npm run lint"
},
"config": {
Expand Down
8 changes: 4 additions & 4 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": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/acceptance/**/*.js'",
"acceptance": "lb-mocha 'DIST/test/acceptance/**/*.js'",
"build": "npm run build:dist && npm run build:dist6",
"build:current": "lb-tsc",
"build:dist": "lb-tsc es2017",
"build:dist6": "lb-tsc es2015",
"build:apidocs": "lb-apidocs",
"clean": "lb-clean loopback-authentication*.tgz dist dist6 package api-docs",
"integration": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/integration/**/*.js'",
"integration": "lb-mocha 'DIST/test/integration/**/*.js'",
"prepare": "npm run build && npm run build:apidocs",
"pretest": "npm run build:current",
"test": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/unit/**/*.js' 'DIST/test/integration/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/unit/**/*.js'",
"test": "lb-mocha 'DIST/test/unit/**/*.js' 'DIST/test/integration/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-mocha 'DIST/test/unit/**/*.js'",
"verify": "npm pack && tar xf loopback-authentication*.tgz && tree package && npm run clean"
},
"author": "IBM",
Expand Down
8 changes: 4 additions & 4 deletions packages/build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ To use `@loopback/build` for your package:
"clean": "lb-clean loopback-grpc*.tgz dist*",
"prepare": "npm run build && npm run build:apidocs",
"pretest": "npm run build:current",
"acceptance": "lb-dist mocha --opts ../../test/mocha.opts 'DIST/test/acceptance/**/*.js'",
"integration": "lb-dist mocha --opts ../../test/mocha.opts 'DIST/test/integration/**/*.js'",
"test": "lb-dist mocha --opts ../../test/mocha.opts 'DIST/test/unit/**/*.js' 'DIST/test/integration/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-dist mocha --opts ../../test/mocha.opts 'DIST/test/unit/**/*.js'",
"acceptance": "lb-mocha --opts ../../test/mocha.opts 'DIST/test/acceptance/**/*.js'",
"integration": "lb-mocha --opts ../../test/mocha.opts 'DIST/test/integration/**/*.js'",
"test": "lb-mocha --opts ../../test/mocha.opts 'DIST/test/unit/**/*.js' 'DIST/test/integration/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-mocha --opts ../../test/mocha.opts 'DIST/test/unit/**/*.js'",
"verify": "npm pack && tar xf loopback-grpc*.tgz && tree package && npm run clean"
},
```
Expand Down
40 changes: 40 additions & 0 deletions packages/build/bin/run-mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env node
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: @loopback/build
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

/*
========
Usage:
node ./bin/run-mocha
========
*/

'use strict';

function run(argv, dryRun) {
const utils = require('./utils');
const path = require('path');

// Resolve source-map-support so that the path can be used by mocha
const sourceMapRegisterPath = require.resolve('source-map-support/register');

// Substitute the DIST variable with the dist folder
const dist = utils.getDistribution();
const mochaOpts = argv.slice(2).map(a => a.replace(/\bDIST\b/g, dist));

// Add default options
if (mochaOpts.indexOf('--opts') === -1) {
const optsPath = require.resolve('../mocha.ts.opts');
mochaOpts.unshift('--opts', optsPath);
}
const args = ['--require', sourceMapRegisterPath, ...mochaOpts];

return utils.runCLI('mocha/bin/mocha', args, dryRun);
}

module.exports = run;
if (require.main === module) run(process.argv);
1 change: 0 additions & 1 deletion packages/build/mocha.ts.opts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
--recursive
--exit
--reporter dot
--require source-map-support/register
4 changes: 3 additions & 1 deletion packages/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"mocha": "^5.0.0",
"nyc": "^11.4.1",
"prettier": "^1.10.2",
"source-map-support": "^0.5.3",
"strong-docs": "^1.6.0",
"tslint": "^5.9.1",
"typescript": "^2.6.2"
Expand All @@ -28,6 +29,7 @@
"lb-tsc": "./bin/compile-package.js",
"lb-tslint": "./bin/run-tslint.js",
"lb-prettier": "./bin/run-prettier.js",
"lb-mocha": "./bin/run-mocha.js",
"lb-nyc": "./bin/run-nyc.js",
"lb-dist": "./bin/select-dist.js",
"lb-apidocs": "./bin/generate-apidocs.js",
Expand All @@ -40,7 +42,7 @@
"prettier:check": "npm run prettier:cli -- -l",
"prettier:fix": "npm run prettier:cli -- --write",
"test": "npm run mocha",
"mocha": "node bin/select-dist mocha --opts mocha.js.opts --timeout 30000 \"test/integration/*.js\"",
"mocha": "node bin/select-dist node bin/run-mocha --opts mocha.js.opts --timeout 30000 \"test/integration/*.js\"",
"posttest": "npm run lint"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/generators/project/templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<% } -%>
"pretest": "npm run clean && npm run build",
<% if (project.mocha) { -%>
"test": "lb-dist mocha DIST/test",
"test": "lb-mocha 'DIST/test'",
<% } -%>
<% if (project.prettier || project.tslint) { -%>
"posttest": "npm run lint",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/clone-example.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('cloneExampleFromGitHub', function() {
return Promise.all([
glob('**', {
cwd: path.join(__dirname, `../../example-${VALID_EXAMPLE}`),
ignore: '@(node_modules|dist*)/**',
ignore: '@(node_modules|dist*|api-docs)/**',
}),
glob('**', {
cwd: outDir,
Expand Down
6 changes: 3 additions & 3 deletions packages/context/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"node": ">=6"
},
"scripts": {
"acceptance": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/acceptance/**/*.js'",
"acceptance": "lb-mocha 'DIST/test/acceptance/**/*.js'",
"build": "npm run build:dist && npm run build:dist6",
"build:current": "lb-tsc",
"build:dist": "lb-tsc es2017",
Expand All @@ -15,8 +15,8 @@
"clean": "lb-clean loopback-context*.tgz dist dist6 package api-docs",
"prepare": "npm run build && npm run build:apidocs",
"pretest": "npm run build:current",
"test": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/unit/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/unit/**/*.js'",
"test": "lb-mocha 'DIST/test/unit/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-mocha 'DIST/test/unit/**/*.js'",
"verify": "npm pack && tar xf loopback-context*.tgz && tree package && npm run clean"
},
"author": "IBM",
Expand Down
8 changes: 4 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"node": ">=6"
},
"scripts": {
"acceptance": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/acceptance/**/*.js'",
"acceptance": "lb-mocha 'DIST/test/acceptance/**/*.js'",
"build": "npm run build:dist && npm run build:dist6",
"build:current": "lb-tsc",
"build:dist": "lb-tsc es2017",
Expand All @@ -15,9 +15,9 @@
"clean": "lb-clean loopback-core*.tgz dist dist6 package api-docs",
"prepare": "npm run build && npm run build:apidocs",
"pretest": "npm run build:current",
"integration": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/integration/**/*.js'",
"test": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/unit/**/*.js' 'DIST/test/integration/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/unit/**/*.js'",
"integration": "lb-mocha 'DIST/test/integration/**/*.js'",
"test": "lb-mocha 'DIST/test/unit/**/*.js' 'DIST/test/integration/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-mocha 'DIST/test/unit/**/*.js'",
"verify": "npm pack && tar xf loopback-core*.tgz && tree package && npm run clean"
},
"author": "IBM",
Expand Down
6 changes: 3 additions & 3 deletions packages/example-getting-started/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"node": ">=8"
},
"scripts": {
"acceptance": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/acceptance/**/*.js'",
"acceptance": "lb-mocha 'DIST/test/acceptance/**/*.js'",
"build": "npm run build:dist && npm run build:dist6",
"build:current": "lb-tsc",
"build:dist": "lb-tsc es2017",
Expand All @@ -17,8 +17,8 @@
"clean": "lb-clean loopback-getting-started*.tgz dist dist6 package api-docs",
"prepare": "npm run build && npm run build:apidocs",
"pretest": "npm run build:current",
"test": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/unit/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/unit/**/*.js'",
"test": "lb-mocha 'DIST/test/unit/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-mocha 'DIST/test/unit/**/*.js'",
"verify": "npm pack && tar xf loopback-getting-started*.tgz && tree package && npm run clean",
"start": "npm run build && node ."
},
Expand Down
6 changes: 3 additions & 3 deletions packages/metadata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"node": ">=6"
},
"scripts": {
"acceptance": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/acceptance/**/*.js'",
"acceptance": "lb-mocha 'DIST/test/acceptance/**/*.js'",
"build": "npm run build:dist && npm run build:dist6",
"build:current": "lb-tsc",
"build:dist": "lb-tsc es2017",
Expand All @@ -15,8 +15,8 @@
"clean": "lb-clean loopback-metadata*.tgz dist dist6 package api-docs",
"prepare": "npm run build && npm run build:apidocs",
"pretest": "npm run build:current",
"test": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/unit/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/unit/**/*.js'",
"test": "lb-mocha 'DIST/test/unit/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-mocha 'DIST/test/unit/**/*.js'",
"verify": "npm pack && tar xf loopback-metadata*.tgz && tree package && npm run clean"
},
"author": "IBM",
Expand Down
4 changes: 3 additions & 1 deletion packages/openapi-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@loopback/openapi-spec-builder": "^4.0.0-alpha.17",
"@loopback/repository": "^4.0.0-alpha.23",
"@loopback/testlab": "^4.0.0-alpha.19",
"@types/debug": "0.0.30",
"@types/lodash": "^4.14.96"
},
"scripts": {
Expand All @@ -21,7 +22,7 @@
"clean": "lb-clean loopback-openapi-v2*.tgz dist* package",
"prepare": "npm run build && npm run build:apidocs",
"pretest": "npm run build:current",
"test": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/unit/**/*.js'",
"test": "lb-mocha 'DIST/test/unit/**/*.js'",
"verify": "npm pack && tar xf loopback-openapi-v2*.tgz && tree package && npm run clean"
},
"author": "IBM",
Expand Down Expand Up @@ -52,6 +53,7 @@
"@loopback/context": "^4.0.0-alpha.26",
"@loopback/openapi-spec": "^4.0.0-alpha.20",
"@loopback/repository-json-schema": "^4.0.0-alpha.1",
"debug": "^3.1.0",
"lodash": "^4.17.4"
}
}
2 changes: 1 addition & 1 deletion packages/repository-json-schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"clean": "lb-clean loopback-json-schema*.tgz dist dist6 package api-docs",
"prepare": "npm run build && npm run build:apidocs",
"pretest": "npm run build:current",
"test": "lb-dist mocha --opts node_modules/@loopback/build/mocha.opts 'dist/test/unit/**/*.js' 'dist/test/integration/**/*.js' 'dist/test/acceptance/**/*.js'",
"test": "lb-mocha 'dist/test/unit/**/*.js' 'dist/test/integration/**/*.js' 'dist/test/acceptance/**/*.js'",
"verify": "npm pack && tar xf loopback-json-schema*.tgz && tree package && npm run clean"
},
"author": "IBM",
Expand Down
6 changes: 3 additions & 3 deletions packages/repository/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"main": "index",
"scripts": {
"acceptance": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/acceptance/**/*.js'",
"acceptance": "lb-mocha 'DIST/test/acceptance/**/*.js'",
"build": "npm run build:dist && npm run build:dist6",
"build:current": "lb-tsc",
"build:dist": "lb-tsc es2017",
Expand All @@ -16,8 +16,8 @@
"clean": "lb-clean loopback-repository*.tgz dist dist6 package api-docs",
"prepare": "npm run build && npm run build:apidocs",
"pretest": "npm run build:current",
"test": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/unit/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/unit/**/*.js'",
"test": "lb-mocha 'DIST/test/unit/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-mocha 'DIST/test/unit/**/*.js'",
"verify": "npm pack && tar xf loopback-juggler*.tgz && tree package && npm run clean"
},
"author": "IBM",
Expand Down
8 changes: 4 additions & 4 deletions packages/rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"node": ">=6"
},
"scripts": {
"acceptance": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/acceptance/**/*.js'",
"acceptance": "lb-mocha 'DIST/test/acceptance/**/*.js'",
"build": "npm run build:dist && npm run build:dist6",
"build:current": "lb-tsc",
"build:dist": "lb-tsc es2017",
Expand All @@ -15,9 +15,9 @@
"clean": "lb-clean loopback-rest*.tgz dist dist6 package api-docs",
"prepare": "npm run build && npm run build:apidocs",
"pretest": "npm run build:current",
"integration": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/integration/**/*.js'",
"test": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/unit/**/*.js' 'DIST/test/integration/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts 'DIST/test/unit/**/*.js'",
"integration": "lb-mocha 'DIST/test/integration/**/*.js'",
"test": "lb-mocha 'DIST/test/unit/**/*.js' 'DIST/test/integration/**/*.js' 'DIST/test/acceptance/**/*.js'",
"unit": "lb-mocha 'DIST/test/unit/**/*.js'",
"verify": "npm pack && tar xf loopback-rest*.tgz && tree package && npm run clean"
},
"author": "IBM",
Expand Down
2 changes: 1 addition & 1 deletion packages/testlab/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"clean": "lb-clean loopback-testlab*.tgz dist dist6 package api-docs",
"prepare": "npm run build",
"pretest": "npm run build:current",
"test": "lb-dist mocha --opts node_modules/@loopback/build/mocha.ts.opts DIST/test",
"test": "lb-mocha 'DIST/test'",
"verify": "npm pack && tar xf loopback-testlab*.tgz && tree package && npm run clean"
},
"author": "IBM",
Expand Down

0 comments on commit 56fc63b

Please sign in to comment.