Skip to content

Commit

Permalink
Added speculative test of allowJsImportTypes scenario (should initial…
Browse files Browse the repository at this point in the history
…ly fail until fix is applied) (#590)

* Added speculative test of allowJsImportTypes scenario

* correct test to reference entry

* added noEmitOnError to trigger error

* no longer depends on @types/jasmine so can be made to run back with 1.8.2

* go for a 1.8.2 friendly tsconfig.json

* Resolved filenames that are definition file from node_modules are treated as isExternalLibraryImport

Suggested by @bsouthga - let's see if this fixes our broken test.

* match backslashes or forward slashes

* Just run test against 2.1+
  • Loading branch information
johnnyreilly authored Jul 26, 2017
1 parent beaa2b2 commit 1b33393
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/servicesHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ function resolveModuleName(
isExternalLibraryImport: tsResolution.resolvedModule.isExternalLibraryImport
};
if (resolutionResult!) {
if (resolutionResult!.resolvedFileName === tsResolutionResult.resolvedFileName) {
if (resolutionResult!.resolvedFileName === tsResolutionResult.resolvedFileName ||
/node_modules(\\|\/).*\.d\.ts$/.test(tsResolutionResult.resolvedFileName)) {
resolutionResult!.isExternalLibraryImport = tsResolutionResult.isExternalLibraryImport;
}
} else {
Expand Down
3 changes: 3 additions & 0 deletions test/execution-tests/2.1.4_babel-allowJsImportTypes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This test confirms that you can import typings from an npm module when `allowJs` is true. See details [#586](https://github.com/TypeStrong/ts-loader/issues/586).

This test is roughly based on https://github.com/bsouthga/ts-loader-types-error-example
51 changes: 51 additions & 0 deletions test/execution-tests/2.1.4_babel-allowJsImportTypes/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* eslint-disable no-var, strict */
'use strict';
var webpackConfig = require('./webpack.config.js');

module.exports = function(config) {
// Documentation: https://karma-runner.github.io/0.13/config/configuration-file.html
config.set({
browsers: [ 'PhantomJS' ],

files: [
// This ensures we have the es6 shims in place and then loads all the tests
'main.js'
],

port: 9876,

frameworks: [ 'jasmine' ],

logLevel: config.LOG_INFO, //config.LOG_DEBUG

preprocessors: {
'main.js': [ 'webpack', 'sourcemap' ]
},

webpack: {
devtool: 'inline-source-map',
module: webpackConfig.module,
resolve: webpackConfig.resolve,

// for test harness purposes only, you would not need this in a normal project
resolveLoader: webpackConfig.resolveLoader
},

webpackMiddleware: {
quiet: true,
stats: {
colors: true
}
},

// reporter options
mochaReporter: {
colors: {
success: 'bgGreen',
info: 'cyan',
warning: 'bgBlue',
error: 'bgRed'
}
}
});
};
4 changes: 4 additions & 0 deletions test/execution-tests/2.1.4_babel-allowJsImportTypes/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'babel-polyfill';

const testsContext = require.context('./', true, /\.tests\.js$/);
testsContext.keys().forEach(testsContext);
16 changes: 16 additions & 0 deletions test/execution-tests/2.1.4_babel-allowJsImportTypes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "allow-js-import-types",
"version": "1.0.0",
"main": "index.js",
"devDependencies": {
"babel": "^6.0.0",
"babel-core": "^6.0.0",
"babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.0.0",
"jasmine-core": "^2.3.4"
},
"dependencies": {
"babel-polyfill": "^6.0.0",
"event-rank": "^0.0.10"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import EventRank from './imported';

export function getEventRank() {
return EventRank;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { EventRank } from 'event-rank';

export default EventRank;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var entry = require('../src/entry');

describe("entry", function() {
it("getEventRank produces something", function() {
var EventRank = entry.getEventRank();
expect(EventRank).not.toBeUndefined();
});
});
11 changes: 11 additions & 0 deletions test/execution-tests/2.1.4_babel-allowJsImportTypes/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"allowJs": true,
"module": "es2015",
"moduleResolution": "node",
"noEmitOnError": true,
"noImplicitAny": false,
"sourceMap": true,
"target": "es2015"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* eslint-disable no-var, strict, prefer-arrow-callback */
'use strict';

var path = require('path');
var webpack = require('webpack');

var babelOptions = {
"presets": [
[
"es2015",
{
"modules": false
}
]
]
};

module.exports = {
entry: './src/entry.js',
output: {
filename: 'bundle.js'
},
module: {
rules: [{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: babelOptions
},
{
loader: 'ts-loader',
options: { entryFileIsJs: true }
}
]
}, {
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: babelOptions
}
]
}]
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: ['.ts', '.tsx', '.js']
},
};

// for test harness purposes only, you would not need this in a normal project
module.exports.resolveLoader = { alias: { 'ts-loader': path.join(__dirname, "../../../index.js") } }

0 comments on commit 1b33393

Please sign in to comment.