-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
155 lines (128 loc) · 4.55 KB
/
index.test.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const path = require('path');
const fs = require('fs');
const rimraf = require('rimraf');
const Bundler = require('parcel-bundler');
const shebangPlugin = require('./index');
const { hasShebang } = require('./utils');
const exampleJsFile = path.join(__dirname, 'mocks/example.js');
const example2JsFile = path.join(__dirname, 'mocks/example2.js');
const exampleWithShebangJsFile = path.join(
__dirname,
'mocks/exampleWithShebang.js'
);
const exampleHtmlFile = path.join(__dirname, 'mocks/example.html');
const parcelConfig = {
target: 'node',
outDir: './test',
watch: false,
cache: false,
logLevel: 2,
sourceMaps: false,
production: true,
};
describe('parcel-plugin-shebang testing', () => {
createBundler = (entryFiles) => new Bundler(entryFiles, parcelConfig);
beforeEach(() => delete process.env['PARCEL_PLUGIN_SHEBANG']);
afterEach(() => rimraf.sync(path.join(__dirname, './test')));
it('should stop plugin process when PARCEL_PLUGIN_SHEBANG environment variable is false', () => {
process.env.PARCEL_PLUGIN_SHEBANG = false;
const bundler = createBundler(exampleJsFile);
const result = shebangPlugin(bundler);
expect(result).toBeFalsy();
});
it('should plugin process for shebang found in entry point', async () => {
const bundler = createBundler(exampleWithShebangJsFile);
const result = shebangPlugin(bundler);
await bundler.bundle();
const content = fs.readFileSync(
path.join(__dirname, './test/exampleWithShebang.js'),
'utf-8'
);
expect(result).toBeTruthy();
expect(hasShebang(content)).toBeTruthy();
});
it('should plugin process for valid parts of config', async () => {
const bundler = createBundler(exampleJsFile);
const result = shebangPlugin(bundler, [
{ test: 'It should not working!' },
{ test2: 'It should not working too!' },
{ interpreter: 'node', files: ['./mocks/example.js'] },
]);
await bundler.bundle();
const content = fs.readFileSync(
path.join(__dirname, './test/example.js'),
'utf-8'
);
expect(result).toBeTruthy();
expect(hasShebang(content)).toBeTruthy();
});
it('should plugin process for multiple entry points', async () => {
const bundler = createBundler([exampleJsFile, example2JsFile]);
const result = shebangPlugin(bundler, [
{
interpreter: 'node',
files: ['./mocks/example.js', './mocks/example2.js'],
},
]);
await bundler.bundle();
const content = fs.readFileSync(
path.join(__dirname, './test/example.js'),
'utf-8'
);
const content2 = fs.readFileSync(
path.join(__dirname, './test/example2.js'),
'utf-8'
);
expect(result).toBeTruthy();
expect(hasShebang(content)).toBeTruthy();
expect(hasShebang(content2)).toBeTruthy();
});
it('should plugin process for multiple entry points and different shebangs', async () => {
const bundler = createBundler([exampleJsFile, example2JsFile]);
const result = shebangPlugin(bundler, [
{ interpreter: 'node', files: ['./mocks/example.js'] },
{ interpreter: 'python3', files: ['./mocks/example2.js'] },
]);
await bundler.bundle();
const content = fs.readFileSync(
path.join(__dirname, './test/example.js'),
'utf-8'
);
const content2 = fs.readFileSync(
path.join(__dirname, './test/example2.js'),
'utf-8'
);
expect(result).toBeTruthy();
expect(hasShebang(content)).toBeTruthy();
expect(hasShebang(content2)).toBeTruthy();
});
it('should not rewrite shebang for entry point', async () => {
const bundler = createBundler(exampleJsFile);
const result = shebangPlugin(bundler, [
{ interpreter: 'node', files: ['./mocks/example.js'] },
{ interpreter: 'python3', files: ['./mocks/example.js'] },
]);
await bundler.bundle();
const content = fs.readFileSync(
path.join(__dirname, './test/example.js'),
'utf-8'
);
expect(result).toBeTruthy();
expect(hasShebang(content)).toBeTruthy();
});
it('should plugin process for children entry point', async () => {
const bundler = createBundler(exampleHtmlFile);
const result = shebangPlugin(bundler, [
{ interpreter: 'python3', files: ['./mocks/example.js'] },
]);
const bundle = await bundler.bundle();
let content = '';
bundle.childBundles.forEach(({ name, parentBundle }) => {
if (parentBundle.entryAsset.name === exampleHtmlFile) {
content = fs.readFileSync(name, 'utf-8');
}
});
expect(result).toBeTruthy();
expect(hasShebang(content)).toBeTruthy();
});
});