diff --git a/.gitignore b/.gitignore index b03887d..21c5b4a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ dist.babel tscommand npm-debug.log .awcache +test/output diff --git a/package.json b/package.json index 0e7cc7d..fb1072f 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "scripts": { "grunt": "grunt", "prepublish": "grunt", - "babel": "babel dist --presets es2015 --out-dir dist.babel --watch" + "babel": "babel dist --presets es2015 --out-dir dist.babel --watch", + "test": "mocha" }, "author": "Stanislav Panferov (http://panferov.me/)", "repository": { @@ -38,9 +39,11 @@ "tsconfig": "^2.1.1" }, "devDependencies": { + "babel-cli": "^6.3.17", "babel-preset-es2015": "^6.1.2", "babel-preset-es2015-node4": "^1.0.0", "babel-preset-stage-2": "^6.1.2", + "expect": "^1.13.4", "git-hooks": "0.0.10", "grunt": "^0.4.5", "grunt-bump": "^0.3.1", @@ -51,6 +54,9 @@ "grunt-shell": "^1.1.2", "grunt-ts": "^3.0.0", "load-grunt-tasks": "^0.6.0", + "mkdirp": "^0.5.1", + "mocha": "^2.3.4", + "rimraf": "^2.5.0", "typescript": "^1.8.0-dev.20151202" } } diff --git a/test/fixtures/basic.ts b/test/fixtures/basic.ts new file mode 100644 index 0000000..254c1fe --- /dev/null +++ b/test/fixtures/basic.ts @@ -0,0 +1,5 @@ +class HiThere { + constructor(a: number, b: string) { + let t = a + b; + } +} diff --git a/test/fixtures/basic.tsx b/test/fixtures/basic.tsx new file mode 100644 index 0000000..17ffa8f --- /dev/null +++ b/test/fixtures/basic.tsx @@ -0,0 +1,5 @@ +class Component { + render() { + return
hi there
; + } +} diff --git a/test/fixtures/tsconfig.json b/test/fixtures/tsconfig.json new file mode 100644 index 0000000..c4832b8 --- /dev/null +++ b/test/fixtures/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "target": "es6", + "declaration": false, + "noImplicitAny": false, + "removeComments": true, + "jsx": "react", + "noLib": false, + "preserveConstEnums": true, + "experimentalDecorators": true, + "suppressImplicitAnyIndexErrors": true, + "experimentalAsyncFunctions": true + }, + "exclude": [ + "node_modules", + "bower_components" + ] +} diff --git a/test/fixtures/with-type-errors.ts b/test/fixtures/with-type-errors.ts new file mode 100644 index 0000000..e2aaaff --- /dev/null +++ b/test/fixtures/with-type-errors.ts @@ -0,0 +1,7 @@ +class IamAClass { + constructor(a: number) { + this.doSomething(a); + } + + doSomething(c: boolean) {} +} diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..df3a500 --- /dev/null +++ b/test/index.js @@ -0,0 +1,111 @@ +var webpack = require('webpack'); +var path = require('path'); +var mkdirp = require('mkdirp'); +var rimraf = require('rimraf'); +var expect = require('expect'); + +var outputDir = path.resolve(__dirname, './output/'); +var loader = path.resolve(__dirname, '../dist.babel'); +var fs = require('fs'); +var Promise = require('bluebird'); +var readFile = Promise.promisify(fs.readFile); + +var globalConfig = { + module: { + loaders: [ + { + test: /\.ts?/, + loader: loader + '?-doTypeCheck', + }, + ], + } +}; + +describe('main test', function() { + beforeEach(function(done) { + rimraf(outputDir, function(err) { + if (err) { return done(err); } + mkdirp(outputDir, done); + }); + }); + + it('should be ok', function(done) { + var filename = 'basic.js'; + var outputFile = path.resolve(outputDir, filename); + var config = { + output: { + path: outputDir, + filename: filename, + }, + entry: './test/fixtures/basic.ts', + }; + var testStringParts = [ + 'var HiThere = (function () {', + 'function HiThere(a, b) {', + 'var t = a + b;', + 'return HiThere;' + ]; + + webpack(Object.assign(globalConfig, config), function(err, stats) { + expect(err).toNotExist(); + expect(stats.compilation.errors.length).toBe(0); + readFile(outputFile).then(function(data) { + var res = data.toString(); + testStringParts.forEach(function(p) { + expect(res.indexOf(p)).toNotEqual(-1); + }); + done(); + }); + }); + }); + + it('should check typing', function(done) { + var config = { + output: { + path: outputDir + }, + entry: './test/fixtures/with-type-errors.ts', + module: { + loaders: [ + { + test: /\.ts?/, + loader: loader + '?doTypeCheck', + }, + ], + } + }; + + webpack(config, function(err, stats) { + expect(stats.compilation.errors).toExist(); + done(); + }); + }); + + it('should load tsx files and use tsconfig', function(done) { + var tsConfig = path.resolve(__dirname, 'fixtures/tsconfig.json'); + var outputFilename = 'basic.jsx'; + var config = { + entry: './test/fixtures/basic.tsx', + module: { + loaders: [ + { + test: /\.tsx?/, + loader: loader + `?tsconfig=${tsConfig}` + }, + ], + }, + output: { + path: outputDir, + filename: 'basic.jsx', + }, + }; + + webpack(config, function(err, stats) { + readFile(path.resolve(outputDir, outputFilename)).then(function(res) { + var testString = 'return React.createElement("div", null, "hi there");'; + expect(res.toString().indexOf(testString)).toNotEqual(-1); + done(); + }); + }); + }); +});