-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
node.config.spec.ts
137 lines (120 loc) · 4.17 KB
/
node.config.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { ProjectGraph, ExecutorContext } from '@nrwl/devkit';
import { join } from 'path';
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
import { GeneratePackageJsonWebpackPlugin } from './generate-package-json-webpack-plugin';
import { getNodeWebpackConfig } from './node.config';
import { BuildNodeBuilderOptions } from './types';
jest.mock('tsconfig-paths-webpack-plugin');
jest.mock('@nrwl/devkit', () => ({
get workspaceRoot() {
return join(__dirname, '../../../..');
},
}));
describe('getNodePartial', () => {
let context: ExecutorContext;
let projectGraph: ProjectGraph;
let input: BuildNodeBuilderOptions;
beforeEach(() => {
context = {
projectName: 'sample-project',
root: '/root',
cwd: '',
target: {} as any,
isVerbose: false,
workspace: {} as any,
};
projectGraph = {} as any;
input = {
main: 'main.ts',
outputPath: 'dist',
tsConfig: 'tsconfig.json',
externalDependencies: 'all',
fileReplacements: [],
statsJson: false,
};
(<any>TsconfigPathsPlugin).mockImplementation(
function MockPathsPlugin() {}
);
});
describe('unconditionally', () => {
it('should target commonjs', () => {
const result = getNodeWebpackConfig(context, projectGraph, input);
expect(result.output.libraryTarget).toEqual('commonjs');
});
it('should target node', () => {
const result = getNodeWebpackConfig(context, projectGraph, input);
expect(result.target).toEqual('node');
});
it('should not polyfill node apis', () => {
const result = getNodeWebpackConfig(context, projectGraph, input);
expect(result.node).toEqual(false);
});
});
describe('the optimization option when true', () => {
it('should minify', () => {
const result = getNodeWebpackConfig(context, projectGraph, {
...input,
optimization: true,
});
expect(result.optimization.minimize).toEqual(true);
expect(result.optimization.minimizer).toBeDefined();
});
it('should concatenate modules', () => {
const result = getNodeWebpackConfig(context, projectGraph, {
...input,
optimization: true,
});
expect(result.optimization.concatenateModules).toEqual(true);
});
});
describe('the externalDependencies option', () => {
it('should change all node_modules to commonjs imports', () => {
const result = getNodeWebpackConfig(context, projectGraph, input);
const callback = jest.fn();
result.externals[0](null, '@nestjs/core', callback);
expect(callback).toHaveBeenCalledWith(null, 'commonjs @nestjs/core');
});
it('should change given module names to commonjs imports but not others', () => {
const result = getNodeWebpackConfig(context, projectGraph, {
...input,
externalDependencies: ['module1'],
});
const callback = jest.fn();
result.externals[0]({ request: 'module1' }, callback);
expect(callback).toHaveBeenCalledWith(null, 'commonjs module1');
result.externals[0]({ request: '@nestjs/core' }, callback);
expect(callback).toHaveBeenCalledWith();
});
it('should not change any modules to commonjs imports', () => {
const result = getNodeWebpackConfig(context, projectGraph, {
...input,
externalDependencies: 'none',
});
expect(result.externals).not.toBeDefined();
});
});
describe('the generatePackageJson option', () => {
it('should add the GeneratePackageJsonWebpackPlugin plugin', () => {
const result = getNodeWebpackConfig(context, projectGraph, {
...input,
generatePackageJson: true,
});
expect(
result.plugins.find(
(plugin) => plugin instanceof GeneratePackageJsonWebpackPlugin
)
).toBeTruthy();
});
it('should not add the GeneratePackageJsonWebpackPlugin plugin', () => {
const result = getNodeWebpackConfig(context, projectGraph, {
...input,
generatePackageJson: false,
});
expect(
result.plugins.find(
(plugin) => plugin instanceof GeneratePackageJsonWebpackPlugin
)
).toBeFalsy();
});
});
});