/
node.config.spec.ts
98 lines (83 loc) · 2.81 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
import { join } from 'path';
import { getNodeWebpackConfig } from './node.config';
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
import { BuildNodeBuilderOptions } from './types';
jest.mock('tsconfig-paths-webpack-plugin');
jest.mock('@nrwl/devkit', () => ({
get workspaceRoot() {
return join(__dirname, '../../../..');
},
}));
describe('getNodePartial', () => {
let input: BuildNodeBuilderOptions;
beforeEach(() => {
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(input);
expect(result.output.libraryTarget).toEqual('commonjs');
});
it('should target node', () => {
const result = getNodeWebpackConfig(input);
expect(result.target).toEqual('node');
});
it('should not polyfill node apis', () => {
const result = getNodeWebpackConfig(input);
expect(result.node).toEqual(false);
});
});
describe('the optimization option when true', () => {
it('should minify', () => {
const result = getNodeWebpackConfig({
...input,
optimization: true,
});
expect(result.optimization.minimize).toEqual(true);
expect(result.optimization.minimizer).toBeDefined();
});
it('should concatenate modules', () => {
const result = getNodeWebpackConfig({
...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(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({
...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({
...input,
externalDependencies: 'none',
});
expect(result.externals).not.toBeDefined();
});
});
});