/
UpwardIncludePlugin.spec.js
119 lines (106 loc) 路 3.4 KB
/
UpwardIncludePlugin.spec.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
const { join } = require('path');
const MemoryFS = require('memory-fs');
const webpack = require('webpack');
const jsYaml = require('js-yaml');
const { mockBuildBus } = require('../../../TestHelpers');
const UpwardIncludePlugin = require('../UpwardIncludePlugin');
const basic3PageProjectDir = join(
__dirname,
'__fixtures__/basic-project-3-pages'
);
const basic1PageProjectDir = join(
__dirname,
'__fixtures__/basic-project-1-page'
);
const missingUpwardFileDir = join(__dirname, '__fixtures/dupe-root-component');
const badUpwardFileDir = join(__dirname, '__fixtures__/missing-page-types');
const compile = config =>
new Promise((resolve, reject) => {
const fs = new MemoryFS();
const compiler = webpack(config);
compiler.outputFileSystem = fs;
compiler.run((err, stats) => {
if (err || stats.hasErrors()) {
reject(new Error(err || stats.toString()));
} else {
resolve({ fs, stats });
}
});
});
test('merges upward files and resources', async () => {
const bus = mockBuildBus({
context: __dirname,
dependencies: [{ name: '@magento/pwa-buildpack' }]
});
bus.runPhase('declare');
const config = {
context: basic1PageProjectDir,
entry: {
main: join(basic1PageProjectDir, 'entry.js')
},
output: {
path: join(basic1PageProjectDir, 'dist')
},
plugins: [
new UpwardIncludePlugin({
bus,
upwardDirs: [basic3PageProjectDir, basic1PageProjectDir]
})
]
};
const {
stats: {
compilation: { assets }
}
} = await compile(config);
expect(assets['upward.yml']).toBeTruthy();
expect(jsYaml.safeLoad(assets['upward.yml'].source())).toMatchObject({
nothing: './nothing.json',
basicProject1Page: 'foo',
nonexistentFile: './unknown.pif',
status: 200
});
expect(assets['nothing.json']).toBeTruthy();
expect(JSON.parse(assets['nothing.json'].source())).toMatchObject({
nothing: 'to see here'
});
expect(assets['robots.txt']).toBeTruthy();
expect(assets['robots.txt'].source().toString()).toMatch(/Deny/);
expect(assets).not.toHaveProperty('unknown.pif');
});
test('handles missing upward file', async () => {
const config = {
context: missingUpwardFileDir,
entry: {
main: join(missingUpwardFileDir, 'entry.js')
},
output: {
path: join(missingUpwardFileDir, 'dist')
},
plugins: [
new UpwardIncludePlugin({
upwardDirs: [missingUpwardFileDir]
})
]
};
// const { stats: { compilation: { assets } } } = await compile(config);
await expect(compile(config)).rejects.toThrow(/unable to read file/);
});
test('handles bad upward file', async () => {
const config = {
context: badUpwardFileDir,
entry: {
main: join(badUpwardFileDir, 'entry.js')
},
output: {
path: join(badUpwardFileDir, 'dist')
},
plugins: [
new UpwardIncludePlugin({
upwardDirs: [badUpwardFileDir]
})
]
};
// const { stats: { compilation: { assets } } } = await compile(config);
await expect(compile(config)).rejects.toThrow(/error parsing/);
});