Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore test files in webpack with ts-loader

I have a project that runs the test files named "*.test.ts" with jest and ts-jest. This is fine but when I launch webpack, I get errors for the test files:

ERROR in /some/path/note.test.ts
(27,3): error TS2304: Cannot find name '

How can I fix the webpack config to totally ignore test files ?

Here is the webpack config:

const path = require ( 'path' )

module.exports =
{ entry: './src/boot.tsx'
, node:
  { __dirname: false
  }
, output:
  { path: path.resolve ( __dirname, 'app', 'build' )
  , filename: 'app.js'
  , publicPath: '/live-reload/'
  }
, devtool: 'source-map'
, resolve:
  { extensions: ['', '.js', '.ts', '.tsx']
  }
, module:
  { loaders:
    [ { test: /\.tsx?$/
      , exclude: /node_modules/
      , loader: 'ts-loader'
      }
    ]
  }
}

[EDIT]

I used the test function to log seen files and the test files do not appear in there, so the problem is not on webpack including the wrong files but on typescript misbehaving (because tests work fine with jest).

like image 212
Anna B Avatar asked Dec 20 '16 09:12

Anna B


People also ask

Does TS-loader use Tsconfig?

First, for ts-loader to produce sourcemaps, you will need to set the tsconfig.

What is exclude in webpack?

Actually, those 'include' and 'exclude' properties are telling the loaders whether to include/exclude the files described (such as the contents of node_modules ), not webpack itself. So the 'excluded' modules you import from node_modules will be bundled - but they won't be transformed by babel.

Can webpack config be Ts?

Of course, because TypeScript is just a superset for Javascript, you can always use TypeScript to write your webpack. config. ts and convert it to a valid Javascript webpack.

Does webpack use TSC?

Like Babel, Webpack depends on TSC to transpile TypeScript to JavaScript but as TSC doesn't have a clue about Webpack, hence Webpack needs a loader to talk to TSC. This is where the ts-loader comes into play. Webpack compiles a TypeScript file using ts-loader package which asks TSC to do the compilation.


2 Answers

I ended up resolving this too, but in a different way, namely, adding the following settings to my .tsconfig file:

"include": [
    "./lib/**/*"
],
"exclude": [
    "./lib/__tests__"
]

Not sure why this didn't come up for the OP and company. Perhaps they already had those settings in their .tslint file (e.g. because their project had been generated or forked from another project).

But it looks like TypeScript (at least, as of 2.3.2) pays attention to these settings and ignores equivalent settings in webconfig.config.js.

like image 59
Jonathan Avatar answered Sep 18 '22 14:09

Jonathan


You can specify a function instead of the regular expression for the loader test property; this can give you a more control.

test: function (modulePath) {
  return modulePath.endsWith('.ts') && !modulePath.endsWith('test.ts');
}
like image 43
Acidic Avatar answered Sep 17 '22 14:09

Acidic