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

Commit dd9906d

Browse files
committed
Merge pull request #89 from nik-kor/feature/basic-tests
feat(tests): create a basic test for the loader
2 parents 8abafa8 + b1aaabf commit dd9906d

File tree

7 files changed

+154
-1
lines changed

7 files changed

+154
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ dist.babel
55
tscommand
66
npm-debug.log
77
.awcache
8+
test/output

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"scripts": {
77
"grunt": "grunt",
88
"prepublish": "grunt",
9-
"babel": "babel dist --presets es2015 --out-dir dist.babel --watch"
9+
"babel": "babel dist --presets es2015 --out-dir dist.babel --watch",
10+
"test": "mocha"
1011
},
1112
"author": "Stanislav Panferov <[email protected]> (http://panferov.me/)",
1213
"repository": {
@@ -38,9 +39,11 @@
3839
"tsconfig": "^2.1.1"
3940
},
4041
"devDependencies": {
42+
"babel-cli": "^6.3.17",
4143
"babel-preset-es2015": "^6.1.2",
4244
"babel-preset-es2015-node4": "^1.0.0",
4345
"babel-preset-stage-2": "^6.1.2",
46+
"expect": "^1.13.4",
4447
"git-hooks": "0.0.10",
4548
"grunt": "^0.4.5",
4649
"grunt-bump": "^0.3.1",
@@ -51,6 +54,9 @@
5154
"grunt-shell": "^1.1.2",
5255
"grunt-ts": "^3.0.0",
5356
"load-grunt-tasks": "^0.6.0",
57+
"mkdirp": "^0.5.1",
58+
"mocha": "^2.3.4",
59+
"rimraf": "^2.5.0",
5460
"typescript": "^1.8.0-dev.20151202"
5561
}
5662
}

test/fixtures/basic.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class HiThere {
2+
constructor(a: number, b: string) {
3+
let t = a + b;
4+
}
5+
}

test/fixtures/basic.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Component {
2+
render() {
3+
return <div>hi there</div>;
4+
}
5+
}

test/fixtures/tsconfig.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"declaration": false,
5+
"noImplicitAny": false,
6+
"removeComments": true,
7+
"jsx": "react",
8+
"noLib": false,
9+
"preserveConstEnums": true,
10+
"experimentalDecorators": true,
11+
"suppressImplicitAnyIndexErrors": true,
12+
"experimentalAsyncFunctions": true
13+
},
14+
"exclude": [
15+
"node_modules",
16+
"bower_components"
17+
]
18+
}

test/fixtures/with-type-errors.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class IamAClass {
2+
constructor(a: number) {
3+
this.doSomething(a);
4+
}
5+
6+
doSomething(c: boolean) {}
7+
}

test/index.js

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
var webpack = require('webpack');
2+
var path = require('path');
3+
var mkdirp = require('mkdirp');
4+
var rimraf = require('rimraf');
5+
var expect = require('expect');
6+
7+
var outputDir = path.resolve(__dirname, './output/');
8+
var loader = path.resolve(__dirname, '../dist.babel');
9+
var fs = require('fs');
10+
var Promise = require('bluebird');
11+
var readFile = Promise.promisify(fs.readFile);
12+
13+
var globalConfig = {
14+
module: {
15+
loaders: [
16+
{
17+
test: /\.ts?/,
18+
loader: loader + '?-doTypeCheck',
19+
},
20+
],
21+
}
22+
};
23+
24+
describe('main test', function() {
25+
beforeEach(function(done) {
26+
rimraf(outputDir, function(err) {
27+
if (err) { return done(err); }
28+
mkdirp(outputDir, done);
29+
});
30+
});
31+
32+
it('should be ok', function(done) {
33+
var filename = 'basic.js';
34+
var outputFile = path.resolve(outputDir, filename);
35+
var config = {
36+
output: {
37+
path: outputDir,
38+
filename: filename,
39+
},
40+
entry: './test/fixtures/basic.ts',
41+
};
42+
var testStringParts = [
43+
'var HiThere = (function () {',
44+
'function HiThere(a, b) {',
45+
'var t = a + b;',
46+
'return HiThere;'
47+
];
48+
49+
webpack(Object.assign(globalConfig, config), function(err, stats) {
50+
expect(err).toNotExist();
51+
expect(stats.compilation.errors.length).toBe(0);
52+
readFile(outputFile).then(function(data) {
53+
var res = data.toString();
54+
testStringParts.forEach(function(p) {
55+
expect(res.indexOf(p)).toNotEqual(-1);
56+
});
57+
done();
58+
});
59+
});
60+
});
61+
62+
it('should check typing', function(done) {
63+
var config = {
64+
output: {
65+
path: outputDir
66+
},
67+
entry: './test/fixtures/with-type-errors.ts',
68+
module: {
69+
loaders: [
70+
{
71+
test: /\.ts?/,
72+
loader: loader + '?doTypeCheck',
73+
},
74+
],
75+
}
76+
};
77+
78+
webpack(config, function(err, stats) {
79+
expect(stats.compilation.errors).toExist();
80+
done();
81+
});
82+
});
83+
84+
it('should load tsx files and use tsconfig', function(done) {
85+
var tsConfig = path.resolve(__dirname, 'fixtures/tsconfig.json');
86+
var outputFilename = 'basic.jsx';
87+
var config = {
88+
entry: './test/fixtures/basic.tsx',
89+
module: {
90+
loaders: [
91+
{
92+
test: /\.tsx?/,
93+
loader: loader + `?tsconfig=${tsConfig}`
94+
},
95+
],
96+
},
97+
output: {
98+
path: outputDir,
99+
filename: 'basic.jsx',
100+
},
101+
};
102+
103+
webpack(config, function(err, stats) {
104+
readFile(path.resolve(outputDir, outputFilename)).then(function(res) {
105+
var testString = 'return React.createElement("div", null, "hi there");';
106+
expect(res.toString().indexOf(testString)).toNotEqual(-1);
107+
done();
108+
});
109+
});
110+
});
111+
});

0 commit comments

Comments
 (0)