Skip to content
Merged
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: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
**/package-lock.json
**/npm-shrinkwrap.json

coverage.*
results.*
/coverage.*
/results.*

**/.DS_Store
**/._*
Expand All @@ -17,4 +17,3 @@ results.*
!test/cli_coverage/node_modules
test_runner
test/cli/test/leaks.js

24 changes: 23 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,27 @@ version: ~> 1.0

import:
- hapijs/ci-config-travis:node_js.yml@main
- hapijs/ci-config-travis:install.yml@main
- hapijs/ci-config-travis:os.yml@main

env:
- TYPESCRIPT="latest"

install:
- "npm install"
- "npm install typescript@$TYPESCRIPT"

jobs:
include:
- os: "linux"
node: "node"
env: TYPESCRIPT="3.6"
- os: "linux"
node: "node"
env: TYPESCRIPT="rc"
- os: "linux"
node: "node"
env: TYPESCRIPT="next"
allow_failures:
- os: "linux"
node: "node"
env: TYPESCRIPT="next"
9 changes: 4 additions & 5 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ const Bossy = require('@hapi/bossy');
const FindRc = require('find-rc');
const Hoek = require('@hapi/hoek');

const Coverage = require('./coverage');
const Modules = require('./modules');
const Pkg = require('../package.json');
const Runner = require('./runner');
const Transform = require('./transform');

// .labrc configuration will be required if it exists
// index.js required below if setting assertions module
Expand All @@ -36,10 +35,10 @@ exports.run = function () {
settings.lintingPath = process.cwd();

if (settings.coverage) {
Coverage.instrument(settings);
Modules.coverage.instrument(settings);
}
else if (settings.transform) {
Transform.install(settings);
Modules.transform.install(settings);
}

if (settings.environment) {
Expand All @@ -51,7 +50,7 @@ exports.run = function () {

if (settings.transform) {
sourceMapOptions = {
retrieveFile: Transform.retrieveFile
retrieveFile: Modules.transform.retrieveFile
};
}

Expand Down
14 changes: 6 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

const Hoek = require('@hapi/hoek');

const Coverage = require('./coverage');
const Leaks = require('./leaks');
const Modules = require('./modules');
const Runner = require('./runner');
const Types = require('./types');


const internals = {};
Expand All @@ -15,13 +13,13 @@ exports.report = Runner.report;

exports.execute = Runner.execute;

exports.coverage = Coverage;

exports.leaks = Leaks;
exports.assertions = null; // Set by the -a command line option

exports.types = Types;
// Inject modules (with lazy loading)

exports.assertions = null; // Set by the -a command line option
for (const module of ['coverage', 'leaks', 'types']) {
Object.defineProperty(exports, module, Object.getOwnPropertyDescriptor(Modules, module));
}


/*
Expand Down
2 changes: 1 addition & 1 deletion lib/coverage.js → lib/modules/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const BabelESLint = require('babel-eslint');
const SourceMap = require('source-map');
const SourceMapSupport = require('source-map-support');

const Eslintrc = require('./linter/.eslintrc');
const Eslintrc = require('../linter/.eslintrc');
const Transform = require('./transform');


Expand Down
23 changes: 23 additions & 0 deletions lib/modules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const internals = {
list: ['coverage', 'leaks', 'lint', 'transform', 'types']
};


internals.addModule = function (module) {

Object.defineProperty(exports, module, {
configurable: true,
enumerable: true,
get() {

return require(`./${module}`);
}
});
};


for (const module of internals.list) {
internals.addModule(module);
}
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/lint.js → lib/modules/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Fs = require('fs');


const internals = {
linter: __dirname + '/linter/index.js'
linter: __dirname + '/../linter/index.js'
};


Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions lib/types.js → lib/modules/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Globby = require('globby');
const Hoek = require('@hapi/hoek');
const Ts = require('typescript');

const Utils = require('./utils');
const Utils = require('../utils');


const internals = {
Expand All @@ -25,7 +25,8 @@ const internals = {
// Codes from https://github.com/microsoft/TypeScript/blob/master/src/compiler/diagnosticMessages.json

skip: [
1308 // Await is only allowed in async function
1308, // Await is only allowed in async function
1378 // Top-level 'await' expressions are only allowed...
],

report: [
Expand Down
13 changes: 5 additions & 8 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ const Hoek = require('@hapi/hoek');
const Seedrandom = require('seedrandom');
const WillCall = require('will-call');

const Modules = require('./modules');
const Reporters = require('./reporters');
const Coverage = require('./coverage');
const Linters = require('./lint');
const Leaks = require('./leaks');
const Types = require('./types');


const internals = {};
Expand Down Expand Up @@ -71,15 +68,15 @@ exports.report = async function (scripts, options) {
const result = await exports.execute(scripts, settings, reporter);

if (settings.leaks) {
result.leaks = Leaks.detect(settings.globals);
result.leaks = Modules.leaks.detect(settings.globals);
}

if (settings.coverage) {
result.coverage = await Coverage.analyze(settings);
result.coverage = await Modules.coverage.analyze(settings);
}

if (settings.types) {
result.types = await Types.validate(settings);
result.types = await Modules.types.validate(settings);
}

if (settings.shuffle) {
Expand Down Expand Up @@ -107,7 +104,7 @@ exports.report = async function (scripts, options) {

const executeLint = async function () {

return settings.lint && await Linters.lint(settings);
return settings.lint && await Modules.lint.lint(settings);
};

const results = await Promise.all([executeScripts(), executeLint()]);
Expand Down
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,24 @@
"source-map": "0.7.x",
"source-map-support": "0.5.x",
"supports-color": "7.x.x",
"typescript": "3.6.x",
"will-call": "1.x.x"
},
"peerDependencies": {
"typescript": ">=3.6.5"
},
"peerDependenciesMeta": {
"typescript": {
"optional": true
}
},
"devDependencies": {
"@hapi/lab-external-module-test": "1.x.x",
"@hapi/code": "8.x.x",
"@hapi/lab-external-module-test": "1.x.x",
"cpr": "3.x.x",
"lab-event-reporter": "1.x.x",
"rimraf": "3.x.x",
"semver": "7.x.x"
"semver": "7.x.x",
"typescript": "~4.0.2"
},
"bin": {
"lab": "./bin/lab"
Expand Down
2 changes: 1 addition & 1 deletion test/linters.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Fs = require('fs');
const Path = require('path');
const _Lab = require('../test_runner');
const Code = require('@hapi/code');
const Linters = require('../lib/lint');
const Linters = require('../lib/modules/lint');


// Test shortcuts
Expand Down
34 changes: 34 additions & 0 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -1849,4 +1849,38 @@ describe('Runner', () => {

notebook.tests.forEach(assertNulledContext);
});

it('runs when typescript fails to load', async () => {

const desc = Object.getOwnPropertyDescriptor(_Lab, 'types');
try {
Object.defineProperty(_Lab, 'types', {
configurable: true,
enumerable: true,
get() {

throw new Error(`Cannot find module 'typescript'`);
}
});

const script = Lab.script();

script.test('a', () => {

return true;
});

const res1 = await Lab.report(script, { output: false, assert: false, types: true });

expect(res1.code).to.equal(1);
expect(res1.output).to.contain([`Cannot find module 'typescript'`]);

const res2 = await Lab.report(script, { output: false, assert: false, types: false });
expect(res2.code).to.equal(0);
expect(res2.output).to.not.contain([`Cannot find module 'typescript'`]);
}
finally {
Object.defineProperty(_Lab, 'types', desc);
}
});
});
2 changes: 1 addition & 1 deletion test/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Path = require('path');
const Code = require('@hapi/code');
const _Lab = require('../test_runner');
const Lab = require('../');
const Transform = require('../lib/transform');
const Transform = require('../lib/modules/transform');


// Declare internals
Expand Down
38 changes: 12 additions & 26 deletions test/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,56 +59,42 @@ describe('Types', () => {

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

const stripDetails = function (info) {

info.message = info.message.split('\n')[0];
return info;
};

process.chdir(Path.join(__dirname, 'types', 'errors'));
const errors = await Lab.types.validate();
expect(errors).to.only.contain([
expect(errors.map(stripDetails)).to.only.contain([
{
filename: 'test/index.ts',
message: 'No overload matches this call.\n' +
` Overload 1 of 2, '(a: string, b: string): string', gave the following error.\n` +
` Argument of type '1' is not assignable to parameter of type 'string'.\n` +
` Overload 2 of 2, '(a: number, b: number): number', gave the following error.\n` +
` Argument of type '"x"' is not assignable to parameter of type 'number'.`,
message: 'No overload matches this call.',
line: 3,
column: 0
},
{
filename: 'test/index.ts',
message: 'No overload matches this call.\n' +
` Overload 1 of 2, '(a: string, b: string): string', gave the following error.\n` +
` Argument of type '1' is not assignable to parameter of type 'string'.\n` +
` Overload 2 of 2, '(a: number, b: number): number', gave the following error.\n` +
` Argument of type '"x"' is not assignable to parameter of type 'number'.`,
message: 'No overload matches this call.',
line: 4,
column: 0
},
{
filename: 'test/nested.ts',
message: 'No overload matches this call.\n' +
` Overload 1 of 2, '(a: string, b: string): string', gave the following error.\n` +
` Argument of type '1' is not assignable to parameter of type 'string'.\n` +
` Overload 2 of 2, '(a: number, b: number): number', gave the following error.\n` +
` Argument of type 'true' is not assignable to parameter of type 'number'.`,
message: 'No overload matches this call.',
line: 8,
column: 0
},
{
filename: 'test/nested.ts',
message: 'No overload matches this call.\n' +
` Overload 1 of 2, '(a: string, b: string): string', gave the following error.\n` +
` Argument of type '1' is not assignable to parameter of type 'string'.\n` +
` Overload 2 of 2, '(a: number, b: number): number', gave the following error.\n` +
` Argument of type 'true' is not assignable to parameter of type 'number'.`,
message: 'No overload matches this call.',
line: 8,
column: 42
},
{
filename: 'test/other.ts',
message: 'No overload matches this call.\n' +
` Overload 1 of 2, '(a: string, b: string): string', gave the following error.\n` +
` Argument of type 'true' is not assignable to parameter of type 'string'.\n` +
` Overload 2 of 2, '(a: number, b: number): number', gave the following error.\n` +
` Argument of type 'true' is not assignable to parameter of type 'number'.`,
message: 'No overload matches this call.',
line: 3,
column: 4
},
Expand Down