/
languageSupport.test.ts
90 lines (81 loc) · 3.79 KB
/
languageSupport.test.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// eslint-disable-next-line node/no-missing-import
import language, { LangType, getBabelLoader, getTypescriptLoader } from '../../lib/utils/languageSupport';
// eslint-disable-next-line node/no-missing-import
import { CustomGenerator } from '../../lib/types';
// TODO: enable after jest release
describe.skip('languageSupport', () => {
const getMockGenerator = (): CustomGenerator => {
const gen = new CustomGenerator(null, null);
gen.entryOption = "'./path/to/index.js'";
gen.dependencies = [];
gen.configuration = {
config: {
webpackOptions: {
module: {
rules: [],
},
},
},
};
return gen;
};
it('getBabelLoader', () => {
const rule = getBabelLoader(['test1', 'test2']);
expect(rule.loader).toEqual("'babel-loader'");
expect(rule.include).toEqual(["path.resolve(__dirname, 'test1')", "path.resolve(__dirname, 'test2')"]);
});
it('getTypescriptLoader', () => {
const rule = getTypescriptLoader(['test1', 'test2']);
expect(rule.loader).toEqual("'ts-loader'");
expect(rule.include).toEqual(["path.resolve(__dirname, 'test1')", "path.resolve(__dirname, 'test2')"]);
});
it('works with ES6', () => {
const gen = getMockGenerator();
language(gen, LangType.ES6);
expect(gen.entryOption).toEqual("'./path/to/index.js'");
expect(gen.dependencies).toEqual(['babel-loader', '@babel/core', '@babel/preset-env']);
const rules = gen.configuration.config.webpackOptions.module.rules;
expect(rules.length).toEqual(1);
expect(rules[0].include).toEqual(["path.resolve(__dirname, 'path/to')"]);
});
it('works with TypeScript for single entry', () => {
const gen = getMockGenerator();
language(gen, LangType.Typescript);
// this helper preserves the original js entryOption but updates the
// webpack config to use ts
expect(gen.entryOption).toEqual("'./path/to/index.js'");
expect(gen.configuration.config.webpackOptions.entry).toEqual("'./path/to/index.ts'");
expect(gen.dependencies).toEqual(['typescript', 'ts-loader']);
expect(gen.configuration.config.webpackOptions.module.rules.length).toEqual(1);
const rules = gen.configuration.config.webpackOptions.module.rules;
expect(rules.length).toEqual(1);
expect(rules[0].include).toEqual(["path.resolve(__dirname, 'path/to')"]);
});
it('works with TypeScript for multi entry', () => {
const gen = getMockGenerator();
gen.entryOption = {
test1: "'./path/to/test1.js'",
test2: "'./path/to/test2.js'",
test3: "'./different/path/to/test3.js'",
};
language(gen, LangType.Typescript);
// this helper preserves the original js entryOption but updates the
// webpack config to use ts
expect(gen.entryOption).toEqual({
test1: "'./path/to/test1.js'",
test2: "'./path/to/test2.js'",
test3: "'./different/path/to/test3.js'",
});
expect(gen.configuration.config.webpackOptions.entry).toEqual({
test1: "'./path/to/test1.ts'",
test2: "'./path/to/test2.ts'",
test3: "'./different/path/to/test3.ts'",
});
expect(gen.dependencies).toEqual(['typescript', 'ts-loader']);
expect(gen.configuration.config.webpackOptions.module.rules.length).toEqual(1);
const rules = gen.configuration.config.webpackOptions.module.rules;
expect(rules.length).toEqual(1);
// duplicate include paths are removed
expect(rules[0].include).toEqual(["path.resolve(__dirname, 'path/to')", "path.resolve(__dirname, 'different/path/to')"]);
});
});