/
validate-package-json.spec.ts
172 lines (153 loc) · 6.78 KB
/
validate-package-json.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import type * as d from '@stencil/core/declarations';
import { mockBuildCtx, mockCompilerCtx, mockConfig } from '@stencil/core/testing';
import * as v from '../validate-build-package-json';
import path from 'path';
describe('validate-package-json', () => {
let config: d.Config;
let compilerCtx: d.CompilerCtx;
let buildCtx: d.BuildCtx;
let collectionOutputTarget: d.OutputTargetDistCollection;
let typesOutputTarget: d.OutputTargetDistTypes;
const root = path.resolve('/');
beforeEach(async () => {
collectionOutputTarget = {
type: 'dist-collection',
dir: '/dist',
collectionDir: '/dist/collection',
};
typesOutputTarget = {
type: 'dist-types',
dir: '/dist',
typesDir: '/dist/types',
};
config = mockConfig();
config.devMode = false;
config.namespace = 'SomeNamespace';
config.fsNamespace = config.namespace.toLowerCase();
compilerCtx = mockCompilerCtx(config);
buildCtx = mockBuildCtx(config, compilerCtx);
buildCtx.packageJson = {};
config.packageJsonFilePath = path.join(root, 'package.json');
await compilerCtx.fs.writeFile(config.packageJsonFilePath, JSON.stringify(buildCtx.packageJson));
});
describe('files', () => {
it('should validate files "dist/"', async () => {
const distPath = path.join(root, 'dist');
await compilerCtx.fs.emptyDirs([distPath]);
await compilerCtx.fs.commit();
buildCtx.packageJson.files = ['dist/'];
await v.validatePackageFiles(config, compilerCtx, buildCtx, collectionOutputTarget);
expect(buildCtx.diagnostics).toHaveLength(0);
});
it('should validate files "./dist/"', async () => {
const distPath = path.join(root, 'dist');
await compilerCtx.fs.emptyDirs([distPath]);
await compilerCtx.fs.commit();
buildCtx.packageJson.files = ['./dist/'];
await v.validatePackageFiles(config, compilerCtx, buildCtx, collectionOutputTarget);
expect(buildCtx.diagnostics).toHaveLength(0);
});
it('should validate files "./dist"', async () => {
const distPath = path.join(root, 'dist');
await compilerCtx.fs.emptyDirs([distPath]);
await compilerCtx.fs.commit();
buildCtx.packageJson.files = ['./dist'];
await v.validatePackageFiles(config, compilerCtx, buildCtx, collectionOutputTarget);
expect(buildCtx.diagnostics).toHaveLength(0);
});
it('should validate files "dist"', async () => {
const distPath = path.join(root, 'dist');
await compilerCtx.fs.emptyDirs([distPath]);
await compilerCtx.fs.commit();
buildCtx.packageJson.files = ['dist'];
await v.validatePackageFiles(config, compilerCtx, buildCtx, collectionOutputTarget);
expect(buildCtx.diagnostics).toHaveLength(0);
});
it('should error when files array misses dist/', async () => {
buildCtx.packageJson.files = [];
await v.validatePackageFiles(config, compilerCtx, buildCtx, collectionOutputTarget);
expect(buildCtx.diagnostics[0].messageText).toMatch(/array must contain the distribution directory/);
expect(buildCtx.diagnostics[0].messageText).toMatch(/"dist\/"/);
});
});
describe('module', () => {
it('validate dist module', async () => {
config.outputTargets = [];
compilerCtx.fs.writeFile(path.join(root, 'dist', 'index.js'), '');
buildCtx.packageJson.module = 'dist/index.js';
v.validateModule(config, compilerCtx, buildCtx, collectionOutputTarget);
expect(buildCtx.diagnostics).toHaveLength(0);
});
it('validate custom elements module', async () => {
config.outputTargets = [{
type: 'dist-custom-elements-bundle',
dir: path.join(root, 'custom-elements')
}];
compilerCtx.fs.writeFile(path.join(root, 'dist', 'index.js'), '');
buildCtx.packageJson.module = 'custom-elements/index.js';
v.validateModule(config, compilerCtx, buildCtx, collectionOutputTarget);
expect(buildCtx.diagnostics).toHaveLength(0);
});
it('missing dist module', async () => {
config.outputTargets = [];
v.validateModule(config, compilerCtx, buildCtx, collectionOutputTarget);
expect(buildCtx.diagnostics).toHaveLength(1);
});
it('missing dist module, but has custom elements output', async () => {
config.outputTargets = [{
type: 'dist-custom-elements-bundle',
dir: path.join(root, 'custom-elements')
}];
v.validateModule(config, compilerCtx, buildCtx, collectionOutputTarget);
expect(buildCtx.diagnostics).toHaveLength(1);
});
});
describe('main', () => {
it('main cannot be the old loader', async () => {
compilerCtx.fs.writeFile(path.join(root, 'dist', 'somenamespace.js'), '');
compilerCtx.fs.writeFile(path.join(root, 'dist', 'index.cjs.js'), '');
buildCtx.packageJson.main = 'dist/somenamespace.js';
v.validateMain(config, compilerCtx, buildCtx, collectionOutputTarget);
expect(buildCtx.diagnostics).toHaveLength(1);
});
it('validate main', async () => {
compilerCtx.fs.writeFile(path.join(root, 'dist', 'index.cjs.js'), '');
buildCtx.packageJson.main = 'dist/index.cjs.js';
v.validateMain(config, compilerCtx, buildCtx, collectionOutputTarget);
expect(buildCtx.diagnostics).toHaveLength(0);
});
it('missing main', async () => {
v.validateMain(config, compilerCtx, buildCtx, collectionOutputTarget);
expect(buildCtx.diagnostics).toHaveLength(1);
});
});
describe('types', () => {
it('validate types', async () => {
compilerCtx.fs.writeFile(path.join(root, 'dist', 'types', 'components.d.ts'), '');
buildCtx.packageJson.types = 'dist/types/components.d.ts';
await v.validateTypes(config, compilerCtx, buildCtx, typesOutputTarget);
expect(buildCtx.diagnostics).toHaveLength(0);
});
it('not d.ts file', async () => {
compilerCtx.fs.writeFile(path.join(root, 'dist', 'types', 'components.d.ts'), '');
buildCtx.packageJson.types = 'dist/types/components.ts';
v.validateTypes(config, compilerCtx, buildCtx, typesOutputTarget);
expect(buildCtx.diagnostics).toHaveLength(1);
});
it('missing types file', async () => {
buildCtx.packageJson.types = 'dist/types/components.d.ts';
await v.validateTypes(config, compilerCtx, buildCtx, typesOutputTarget);
expect(buildCtx.diagnostics).toHaveLength(1);
});
it('missing types', async () => {
v.validateTypes(config, compilerCtx, buildCtx, typesOutputTarget);
expect(buildCtx.diagnostics).toHaveLength(1);
});
});
describe('collection', () => {
it('should error when missing collection property', async () => {
v.validateCollection(config, compilerCtx, buildCtx, collectionOutputTarget);
expect(buildCtx.diagnostics[0].messageText).toMatch(/package.json "collection" property is required/);
});
});
});