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

Commit 9aed17f

Browse files
author
Nikita Korotkih
committed
feat(tests): create a basic test for the loader
1 parent 8abafa8 commit 9aed17f

File tree

7 files changed

+151
-1
lines changed

7 files changed

+151
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
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

Lines changed: 7 additions & 1 deletion
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 <fnight.m@gmail.com> (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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 18 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
entry: './test/fixtures/with-type-errors.ts',
65+
module: {
66+
loaders: [
67+
{
68+
test: /\.ts?/,
69+
loader: loader + '?doTypeCheck',
70+
},
71+
],
72+
}
73+
};
74+
75+
webpack(config, function(err, stats) {
76+
expect(stats.compilation.errors).toExist();
77+
done();
78+
});
79+
});
80+
81+
it('should load tsx files and use tsconfig', function(done) {
82+
var tsConfig = path.resolve(__dirname, 'fixtures/tsconfig.json');
83+
var outputFilename = 'basic.jsx';
84+
var config = {
85+
entry: './test/fixtures/basic.tsx',
86+
module: {
87+
loaders: [
88+
{
89+
test: /\.tsx?/,
90+
loader: loader + `?tsconfig=${tsConfig}`
91+
},
92+
],
93+
},
94+
output: {
95+
path: outputDir,
96+
filename: 'basic.jsx',
97+
},
98+
};
99+
100+
webpack(config, function(err, stats) {
101+
readFile(path.resolve(outputDir, outputFilename)).then(function(res) {
102+
var testString = 'return React.createElement("div", null, "hi there");';
103+
expect(res.toString().indexOf(testString)).toNotEqual(-1);
104+
done();
105+
});
106+
});
107+
});
108+
});

0 commit comments

Comments
 (0)