Skip to content
This repository was archived by the owner on Dec 1, 2019. It is now read-only.

feat(tests): create a basic test for the loader #89

Merged
merged 2 commits into from
Jan 4, 2016
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist.babel
tscommand
npm-debug.log
.awcache
test/output
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]> (http://panferov.me/)",
"repository": {
Expand Down Expand Up @@ -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",
Expand All @@ -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"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class HiThere {
constructor(a: number, b: string) {
let t = a + b;
}
}
5 changes: 5 additions & 0 deletions test/fixtures/basic.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Component {
render() {
return <div>hi there</div>;
}
}
18 changes: 18 additions & 0 deletions test/fixtures/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
7 changes: 7 additions & 0 deletions test/fixtures/with-type-errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class IamAClass {
constructor(a: number) {
this.doSomething(a);
}

doSomething(c: boolean) {}
}
111 changes: 111 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
});