-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.spec.ts
72 lines (66 loc) · 2.9 KB
/
index.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import Bluebird, {promisify} from 'bluebird';
import {resolve} from 'path';
import {Options, tmpName} from 'tmp';
import * as webpack from 'webpack';
import DtsGeneratorPlugin, {IDtsGeneratorPluginOptions} from './index';
interface IStatsJsonAsset {
name: string;
size: number;
chunks: number[];
chunkNames: string[];
emitted: boolean;
isOverSizeLimit: boolean;
}
interface IStatsJson {
assets: IStatsJsonAsset[];
chunks: string[];
warnings: string[];
errors: string[];
}
interface IWebpackResult {
stats: webpack.Stats;
statsJson: IStatsJson;
}
describe ('dtsGeneratorPlugin', () => {
const defaultName: string = 'foo';
let webpackConfig: (dtsGeneratorPluginOptions?: IDtsGeneratorPluginOptions) => Bluebird<IWebpackResult>;
beforeAll(() => {
const tmpNameAsync: (options: Options) => Bluebird<string> =
promisify<string, Options>(tmpName);
const webpackAsync: (configuration: webpack.Configuration) => Bluebird<webpack.Stats> =
promisify<webpack.Stats, webpack.Configuration>(webpack);
const defaultDtsGeneratorPluginOptions: IDtsGeneratorPluginOptions = {
name: defaultName,
project: resolve(__dirname, 'fixtures')
};
webpackConfig = (dtsGeneratorPluginOptions: IDtsGeneratorPluginOptions = defaultDtsGeneratorPluginOptions):
Bluebird<IWebpackResult> =>
tmpNameAsync({dir: resolve(__dirname, '.tmp')})
.then((outputPath: string) => webpackAsync({
entry: resolve(__dirname, 'fixtures/index.ts'),
resolve: {extensions: ['.ts']},
plugins: [new DtsGeneratorPlugin(Object.assign({}, defaultDtsGeneratorPluginOptions, dtsGeneratorPluginOptions))],
module: {rules: [{test: /\.ts$/, loader: 'awesome-typescript-loader', query: {silent: true}}]},
output: {filename: '[name].bundle.js', path: outputPath}
}))
.then((stats: webpack.Stats) => ({stats, statsJson: <IStatsJson> stats.toJson()}));
});
it ('should emit with standard options', () =>
webpackConfig().then(({stats, statsJson}: IWebpackResult) => {
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
expect(statsJson.assets.map((value: IStatsJsonAsset) => value.name))
.toContain(`${defaultName}.d.ts`);
}));
it ('should emit with different simple options', () =>
webpackConfig({
name: 'foobar',
indent: ' ',
main: 'foobar/index'
}).then(({stats, statsJson}: IWebpackResult) => {
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
expect(statsJson.assets.map((value: IStatsJsonAsset) => value.name))
.toContain('foobar.d.ts');
}));
});